Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
726c8f65 |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | use Test::Exception; |
726c8f65 |
8 | use Try::Tiny; |
199fbc45 |
9 | use DBIx::Class::Optional::Dependencies (); |
bbf6a9a5 |
10 | use DBIx::Class::_Util 'scope_guard'; |
c0329273 |
11 | |
726c8f65 |
12 | use DBICTest; |
13 | |
199fbc45 |
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/}; |
16 | |
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')) |
21 | unless |
22 | $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_odbc') |
23 | or |
24 | $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_ado') |
25 | or |
26 | (not $dsn || $dsn2); |
27 | |
726c8f65 |
28 | DBICTest::Schema->load_classes('ArtistGUID'); |
29 | |
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' |
34 | |
726c8f65 |
35 | plan skip_all => <<'EOF' unless $dsn || $dsn2; |
076bd599 |
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'. |
726c8f65 |
38 | EOF |
39 | |
726c8f65 |
40 | my @info = ( |
41 | [ $dsn, $user || '', $pass || '' ], |
42 | [ $dsn2, $user2 || '', $pass2 || '' ], |
43 | ); |
44 | |
726c8f65 |
45 | foreach my $info (@info) { |
46 | my ($dsn, $user, $pass) = @$info; |
47 | |
48 | next unless $dsn; |
49 | |
50 | # Check that we can connect without any options. |
2d48959a |
51 | my $schema = DBICTest::Schema->connect($dsn, $user, $pass); |
726c8f65 |
52 | lives_ok { |
53 | $schema->storage->ensure_connected; |
54 | } 'connection without any options'; |
55 | |
56 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
57 | $binstr{'large'} = $binstr{'small'} x 1024; |
58 | |
59 | my $maxloblen = length $binstr{'large'}; |
60 | |
61 | $schema = DBICTest::Schema->connect($dsn, $user, $pass, { |
62 | quote_names => 1, |
63 | auto_savepoint => 1, |
64 | LongReadLen => $maxloblen, |
65 | }); |
66 | |
bbf6a9a5 |
67 | my $guard = scope_guard { cleanup($schema) }; |
726c8f65 |
68 | |
69 | my $dbh = $schema->storage->dbh; |
70 | |
71 | # turn off warnings for OLE exception from ADO about nonexistant table |
72 | eval { local $^W = 0; $dbh->do("DROP TABLE artist") }; |
73 | |
74 | $dbh->do(<<EOF); |
75 | CREATE TABLE artist ( |
76 | artistid AUTOINCREMENT PRIMARY KEY, |
77 | name VARCHAR(255) NULL, |
78 | charfield CHAR(10) NULL, |
79 | rank INT NULL |
80 | ) |
81 | EOF |
82 | |
83 | my $ars = $schema->resultset('Artist'); |
84 | is ( $ars->count, 0, 'No rows at first' ); |
85 | |
86 | # test primary key handling |
87 | my $new = $ars->create({ name => 'foo' }); |
88 | ok($new->artistid, "Auto-PK worked"); |
89 | |
90 | my $first_artistid = $new->artistid; |
91 | |
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'); |
97 | |
98 | # test joins |
99 | eval { local $^W = 0; $dbh->do("DROP TABLE cd") }; |
100 | |
101 | $dbh->do(<<EOF); |
102 | CREATE TABLE cd ( |
103 | cdid AUTOINCREMENT PRIMARY KEY, |
104 | artist INTEGER NULL, |
105 | title VARCHAR(255) NULL, |
106 | [year] CHAR(4) NULL, |
107 | genreid INTEGER NULL, |
108 | single_track INTEGER NULL |
109 | ) |
110 | EOF |
111 | |
112 | $dbh->do(<<EOF); |
113 | CREATE TABLE track ( |
114 | trackid AUTOINCREMENT PRIMARY KEY, |
115 | cd INTEGER REFERENCES cd(cdid), |
116 | [position] INTEGER, |
117 | title VARCHAR(255), |
118 | last_updated_on DATETIME, |
119 | last_updated_at DATETIME |
120 | ) |
121 | EOF |
122 | |
123 | my $cd = $schema->resultset('CD')->create({ |
124 | artist => $first_artistid, |
125 | title => 'Some Album', |
126 | }); |
127 | |
128 | # one-step join |
129 | my $joined_artist = $schema->resultset('Artist')->search({ |
130 | artistid => $first_artistid, |
131 | }, { |
132 | join => [ 'cds' ], |
133 | '+select' => [ 'cds.title' ], |
134 | '+as' => [ 'cd_title' ], |
135 | })->next; |
136 | |
137 | is $joined_artist->get_column('cd_title'), 'Some Album', |
138 | 'one-step join works'; |
139 | |
140 | # two-step join |
141 | my $track = $schema->resultset('Track')->create({ |
142 | cd => $cd->cdid, |
143 | position => 1, |
144 | title => 'my track', |
145 | }); |
146 | |
147 | my $joined_track = try { |
148 | $schema->resultset('Artist')->search({ |
149 | artistid => $first_artistid, |
150 | }, { |
151 | join => [{ cds => 'tracks' }], |
152 | '+select' => [ 'tracks.title' ], |
153 | '+as' => [ 'track_title' ], |
154 | })->next; |
155 | } |
156 | catch { |
696ba760 |
157 | diag "Could not execute two-step left join: $_"; |
726c8f65 |
158 | }; |
159 | |
160 | is try { $joined_track->get_column('track_title') }, 'my track', |
696ba760 |
161 | 'two-step left join works'; |
162 | |
696ba760 |
163 | $joined_artist = try { |
696ba760 |
164 | $schema->resultset('Track')->search({ |
165 | trackid => $track->trackid, |
166 | }, { |
167 | join => [{ cd => 'artist' }], |
168 | '+select' => [ 'artist.name' ], |
169 | '+as' => [ 'artist_name' ], |
170 | })->next; |
171 | } |
172 | catch { |
173 | diag "Could not execute two-step inner join: $_"; |
174 | }; |
175 | |
696ba760 |
176 | is try { $joined_artist->get_column('artist_name') }, 'foo', |
177 | 'two-step inner join works'; |
726c8f65 |
178 | |
179 | # test basic transactions |
180 | $schema->txn_do(sub { |
181 | $ars->create({ name => 'transaction_commit' }); |
182 | }); |
183 | ok($ars->search({ name => 'transaction_commit' })->first, |
184 | 'transaction committed'); |
185 | $ars->search({ name => 'transaction_commit' })->delete, |
186 | throws_ok { |
187 | $schema->txn_do(sub { |
188 | $ars->create({ name => 'transaction_rollback' }); |
189 | die 'rolling back'; |
190 | }); |
191 | } qr/rolling back/, 'rollback executed'; |
192 | is $ars->search({ name => 'transaction_rollback' })->first, undef, |
193 | 'transaction rolled back'; |
194 | |
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' }); |
200 | }); |
201 | ok($ars->search({ name => 'in_inner_transaction' })->first, |
202 | 'commit from inner transaction visible in outer transaction'); |
203 | throws_ok { |
204 | $schema->txn_do(sub { |
205 | $ars->create({ name => 'in_inner_transaction_rolling_back' }); |
206 | die 'rolling back inner transaction'; |
207 | }); |
208 | } qr/rolling back inner transaction/, 'inner transaction rollback executed'; |
209 | }); |
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, |
215 | undef, |
216 | 'rollback from inner transaction'; |
217 | $ars->search({ name => 'in_outer_transaction' })->delete; |
218 | $ars->search({ name => 'in_inner_transaction' })->delete; |
219 | |
220 | # test populate |
221 | lives_ok (sub { |
222 | my @pop; |
223 | for (1..2) { |
224 | push @pop, { name => "Artist_$_" }; |
225 | } |
226 | $ars->populate (\@pop); |
227 | }); |
228 | |
229 | # test populate with explicit key |
230 | lives_ok (sub { |
231 | my @pop; |
232 | for (1..2) { |
233 | push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ }; |
234 | } |
235 | $ars->populate (\@pop); |
236 | }); |
237 | |
238 | # count what we did so far |
239 | is ($ars->count, 6, 'Simple count works'); |
240 | |
241 | # test LIMIT support |
242 | # not testing offset because access only supports TOP |
243 | my $lim = $ars->search( {}, |
244 | { |
245 | rows => 2, |
246 | offset => 0, |
247 | order_by => 'artistid' |
248 | } |
249 | ); |
250 | is( $lim->count, 2, 'ROWS+OFFSET count ok' ); |
251 | is( $lim->all, 2, 'Number of ->all objects matches count' ); |
252 | |
253 | # test iterator |
254 | $lim->reset; |
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" ); |
258 | |
259 | # test empty insert |
260 | my $current_artistid = $ars->search({}, { |
261 | select => [ { max => 'artistid' } ], as => ['artistid'] |
262 | })->first->artistid; |
263 | |
264 | my $row; |
265 | lives_ok { $row = $ars->create({}) } |
266 | 'empty insert works'; |
267 | |
268 | $row->discard_changes; |
269 | |
270 | is $row->artistid, $current_artistid+1, |
271 | 'empty insert generated correct PK'; |
272 | |
273 | # test that autoinc column still works after empty insert |
274 | $row = $ars->create({ name => 'after_empty_insert' }); |
275 | |
276 | is $row->artistid, $current_artistid+2, |
277 | 'autoincrement column functional aftear empty insert'; |
278 | |
279 | # test blobs (stolen from 73oracle.t) |
280 | |
281 | # turn off horrendous binary DBIC_TRACE output |
282 | { |
283 | local $schema->storage->{debug} = 0; |
284 | |
285 | eval { local $^W = 0; $dbh->do('DROP TABLE bindtype_test') }; |
286 | $dbh->do(qq[ |
287 | CREATE TABLE bindtype_test |
288 | ( |
289 | id INT NOT NULL PRIMARY KEY, |
290 | bytea INT NULL, |
291 | blob IMAGE NULL, |
292 | clob TEXT NULL, |
293 | a_memo MEMO NULL |
294 | ) |
295 | ],{ RaiseError => 1, PrintError => 1 }); |
296 | |
297 | my $rs = $schema->resultset('BindType'); |
298 | my $id = 0; |
299 | |
300 | foreach my $type (qw( blob clob a_memo )) { |
301 | foreach my $size (qw( small large )) { |
302 | SKIP: { |
303 | skip 'TEXT columns not cast to MEMO over ODBC', 2 |
304 | if $type eq 'clob' && $size eq 'large' && $dsn =~ /:ODBC:/; |
305 | |
306 | $id++; |
307 | |
308 | lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) } |
309 | "inserted $size $type without dying" or next; |
310 | |
311 | my $from_db = eval { $rs->find($id)->$type } || ''; |
312 | diag $@ if $@; |
313 | |
314 | ok($from_db eq $binstr{$size}, "verified inserted $size $type" ) |
315 | or do { |
316 | my $hexdump = sub { |
317 | join '', map sprintf('%02X', ord), split //, shift |
318 | }; |
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); |
326 | }; |
327 | } |
328 | } |
329 | } |
330 | # test IMAGE update |
331 | lives_ok { |
332 | $rs->search({ id => 0 })->update({ blob => $binstr{small} }); |
333 | } 'updated IMAGE to small binstr without dying'; |
334 | |
335 | lives_ok { |
336 | $rs->search({ id => 0 })->update({ blob => $binstr{large} }); |
337 | } 'updated IMAGE to large binstr without dying'; |
338 | } |
339 | |
340 | # test GUIDs (and the cursor GUID fixup stuff for ADO) |
341 | |
342 | require Data::GUID; |
343 | $schema->storage->new_guid(sub { Data::GUID->new->as_string }); |
344 | |
345 | local $schema->source('ArtistGUID')->column_info('artistid')->{data_type} |
346 | = 'guid'; |
347 | |
348 | local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type} |
349 | = 'guid'; |
350 | |
351 | $schema->storage->dbh_do (sub { |
352 | my ($storage, $dbh) = @_; |
353 | eval { local $^W = 0; $dbh->do("DROP TABLE artist_guid") }; |
354 | $dbh->do(<<"SQL"); |
355 | CREATE TABLE artist_guid ( |
356 | artistid GUID NOT NULL, |
357 | name VARCHAR(100), |
358 | rank INT NULL, |
359 | charfield CHAR(10) NULL, |
360 | a_guid GUID, |
361 | primary key(artistid) |
362 | ) |
363 | SQL |
364 | }); |
365 | |
366 | lives_ok { |
367 | $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' }) |
368 | } 'created a row with a GUID'; |
369 | |
370 | ok( |
371 | eval { $row->artistid }, |
372 | 'row has GUID PK col populated', |
373 | ); |
374 | diag $@ if $@; |
375 | |
376 | ok( |
377 | eval { $row->a_guid }, |
378 | 'row has a GUID col with auto_nextval populated', |
379 | ); |
380 | diag $@ if $@; |
381 | |
382 | my $row_from_db = $schema->resultset('ArtistGUID') |
383 | ->search({ name => 'mtfnpy' })->first; |
384 | |
385 | is $row_from_db->artistid, $row->artistid, |
386 | 'PK GUID round trip (via ->search->next)'; |
387 | |
388 | is $row_from_db->a_guid, $row->a_guid, |
389 | 'NON-PK GUID round trip (via ->search->next)'; |
390 | |
391 | $row_from_db = $schema->resultset('ArtistGUID') |
392 | ->find($row->artistid); |
393 | |
394 | is $row_from_db->artistid, $row->artistid, |
395 | 'PK GUID round trip (via ->find)'; |
396 | |
397 | is $row_from_db->a_guid, $row->a_guid, |
398 | 'NON-PK GUID round trip (via ->find)'; |
399 | |
400 | ($row_from_db) = $schema->resultset('ArtistGUID') |
401 | ->search({ name => 'mtfnpy' })->all; |
402 | |
403 | is $row_from_db->artistid, $row->artistid, |
404 | 'PK GUID round trip (via ->search->all)'; |
405 | |
406 | is $row_from_db->a_guid, $row->a_guid, |
407 | 'NON-PK GUID round trip (via ->search->all)'; |
408 | } |
409 | |
410 | done_testing; |
411 | |
412 | sub cleanup { |
2d48959a |
413 | my $schema = shift; |
414 | |
726c8f65 |
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/; |
421 | } |
422 | } |
423 | |
424 | # vim:sts=2 sw=2: |