a7362722177c1601be6d81cc9108dd8f8f8f4b1b
[dbsrgits/DBIx-Class.git] / t / 92storage_on_connect_do.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 9;
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     {
17         on_connect_do       => ['CREATE TABLE TEST_empty (id INTEGER)'],
18         on_disconnect_do    =>
19             [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
20     },
21 ), 'connection()';
22
23 ok $schema->storage->dbh->do('SELECT 1 FROM TEST_empty'), 'on_connect_do() worked';
24 eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); };
25 ok $@, 'Searching for nonexistent table dies';
26
27 $schema->storage->disconnect();
28
29 my($connected, $disconnected);
30 ok $schema->connection(
31     DBICTest->_database,
32     {
33         on_connect_do       => sub { $connected = 1 },
34         on_disconnect_do    => sub { $disconnected = 1 },
35     },
36 ), 'second connection()';
37 $schema->storage->dbh->do('SELECT 1');
38 ok $connected, 'on_connect_do() called after connect()';
39 ok ! $disconnected, 'on_disconnect_do() not called after connect()';
40 $schema->storage->disconnect();
41 ok $disconnected, 'on_disconnect_do() called after disconnect()';
42
43
44 sub check_exists {
45     my $storage = shift;
46     ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
47 }
48
49 sub check_dropped {
50     my $storage = shift;
51     eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); };
52     ok $@, 'Reading from dropped table fails';
53 }