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