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