1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use DBIx::Class::Optional::Dependencies ();
10 use DBIx::Class::_Util 'scope_guard';
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSACCESS_ODBC_${_}" } qw/DSN USER PASS/};
15 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSACCESS_ADO_${_}" } qw/DSN USER PASS/};
17 plan skip_all => 'Test needs ' .
18 (join ' or ', map { $_ ? $_ : () }
19 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_odbc'),
20 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_ado'))
22 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_odbc')
24 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_ado')
28 DBICTest::Schema->load_classes('ArtistGUID');
30 # Example DSNs (32bit only):
31 # dbi:ODBC:driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=C:\Users\rkitover\Documents\access_sample.accdb
32 # dbi:ADO:Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb
33 # dbi:ADO:Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\rkitover\Documents\access_sample.accdb;Persist Security Info=False'
35 plan skip_all => <<'EOF' unless $dsn || $dsn2;
36 Set $ENV{DBICTEST_MSACCESS_ODBC_DSN} and/or $ENV{DBICTEST_MSACCESS_ADO_DSN} (and optionally _USER and _PASS) to run these tests.
37 Warning: this test drops and creates the tables 'artist', 'cd', 'bindtype_test' and 'artist_guid'.
41 [ $dsn, $user || '', $pass || '' ],
42 [ $dsn2, $user2 || '', $pass2 || '' ],
45 foreach my $info (@info) {
46 my ($dsn, $user, $pass) = @$info;
50 # Check that we can connect without any options.
51 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
53 $schema->storage->ensure_connected;
54 } 'connection without any options';
56 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
57 $binstr{'large'} = $binstr{'small'} x 1024;
59 my $maxloblen = length $binstr{'large'};
61 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
64 LongReadLen => $maxloblen,
67 my $guard = scope_guard { cleanup($schema) };
69 my $dbh = $schema->storage->dbh;
71 # turn off warnings for OLE exception from ADO about nonexistant table
72 eval { local $^W = 0; $dbh->do("DROP TABLE artist") };
76 artistid AUTOINCREMENT PRIMARY KEY,
77 name VARCHAR(255) NULL,
78 charfield CHAR(10) NULL,
83 my $ars = $schema->resultset('Artist');
84 is ( $ars->count, 0, 'No rows at first' );
86 # test primary key handling
87 my $new = $ars->create({ name => 'foo' });
88 ok($new->artistid, "Auto-PK worked");
90 my $first_artistid = $new->artistid;
92 # test explicit key spec
93 $new = $ars->create ({ name => 'bar', artistid => 66 });
94 is($new->artistid, 66, 'Explicit PK worked');
95 $new->discard_changes;
96 is($new->artistid, 66, 'Explicit PK assigned');
99 eval { local $^W = 0; $dbh->do("DROP TABLE cd") };
103 cdid AUTOINCREMENT PRIMARY KEY,
105 title VARCHAR(255) NULL,
107 genreid INTEGER NULL,
108 single_track INTEGER NULL
114 trackid AUTOINCREMENT PRIMARY KEY,
115 cd INTEGER REFERENCES cd(cdid),
118 last_updated_on DATETIME,
119 last_updated_at DATETIME
123 my $cd = $schema->resultset('CD')->create({
124 artist => $first_artistid,
125 title => 'Some Album',
129 my $joined_artist = $schema->resultset('Artist')->search({
130 artistid => $first_artistid,
133 '+select' => [ 'cds.title' ],
134 '+as' => [ 'cd_title' ],
137 is $joined_artist->get_column('cd_title'), 'Some Album',
138 'one-step join works';
141 my $track = $schema->resultset('Track')->create({
147 my $joined_track = try {
148 $schema->resultset('Artist')->search({
149 artistid => $first_artistid,
151 join => [{ cds => 'tracks' }],
152 '+select' => [ 'tracks.title' ],
153 '+as' => [ 'track_title' ],
157 diag "Could not execute two-step left join: $_";
160 is try { $joined_track->get_column('track_title') }, 'my track',
161 'two-step left join works';
163 $joined_artist = try {
164 $schema->resultset('Track')->search({
165 trackid => $track->trackid,
167 join => [{ cd => 'artist' }],
168 '+select' => [ 'artist.name' ],
169 '+as' => [ 'artist_name' ],
173 diag "Could not execute two-step inner join: $_";
176 is try { $joined_artist->get_column('artist_name') }, 'foo',
177 'two-step inner join works';
179 # test basic transactions
180 $schema->txn_do(sub {
181 $ars->create({ name => 'transaction_commit' });
183 ok($ars->search({ name => 'transaction_commit' })->first,
184 'transaction committed');
185 $ars->search({ name => 'transaction_commit' })->delete,
187 $schema->txn_do(sub {
188 $ars->create({ name => 'transaction_rollback' });
191 } qr/rolling back/, 'rollback executed';
192 is $ars->search({ name => 'transaction_rollback' })->first, undef,
193 'transaction rolled back';
195 # test two-phase commit and inner transaction rollback from nested transactions
196 $schema->txn_do(sub {
197 $ars->create({ name => 'in_outer_transaction' });
198 $schema->txn_do(sub {
199 $ars->create({ name => 'in_inner_transaction' });
201 ok($ars->search({ name => 'in_inner_transaction' })->first,
202 'commit from inner transaction visible in outer transaction');
204 $schema->txn_do(sub {
205 $ars->create({ name => 'in_inner_transaction_rolling_back' });
206 die 'rolling back inner transaction';
208 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
210 ok($ars->search({ name => 'in_outer_transaction' })->first,
211 'commit from outer transaction');
212 ok($ars->search({ name => 'in_inner_transaction' })->first,
213 'commit from inner transaction');
214 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
216 'rollback from inner transaction';
217 $ars->search({ name => 'in_outer_transaction' })->delete;
218 $ars->search({ name => 'in_inner_transaction' })->delete;
224 push @pop, { name => "Artist_$_" };
226 $ars->populate (\@pop);
229 # test populate with explicit key
233 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
235 $ars->populate (\@pop);
238 # count what we did so far
239 is ($ars->count, 6, 'Simple count works');
242 # not testing offset because access only supports TOP
243 my $lim = $ars->search( {},
247 order_by => 'artistid'
250 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
251 is( $lim->all, 2, 'Number of ->all objects matches count' );
255 is( $lim->next->artistid, 1, "iterator->next ok" );
256 is( $lim->next->artistid, 66, "iterator->next ok" );
257 is( $lim->next, undef, "next past end of resultset ok" );
260 my $current_artistid = $ars->search({}, {
261 select => [ { max => 'artistid' } ], as => ['artistid']
265 lives_ok { $row = $ars->create({}) }
266 'empty insert works';
268 $row->discard_changes;
270 is $row->artistid, $current_artistid+1,
271 'empty insert generated correct PK';
273 # test that autoinc column still works after empty insert
274 $row = $ars->create({ name => 'after_empty_insert' });
276 is $row->artistid, $current_artistid+2,
277 'autoincrement column functional aftear empty insert';
279 # test blobs (stolen from 73oracle.t)
281 # turn off horrendous binary DBIC_TRACE output
283 local $schema->storage->{debug} = 0;
285 eval { local $^W = 0; $dbh->do('DROP TABLE bindtype_test') };
287 CREATE TABLE bindtype_test
289 id INT NOT NULL PRIMARY KEY,
295 ],{ RaiseError => 1, PrintError => 1 });
297 my $rs = $schema->resultset('BindType');
300 foreach my $type (qw( blob clob a_memo )) {
301 foreach my $size (qw( small large )) {
303 skip 'TEXT columns not cast to MEMO over ODBC', 2
304 if $type eq 'clob' && $size eq 'large' && $dsn =~ /:ODBC:/;
308 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
309 "inserted $size $type without dying" or next;
311 my $from_db = eval { $rs->find($id)->$type } || '';
314 ok($from_db eq $binstr{$size}, "verified inserted $size $type" )
317 join '', map sprintf('%02X', ord), split //, shift
319 diag 'Got: ', "\n", substr($hexdump->($from_db),0,255), '...',
320 substr($hexdump->($from_db),-255);
321 diag 'Size: ', length($from_db);
322 diag 'Expected Size: ', length($binstr{$size});
323 diag 'Expected: ', "\n",
324 substr($hexdump->($binstr{$size}), 0, 255),
325 "...", substr($hexdump->($binstr{$size}),-255);
332 $rs->search({ id => 0 })->update({ blob => $binstr{small} });
333 } 'updated IMAGE to small binstr without dying';
336 $rs->search({ id => 0 })->update({ blob => $binstr{large} });
337 } 'updated IMAGE to large binstr without dying';
340 # test GUIDs (and the cursor GUID fixup stuff for ADO)
343 $schema->storage->new_guid(sub { Data::GUID->new->as_string });
345 local $schema->source('ArtistGUID')->column_info('artistid')->{data_type}
348 local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type}
351 $schema->storage->dbh_do (sub {
352 my ($storage, $dbh) = @_;
353 eval { local $^W = 0; $dbh->do("DROP TABLE artist_guid") };
355 CREATE TABLE artist_guid (
356 artistid GUID NOT NULL,
359 charfield CHAR(10) NULL,
361 primary key(artistid)
367 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
368 } 'created a row with a GUID';
371 eval { $row->artistid },
372 'row has GUID PK col populated',
377 eval { $row->a_guid },
378 'row has a GUID col with auto_nextval populated',
382 my $row_from_db = $schema->resultset('ArtistGUID')
383 ->search({ name => 'mtfnpy' })->first;
385 is $row_from_db->artistid, $row->artistid,
386 'PK GUID round trip (via ->search->next)';
388 is $row_from_db->a_guid, $row->a_guid,
389 'NON-PK GUID round trip (via ->search->next)';
391 $row_from_db = $schema->resultset('ArtistGUID')
392 ->find($row->artistid);
394 is $row_from_db->artistid, $row->artistid,
395 'PK GUID round trip (via ->find)';
397 is $row_from_db->a_guid, $row->a_guid,
398 'NON-PK GUID round trip (via ->find)';
400 ($row_from_db) = $schema->resultset('ArtistGUID')
401 ->search({ name => 'mtfnpy' })->all;
403 is $row_from_db->artistid, $row->artistid,
404 'PK GUID round trip (via ->search->all)';
406 is $row_from_db->a_guid, $row->a_guid,
407 'NON-PK GUID round trip (via ->search->all)';
415 if (my $storage = eval { $schema->storage }) {
416 # cannot drop a table if it has been used, have to reconnect first
417 $schema->storage->disconnect;
418 local $^W = 0; # for ADO OLE exceptions
419 $schema->storage->dbh->do("DROP TABLE $_")
420 for qw/artist track cd bindtype_test artist_guid/;