Fix two storage tests
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_call.t
1 use strict;
2 use warnings;
3 no warnings qw/once redefine/;
4
5 use lib qw(t/lib);
6 use DBICTest;
7
8 use Test::More tests => 9;
9
10 use DBIx::Class::Storage::DBI;
11 my $schema = DBICTest->init_schema(
12   no_connect  => 1,
13   no_deploy   => 1,
14 );
15
16 local *DBIx::Class::Storage::DBI::connect_call_foo = sub {
17   isa_ok $_[0], 'DBIx::Class::Storage::DBI',
18     'got storage in connect_call method';
19   is $_[1], 'bar', 'got param in connect_call method';
20 };
21
22 local *DBIx::Class::Storage::DBI::disconnect_call_foo = sub {
23   isa_ok $_[0], 'DBIx::Class::Storage::DBI',
24     'got storage in disconnect_call method';
25 };
26
27 ok $schema->connection(
28   DBICTest->_database,
29   {
30     on_connect_call => [
31         [ do_sql => 'create table test1 (id integer)' ],
32         [ do_sql => [ 'insert into test1 values (?)', {}, 1 ] ],
33         [ do_sql => sub { ['insert into test1 values (2)'] } ],
34         [ sub { $_[0]->dbh->do($_[1]) }, 'insert into test1 values (3)' ],
35         # this invokes $storage->connect_call_foo('bar') (above)
36         [ foo => 'bar' ],
37     ],
38     on_connect_do => 'insert into test1 values (4)',
39     on_disconnect_call => 'foo',
40   },
41 ), 'connection()';
42
43 is_deeply (
44   $schema->storage->dbh->selectall_arrayref('select * from test1'),
45   [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ],
46   'on_connect_call/do actions worked'
47 );
48
49 local *DBIx::Class::Storage::DBI::connect_call_foo = sub {
50   isa_ok $_[0], 'DBIx::Class::Storage::DBI',
51     'got storage in connect_call method';
52 };
53
54 local *DBIx::Class::Storage::DBI::connect_call_bar = sub {
55   isa_ok $_[0], 'DBIx::Class::Storage::DBI',
56     'got storage in connect_call method';
57 };
58
59 $schema->storage->disconnect;
60
61 ok $schema->connection(
62   DBICTest->_database,
63   {
64     # method list form
65     on_connect_call => [ 'foo', sub { ok 1, "coderef in list form" }, 'bar' ],
66   },
67 ), 'connection()';
68
69 $schema->storage->ensure_connected;