Commit | Line | Data |
579ca3f7 |
1 | use strict; |
2 | use warnings; |
3 | |
abe38018 |
4 | use Test::More tests => 10; |
579ca3f7 |
5 | |
6 | use lib qw(t/lib); |
7 | use base 'DBICTest'; |
8 | |
9 | |
10 | my $schema = DBICTest->init_schema( |
11 | no_connect => 1, |
12 | no_deploy => 1, |
13 | ); |
14 | ok $schema->connection( |
15 | DBICTest->_database, |
16 | { |
1bd1640b |
17 | on_connect_do => [ |
18 | 'CREATE TABLE TEST_empty (id INTEGER)', |
19 | [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ], |
1dafdb2a |
20 | \&insert_from_subref, |
1bd1640b |
21 | ], |
579ca3f7 |
22 | on_disconnect_do => |
23 | [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped], |
24 | }, |
25 | ), 'connection()'; |
26 | |
1bd1640b |
27 | is_deeply |
28 | $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'), |
1dafdb2a |
29 | [ [ 2 ], [ 3 ], [ 7 ] ], |
1bd1640b |
30 | 'on_connect_do() worked'; |
579ca3f7 |
31 | eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); }; |
32 | ok $@, 'Searching for nonexistent table dies'; |
33 | |
34 | $schema->storage->disconnect(); |
35 | |
abe38018 |
36 | my($connected, $disconnected, @cb_args); |
6d2e7a96 |
37 | ok $schema->connection( |
38 | DBICTest->_database, |
39 | { |
abe38018 |
40 | on_connect_do => sub { $connected = 1; @cb_args = @_; }, |
6d2e7a96 |
41 | on_disconnect_do => sub { $disconnected = 1 }, |
42 | }, |
43 | ), 'second connection()'; |
44 | $schema->storage->dbh->do('SELECT 1'); |
45 | ok $connected, 'on_connect_do() called after connect()'; |
46 | ok ! $disconnected, 'on_disconnect_do() not called after connect()'; |
47 | $schema->storage->disconnect(); |
48 | ok $disconnected, 'on_disconnect_do() called after disconnect()'; |
49 | |
abe38018 |
50 | isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook'); |
6d2e7a96 |
51 | |
579ca3f7 |
52 | sub check_exists { |
53 | my $storage = shift; |
54 | ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists'; |
1dafdb2a |
55 | return; |
579ca3f7 |
56 | } |
57 | |
58 | sub check_dropped { |
59 | my $storage = shift; |
60 | eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); }; |
61 | ok $@, 'Reading from dropped table fails'; |
1dafdb2a |
62 | return; |
63 | } |
64 | |
65 | sub insert_from_subref { |
66 | my $storage = shift; |
67 | return [ |
68 | [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ], |
69 | 'INSERT INTO TEST_empty VALUES (7)', |
70 | ]; |
579ca3f7 |
71 | } |