1#!/usr/bin/perl -w 2 3 4use strict; 5use warnings; 6use Test::More; 7BEGIN { require "t/utils.pl" } 8our (@AvailableDrivers); 9 10use constant TESTS_PER_DRIVER => 16; 11 12my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER; 13plan tests => $total; 14 15foreach my $d ( @AvailableDrivers ) { 16SKIP: { 17 unless( has_schema( 'TestApp::Address', $d ) ) { 18 skip "No schema for '$d' driver", TESTS_PER_DRIVER; 19 } 20 unless( should_test( $d ) ) { 21 skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER; 22 } 23 24 my $handle = get_handle( $d ); 25 connect_handle( $handle ); 26 isa_ok($handle->dbh, 'DBI::db'); 27 28 my $ret = init_schema( 'TestApp::Address', $handle ); 29 isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back"); 30 31 my $rec = TestApp::Address->new($handle); 32 isa_ok($rec, 'DBIx::SearchBuilder::Record'); 33 34 my ($id) = $rec->Create( Name => 'Jesse', Phone => '617 124 567'); 35 ok($id,"Created record #$id"); 36 37 ok($rec->Load($id), "Loaded the record"); 38 is($rec->id, $id, "The record has its id"); 39 is($rec->Name, 'Jesse', "The record's name is Jesse"); 40 41 my $rec_cache = TestApp::Address->new($handle); 42 my ($status, $msg) = $rec_cache->LoadById($id); 43 ok($status, 'loaded record'); 44 is($rec_cache->id, $id, 'the same record as we created'); 45 is($msg, 'Fetched from cache', 'we fetched record from cache'); 46 47 DBIx::SearchBuilder::Record::Cachable->FlushCache; 48 49 ok($rec->LoadByCols( Name => 'Jesse' ), "Loaded the record"); 50 is($rec->id, $id, "The record has its id"); 51 is($rec->Name, 'Jesse', "The record's name is Jesse"); 52 53 $rec_cache = TestApp::Address->new($handle); 54 ($status, $msg) = $rec_cache->LoadById($id); 55 ok($status, 'loaded record'); 56 is($rec_cache->id, $id, 'the same record as we created'); 57 is($msg, 'Fetched from cache', 'we fetched record from cache'); 58 59 cleanup_schema( 'TestApp::Address', $handle ); 60}} # SKIP, foreach blocks 61 621; 63 64 65 66package TestApp::Address; 67 68use base qw/DBIx::SearchBuilder::Record::Cachable/; 69 70sub _Init { 71 my $self = shift; 72 my $handle = shift; 73 $self->Table('Address'); 74 $self->_Handle($handle); 75} 76 77sub _ClassAccessible { 78 return { 79 id => 80 {read => 1, type => 'int(11)', default => ''}, 81 Name => 82 {read => 1, write => 1, type => 'varchar(14)', default => ''}, 83 Phone => 84 {read => 1, write => 1, type => 'varchar(18)', length => 18, default => ''}, 85 EmployeeId => 86 {read => 1, write => 1, type => 'int(8)', default => ''}, 87 88 } 89} 90 91sub _CacheConfig { 92 return { 93 'cache_for_sec' => 60, 94 }; 95} 96 97sub schema_mysql { 98<<EOF; 99CREATE TEMPORARY TABLE Address ( 100 id integer AUTO_INCREMENT, 101 Name varchar(36), 102 Phone varchar(18), 103 EmployeeId int(8), 104 PRIMARY KEY (id)) 105EOF 106 107} 108 109sub schema_pg { 110<<EOF; 111CREATE TEMPORARY TABLE Address ( 112 id serial PRIMARY KEY, 113 Name varchar, 114 Phone varchar, 115 EmployeeId integer 116) 117EOF 118 119} 120 121sub schema_sqlite { 122 123<<EOF; 124CREATE TABLE Address ( 125 id integer primary key, 126 Name varchar(36), 127 Phone varchar(18), 128 EmployeeId int(8)) 129EOF 130 131} 132 133sub schema_oracle { [ 134 "CREATE SEQUENCE Address_seq", 135 "CREATE TABLE Address ( 136 id integer CONSTRAINT Address_Key PRIMARY KEY, 137 Name varchar(36), 138 Phone varchar(18), 139 EmployeeId integer 140 )", 141] } 142 143sub cleanup_schema_oracle { [ 144 "DROP SEQUENCE Address_seq", 145 "DROP TABLE Address", 146] } 147 1481; 149