Commit | Line | Data |
579ca3f7 |
1 | use strict; |
2 | use warnings; |
3 | |
f3d405dc |
4 | # !!! do not replace this with done_testing - tests reside in the callbacks |
5 | # !!! number of calls is important |
6c925c72 |
6 | use Test::More tests => 13; |
f3d405dc |
7 | # !!! |
6c925c72 |
8 | use Test::Warn; |
f3d405dc |
9 | use Test::Exception; |
579ca3f7 |
10 | |
11 | use lib qw(t/lib); |
8d6b1478 |
12 | use DBICTest; |
7c9aa29f |
13 | require DBI; |
579ca3f7 |
14 | |
15 | |
16 | my $schema = DBICTest->init_schema( |
17 | no_connect => 1, |
18 | no_deploy => 1, |
19 | ); |
00f57441 |
20 | |
21 | ok $schema->connection( |
22 | DBICTest->_database, |
23 | { |
24 | on_connect_do => 'CREATE TABLE TEST_empty (id INTEGER)', |
25 | }, |
26 | ), 'connection()'; |
27 | |
28 | is_deeply ( |
29 | $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'), |
30 | [], |
31 | 'string version on_connect_do() worked' |
32 | ); |
33 | |
34 | $schema->storage->disconnect; |
35 | |
579ca3f7 |
36 | ok $schema->connection( |
6c925c72 |
37 | sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 }) }, |
579ca3f7 |
38 | { |
1bd1640b |
39 | on_connect_do => [ |
40 | 'CREATE TABLE TEST_empty (id INTEGER)', |
41 | [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ], |
1dafdb2a |
42 | \&insert_from_subref, |
1bd1640b |
43 | ], |
579ca3f7 |
44 | on_disconnect_do => |
45 | [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped], |
46 | }, |
47 | ), 'connection()'; |
48 | |
6c925c72 |
49 | warnings_exist { |
50 | $schema->storage->ensure_connected |
51 | } qr/The 'RaiseError' of the externally supplied DBI handle is set to false/, |
52 | 'Warning on clobbered AutoCommit => 0 fired'; |
53 | |
00f57441 |
54 | is_deeply ( |
1bd1640b |
55 | $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'), |
1dafdb2a |
56 | [ [ 2 ], [ 3 ], [ 7 ] ], |
00f57441 |
57 | 'on_connect_do() worked' |
58 | ); |
f3d405dc |
59 | dies_ok { |
60 | $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); |
61 | } 'Searching for nonexistent table dies'; |
579ca3f7 |
62 | |
63 | $schema->storage->disconnect(); |
64 | |
abe38018 |
65 | my($connected, $disconnected, @cb_args); |
6d2e7a96 |
66 | ok $schema->connection( |
67 | DBICTest->_database, |
68 | { |
abe38018 |
69 | on_connect_do => sub { $connected = 1; @cb_args = @_; }, |
6d2e7a96 |
70 | on_disconnect_do => sub { $disconnected = 1 }, |
71 | }, |
72 | ), 'second connection()'; |
73 | $schema->storage->dbh->do('SELECT 1'); |
74 | ok $connected, 'on_connect_do() called after connect()'; |
75 | ok ! $disconnected, 'on_disconnect_do() not called after connect()'; |
76 | $schema->storage->disconnect(); |
77 | ok $disconnected, 'on_disconnect_do() called after disconnect()'; |
78 | |
abe38018 |
79 | isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook'); |
6892eb09 |
80 | @cb_args = (); |
6d2e7a96 |
81 | |
579ca3f7 |
82 | sub check_exists { |
83 | my $storage = shift; |
84 | ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists'; |
1dafdb2a |
85 | return; |
579ca3f7 |
86 | } |
87 | |
88 | sub check_dropped { |
89 | my $storage = shift; |
f3d405dc |
90 | |
91 | dies_ok { |
92 | $storage->dbh->do('SELECT 1 FROM TEST_empty'); |
93 | } 'Reading from dropped table fails'; |
1dafdb2a |
94 | return; |
95 | } |
96 | |
97 | sub insert_from_subref { |
98 | my $storage = shift; |
99 | return [ |
100 | [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ], |
101 | 'INSERT INTO TEST_empty VALUES (7)', |
102 | ]; |
579ca3f7 |
103 | } |