Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
efe6365b |
3 | use strict; |
6b5e61af |
4 | use warnings; |
efe6365b |
5 | |
6 | use Test::More; |
6b5e61af |
7 | use Test::Warn; |
f3d405dc |
8 | use Test::Exception; |
c0329273 |
9 | |
efe6365b |
10 | use DBICTest; |
8fc4291e |
11 | use DBIx::Class::_Util 'dump_value'; |
efe6365b |
12 | |
fcf741b1 |
13 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); |
efe6365b |
14 | |
baa31d2f |
15 | my $storage = $schema->storage; |
8b60b921 |
16 | |
17 | is( |
18 | ref($storage), |
19 | 'DBIx::Class::Storage::DBI::SQLite', |
20 | 'Storage reblessed correctly into DBIx::Class::Storage::DBI::SQLite' |
de0edd06 |
21 | ) unless $storage->isa('DBIx::Class::Storage::DBI::Replicated'); |
baa31d2f |
22 | |
f3d405dc |
23 | throws_ok { |
19fb8520 |
24 | $schema->storage->throw_exception('test_exception_42'); |
f3d405dc |
25 | } qr/\btest_exception_42\b/, 'basic exception'; |
19fb8520 |
26 | |
f3d405dc |
27 | throws_ok { |
19fb8520 |
28 | $schema->resultset('CD')->search_literal('broken +%$#$1')->all; |
f3d405dc |
29 | } qr/prepare_cached failed/, 'exception via DBI->HandleError, etc'; |
19fb8520 |
30 | |
9a0891be |
31 | |
5c505caf |
32 | # make sure repeated disconnection works |
33 | { |
34 | my $fn = DBICTest->_sqlite_dbfilename; |
35 | |
36 | lives_ok { |
37 | $schema->storage->ensure_connected; |
38 | my $dbh = $schema->storage->dbh; |
39 | $schema->storage->disconnect for 1,2; |
40 | unlink $fn; |
41 | $dbh->disconnect; |
42 | }; |
43 | |
44 | lives_ok { |
45 | $schema->storage->ensure_connected; |
46 | $schema->storage->disconnect for 1,2; |
47 | unlink $fn; |
48 | $schema->storage->disconnect for 1,2; |
49 | }; |
50 | |
51 | lives_ok { |
52 | $schema->storage->ensure_connected; |
53 | $schema->storage->_dbh->disconnect; |
54 | unlink $fn; |
55 | $schema->storage->disconnect for 1,2; |
56 | }; |
57 | } |
58 | |
92fe2181 |
59 | # testing various invocations of connect_info ([ ... ]) |
60 | |
61 | my $coderef = sub { 42 }; |
62 | my $invocations = { |
63 | 'connect_info ([ $d, $u, $p, \%attr, \%extra_attr])' => { |
64 | args => [ |
65 | 'foo', |
66 | 'bar', |
67 | undef, |
68 | { |
69 | on_connect_do => [qw/a b c/], |
70 | PrintError => 0, |
71 | }, |
72 | { |
73 | AutoCommit => 1, |
74 | on_disconnect_do => [qw/d e f/], |
75 | }, |
76 | { |
77 | unsafe => 1, |
78 | auto_savepoint => 1, |
79 | }, |
80 | ], |
81 | dbi_connect_info => [ |
82 | 'foo', |
83 | 'bar', |
84 | undef, |
85 | { |
3a4c1d89 |
86 | %{$storage->_default_dbi_connect_attributes || {} }, |
92fe2181 |
87 | PrintError => 0, |
88 | AutoCommit => 1, |
89 | }, |
90 | ], |
91 | }, |
92 | |
93 | 'connect_info ([ \%code, \%extra_attr ])' => { |
94 | args => [ |
95 | $coderef, |
96 | { |
97 | on_connect_do => [qw/a b c/], |
98 | PrintError => 0, |
99 | AutoCommit => 1, |
100 | on_disconnect_do => [qw/d e f/], |
101 | }, |
102 | { |
103 | unsafe => 1, |
104 | auto_savepoint => 1, |
105 | }, |
106 | ], |
107 | dbi_connect_info => [ |
108 | $coderef, |
109 | ], |
110 | }, |
111 | |
112 | 'connect_info ([ \%attr ])' => { |
113 | args => [ |
114 | { |
115 | on_connect_do => [qw/a b c/], |
3a4c1d89 |
116 | PrintError => 1, |
117 | AutoCommit => 0, |
92fe2181 |
118 | on_disconnect_do => [qw/d e f/], |
119 | user => 'bar', |
120 | dsn => 'foo', |
121 | }, |
122 | { |
123 | unsafe => 1, |
124 | auto_savepoint => 1, |
125 | }, |
126 | ], |
127 | dbi_connect_info => [ |
128 | 'foo', |
129 | 'bar', |
130 | undef, |
131 | { |
3a4c1d89 |
132 | %{$storage->_default_dbi_connect_attributes || {} }, |
133 | PrintError => 1, |
134 | AutoCommit => 0, |
92fe2181 |
135 | }, |
136 | ], |
6c925c72 |
137 | warn => qr/\QYou provided explicit AutoCommit => 0 in your connection_info/, |
92fe2181 |
138 | }, |
8fd069b9 |
139 | 'connect_info ([ \%attr_with_coderef ])' => { |
140 | args => [ { |
141 | dbh_maker => $coderef, |
6b5e61af |
142 | dsn => 'blah', |
143 | user => 'bleh', |
8fd069b9 |
144 | on_connect_do => [qw/a b c/], |
145 | on_disconnect_do => [qw/d e f/], |
146 | } ], |
147 | dbi_connect_info => [ |
148 | $coderef |
149 | ], |
6b5e61af |
150 | warn => qr/Attribute\(s\) 'dsn', 'user' in connect_info were ignored/, |
8fd069b9 |
151 | }, |
92fe2181 |
152 | }; |
153 | |
154 | for my $type (keys %$invocations) { |
eed5492f |
155 | local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK}; |
92fe2181 |
156 | |
157 | # we can not use a cloner portably because of the coderef |
158 | # so compare dumps instead |
8fc4291e |
159 | my $arg_dump = dump_value $invocations->{$type}{args}; |
92fe2181 |
160 | |
6b5e61af |
161 | warnings_exist ( |
162 | sub { $storage->connect_info ($invocations->{$type}{args}) }, |
eed5492f |
163 | $invocations->{$type}{warn} || [], |
6b5e61af |
164 | 'Warned about ignored attributes', |
165 | ); |
92fe2181 |
166 | |
8fc4291e |
167 | is ( |
168 | $arg_dump, |
169 | dump_value $invocations->{$type}{args}, |
170 | "$type didn't modify passed arguments", |
171 | ); |
92fe2181 |
172 | |
173 | is_deeply ($storage->_dbi_connect_info, $invocations->{$type}{dbi_connect_info}, "$type produced correct _dbi_connect_info"); |
174 | ok ( (not $storage->auto_savepoint and not $storage->unsafe), "$type correctly ignored extra hashref"); |
175 | |
176 | is_deeply ( |
177 | [$storage->on_connect_do, $storage->on_disconnect_do ], |
178 | [ [qw/a b c/], [qw/d e f/] ], |
179 | "$type correctly parsed DBIC specific on_[dis]connect_do", |
180 | ); |
181 | } |
baa31d2f |
182 | |
d87929a4 |
183 | # make sure connection-less storages do not throw on _determine_driver |
37b5ab51 |
184 | # but work with ENV at the same time |
185 | SKIP: for my $env_dsn (undef, (DBICTest->_database)[0] ) { |
3cff955a |
186 | |
187 | skip 'This set of tests relies on being connected to SQLite', 1 |
37b5ab51 |
188 | if $env_dsn and $env_dsn !~ /\:SQLite\:/; |
444f799b |
189 | |
879ff8cb |
190 | local $ENV{DBI_DSN} = $env_dsn || ''; |
37b5ab51 |
191 | |
192 | my $s = DBICTest::Schema->connect(); |
d87929a4 |
193 | is_deeply ( |
194 | $s->storage->connect_info, |
195 | [], |
37b5ab51 |
196 | 'Starting with no explicitly passed in connect info' |
197 | . ($env_dsn ? ' (with DBI_DSN)' : ''), |
d87929a4 |
198 | ); |
199 | |
37b5ab51 |
200 | my $sm = $s->storage->sql_maker; |
201 | |
879ff8cb |
202 | ok (! $s->storage->connected, 'Storage does not appear connected after SQLMaker instance is taken'); |
d87929a4 |
203 | |
37b5ab51 |
204 | if ($env_dsn) { |
205 | isa_ok($sm, 'DBIx::Class::SQLMaker'); |
d87929a4 |
206 | |
37b5ab51 |
207 | ok ( $s->storage->_driver_determined, 'Driver determined (with DBI_DSN)'); |
208 | isa_ok ( $s->storage, 'DBIx::Class::Storage::DBI::SQLite' ); |
209 | } |
210 | else { |
211 | isa_ok($sm, 'DBIx::Class::SQLMaker'); |
d87929a4 |
212 | |
37b5ab51 |
213 | ok (! $s->storage->_driver_determined, 'Driver undetermined'); |
214 | |
215 | throws_ok { |
216 | $s->storage->ensure_connected |
217 | } qr/You did not provide any connection_info/, |
218 | 'sensible exception on empty conninfo connect'; |
219 | } |
d87929a4 |
220 | } |
221 | |
6b5e61af |
222 | done_testing; |