Harmonize DBICTEST_VIA_REPLICATED detection in tests
[dbsrgits/DBIx-Class.git] / t / storage / base.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8 use Test::Exception;
9
10 use DBICTest;
11 use DBIx::Class::_Util 'dump_value';
12
13 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
14
15 my $storage = $schema->storage;
16
17 is(
18   ref($storage),
19   'DBIx::Class::Storage::DBI::SQLite',
20   'Storage reblessed correctly into DBIx::Class::Storage::DBI::SQLite'
21 ) unless $storage->isa('DBIx::Class::Storage::DBI::Replicated');
22
23 throws_ok {
24     $schema->storage->throw_exception('test_exception_42');
25 } qr/\btest_exception_42\b/, 'basic exception';
26
27 throws_ok {
28     $schema->resultset('CD')->search_literal('broken +%$#$1')->all;
29 } qr/prepare_cached failed/, 'exception via DBI->HandleError, etc';
30
31
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
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           {
86             %{$storage->_default_dbi_connect_attributes || {} },
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/],
116             PrintError => 1,
117             AutoCommit => 0,
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           {
132             %{$storage->_default_dbi_connect_attributes || {} },
133             PrintError => 1,
134             AutoCommit => 0,
135           },
136       ],
137       warn => qr/\QYou provided explicit AutoCommit => 0 in your connection_info/,
138   },
139   'connect_info ([ \%attr_with_coderef ])' => {
140       args => [ {
141         dbh_maker => $coderef,
142         dsn => 'blah',
143         user => 'bleh',
144         on_connect_do => [qw/a b c/],
145         on_disconnect_do => [qw/d e f/],
146       } ],
147       dbi_connect_info => [
148         $coderef
149       ],
150       warn => qr/Attribute\(s\) 'dsn', 'user' in connect_info were ignored/,
151   },
152 };
153
154 for my $type (keys %$invocations) {
155   local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK};
156
157   # we can not use a cloner portably because of the coderef
158   # so compare dumps instead
159   my $arg_dump = dump_value $invocations->{$type}{args};
160
161   warnings_exist (
162     sub { $storage->connect_info ($invocations->{$type}{args}) },
163      $invocations->{$type}{warn} || [],
164     'Warned about ignored attributes',
165   );
166
167   is (
168     $arg_dump,
169     dump_value $invocations->{$type}{args},
170     "$type didn't modify passed arguments",
171   );
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 }
182
183 # make sure connection-less storages do not throw on _determine_driver
184 # but work with ENV at the same time
185 SKIP: for my $env_dsn (undef, (DBICTest->_database)[0] ) {
186
187   skip 'This set of tests relies on being connected to SQLite', 1
188     if $env_dsn and $env_dsn !~ /\:SQLite\:/;
189
190   local $ENV{DBI_DSN} = $env_dsn || '';
191
192   my $s = DBICTest::Schema->connect();
193   is_deeply (
194     $s->storage->connect_info,
195     [],
196     'Starting with no explicitly passed in connect info'
197   . ($env_dsn ? ' (with DBI_DSN)' : ''),
198   );
199
200   my $sm = $s->storage->sql_maker;
201
202   ok (! $s->storage->connected, 'Storage does not appear connected after SQLMaker instance is taken');
203
204   if ($env_dsn) {
205     isa_ok($sm, 'DBIx::Class::SQLMaker');
206
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');
212
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   }
220 }
221
222 done_testing;