Run the entire test suite under replicated SQLite on DBICTEST_VIA_REPLICATED
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_do.t
1 use strict;
2 use warnings;
3
4 BEGIN { $ENV{DBICTEST_VIA_REPLICATED} = 0 }
5
6 # !!! do not replace this with done_testing - tests reside in the callbacks
7 # !!! number of calls is important
8 use Test::More tests => 13;
9 # !!!
10 use Test::Warn;
11 use Test::Exception;
12
13 use lib qw(t/lib);
14 use DBICTest;
15 require DBI;
16
17
18 my $schema = DBICTest->init_schema(
19     no_connect  => 1,
20     no_deploy   => 1,
21 );
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
38 ok $schema->connection(
39     sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 }) },
40     {
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
46         on_connect_do       => [
47             'CREATE TABLE TEST_empty (id INTEGER)',
48             [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
49             \&insert_from_subref,
50         ],
51         on_disconnect_do    =>
52             [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
53     },
54 ), 'connection()';
55
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
61 is_deeply (
62   $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
63   [ [ 2 ], [ 3 ], [ 7 ] ],
64   'on_connect_do() worked'
65 );
66 dies_ok {
67   $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent');
68 } 'Searching for nonexistent table dies';
69
70 $schema->storage->disconnect();
71
72 my($connected, $disconnected, @cb_args);
73 ok $schema->connection(
74     DBICTest->_database,
75     {
76         on_connect_do       => sub { $connected = 1; @cb_args = @_; },
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
86 isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook');
87 @cb_args = ();
88
89 sub check_exists {
90     my $storage = shift;
91     ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
92     return;
93 }
94
95 sub check_dropped {
96     my $storage = shift;
97
98     dies_ok {
99       $storage->dbh->do('SELECT 1 FROM TEST_empty');
100     } 'Reading from dropped table fails';
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     ];
110 }