Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
5fc1107f |
3 | use strict; |
4 | use warnings; |
5 | no warnings qw/once redefine/; |
6 | |
c0329273 |
7 | |
f602cd7b |
8 | use DBI; |
9900b569 |
9 | use DBICTest; |
f602cd7b |
10 | use DBICTest::Schema; |
11 | use DBIx::Class::Storage::DBI; |
5fc1107f |
12 | |
f602cd7b |
13 | # !!! do not replace this with done_testing - tests reside in the callbacks |
14 | # !!! number of calls is important |
6c925c72 |
15 | use Test::More tests => 17; |
f602cd7b |
16 | # !!! |
6c925c72 |
17 | use Test::Warn; |
5fc1107f |
18 | |
f602cd7b |
19 | my $schema = DBICTest::Schema->clone; |
20 | |
21 | { |
22 | *DBIx::Class::Storage::DBI::connect_call_foo = sub { |
23 | isa_ok $_[0], 'DBIx::Class::Storage::DBI', |
24 | 'got storage in connect_call method'; |
25 | is $_[1], 'bar', 'got param in connect_call method'; |
26 | }; |
27 | |
28 | *DBIx::Class::Storage::DBI::disconnect_call_foo = sub { |
29 | isa_ok $_[0], 'DBIx::Class::Storage::DBI', |
30 | 'got storage in disconnect_call method'; |
31 | }; |
5fc1107f |
32 | |
f602cd7b |
33 | ok $schema->connection( |
34 | DBICTest->_database, |
35 | { |
36 | on_connect_call => [ |
37 | [ do_sql => 'create table test1 (id integer)' ], |
38 | [ do_sql => [ 'insert into test1 values (?)', {}, 1 ] ], |
39 | [ do_sql => sub { ['insert into test1 values (2)'] } ], |
40 | [ sub { $_[0]->dbh->do($_[1]) }, 'insert into test1 values (3)' ], |
41 | # this invokes $storage->connect_call_foo('bar') (above) |
42 | [ foo => 'bar' ], |
43 | ], |
44 | on_connect_do => 'insert into test1 values (4)', |
45 | on_disconnect_call => 'foo', |
46 | }, |
47 | ), 'connection()'; |
5fc1107f |
48 | |
f602cd7b |
49 | ok (! $schema->storage->connected, 'start disconnected'); |
5fc1107f |
50 | |
f602cd7b |
51 | is_deeply ( |
52 | $schema->storage->dbh->selectall_arrayref('select * from test1'), |
53 | [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ], |
54 | 'on_connect_call/do actions worked' |
55 | ); |
5fc1107f |
56 | |
f602cd7b |
57 | $schema->storage->disconnect; |
58 | } |
5fc1107f |
59 | |
f602cd7b |
60 | { |
61 | *DBIx::Class::Storage::DBI::connect_call_foo = sub { |
62 | isa_ok $_[0], 'DBIx::Class::Storage::DBI', |
63 | 'got storage in connect_call method'; |
64 | }; |
5fc1107f |
65 | |
f602cd7b |
66 | *DBIx::Class::Storage::DBI::connect_call_bar = sub { |
67 | isa_ok $_[0], 'DBIx::Class::Storage::DBI', |
68 | 'got storage in connect_call method'; |
69 | }; |
5fc1107f |
70 | |
5fc1107f |
71 | |
f602cd7b |
72 | ok $schema->connection( |
73 | DBICTest->_database, |
74 | { |
75 | # method list form |
76 | on_connect_call => [ 'foo', sub { ok 1, "coderef in list form" }, 'bar' ], |
77 | }, |
78 | ), 'connection()'; |
5fc1107f |
79 | |
f602cd7b |
80 | ok (! $schema->storage->connected, 'start disconnected'); |
81 | $schema->storage->ensure_connected; |
82 | $schema->storage->disconnect; # this should not fire any tests |
83 | } |
46bb50b7 |
84 | |
85 | { |
86 | ok $schema->connection( |
6c925c72 |
87 | sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 } ) }, |
46bb50b7 |
88 | { |
89 | # method list form |
90 | on_connect_call => [ sub { ok 1, "on_connect_call after DT parser" }, ], |
91 | on_disconnect_call => [ sub { ok 1, "on_disconnect_call after DT parser" }, ], |
92 | }, |
93 | ), 'connection()'; |
94 | |
95 | ok (! $schema->storage->connected, 'start disconnected'); |
46bb50b7 |
96 | |
6c925c72 |
97 | # this should connect due to the coderef, and also warn due to the false autocommit above |
98 | warnings_exist { |
99 | $schema->storage->_determine_driver |
100 | } qr/The 'RaiseError' of the externally supplied DBI handle is set to false/, 'Warning on clobbered AutoCommit => 0 fired'; |
746c315f |
101 | |
102 | ok ($schema->storage->connected, 'determine driver connects'); |
46bb50b7 |
103 | $schema->storage->disconnect; |
104 | } |