new config option to DBICTest to let you set an alternative storage type, start on...
[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       => [
18             'CREATE TABLE TEST_empty (id INTEGER)',
19             [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
20             \&insert_from_subref,
21         ],
22         on_disconnect_do    =>
23             [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
24     },
25 ), 'connection()';
26
27 is_deeply
28   $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
29   [ [ 2 ], [ 3 ], [ 7 ] ],
30   'on_connect_do() worked';
31 eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); };
32 ok $@, 'Searching for nonexistent table dies';
33
34 $schema->storage->disconnect();
35
36 my($connected, $disconnected);
37 ok $schema->connection(
38     DBICTest->_database,
39     {
40         on_connect_do       => sub { $connected = 1 },
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
50
51 sub check_exists {
52     my $storage = shift;
53     ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
54     return;
55 }
56
57 sub check_dropped {
58     my $storage = shift;
59     eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); };
60     ok $@, 'Reading from dropped table fails';
61     return;
62 }
63
64 sub insert_from_subref {
65     my $storage = shift;
66     return [
67         [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ],
68         'INSERT INTO TEST_empty VALUES (7)',
69     ];
70 }