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