better money value comparison in tests
[dbsrgits/DBIx-Class.git] / t / 92storage_on_connect_do.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 12;
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
15 ok $schema->connection(
16   DBICTest->_database,
17   {
18     on_connect_do => 'CREATE TABLE TEST_empty (id INTEGER)',
19   },
20 ), 'connection()';
21
22 is_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
30 ok $schema->connection(
31     DBICTest->_database,
32     {
33         on_connect_do       => [
34             'CREATE TABLE TEST_empty (id INTEGER)',
35             [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
36             \&insert_from_subref,
37         ],
38         on_disconnect_do    =>
39             [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
40     },
41 ), 'connection()';
42
43 is_deeply (
44   $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
45   [ [ 2 ], [ 3 ], [ 7 ] ],
46   'on_connect_do() worked'
47 );
48 eval { $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent'); };
49 ok $@, 'Searching for nonexistent table dies';
50
51 $schema->storage->disconnect();
52
53 my($connected, $disconnected, @cb_args);
54 ok $schema->connection(
55     DBICTest->_database,
56     {
57         on_connect_do       => sub { $connected = 1; @cb_args = @_; },
58         on_disconnect_do    => sub { $disconnected = 1 },
59     },
60 ), 'second connection()';
61 $schema->storage->dbh->do('SELECT 1');
62 ok $connected, 'on_connect_do() called after connect()';
63 ok ! $disconnected, 'on_disconnect_do() not called after connect()';
64 $schema->storage->disconnect();
65 ok $disconnected, 'on_disconnect_do() called after disconnect()';
66
67 isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook');
68
69 sub check_exists {
70     my $storage = shift;
71     ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
72     return;
73 }
74
75 sub check_dropped {
76     my $storage = shift;
77     eval { $storage->dbh->do('SELECT 1 FROM TEST_empty'); };
78     ok $@, 'Reading from dropped table fails';
79     return;
80 }
81
82 sub insert_from_subref {
83     my $storage = shift;
84     return [
85         [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ],
86         'INSERT INTO TEST_empty VALUES (7)',
87     ];
88 }