Lose yet another dep (Data::Dumper::Concise)
[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 $ENV{DBICTEST_VIA_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
60 # testing various invocations of connect_info ([ ... ])
61
62 my $coderef = sub { 42 };
63 my $invocations = {
64   'connect_info ([ $d, $u, $p, \%attr, \%extra_attr])' => {
65       args => [
66           'foo',
67           'bar',
68           undef,
69           {
70             on_connect_do => [qw/a b c/],
71             PrintError => 0,
72           },
73           {
74             AutoCommit => 1,
75             on_disconnect_do => [qw/d e f/],
76           },
77           {
78             unsafe => 1,
79             auto_savepoint => 1,
80           },
81         ],
82       dbi_connect_info => [
83           'foo',
84           'bar',
85           undef,
86           {
87             %{$storage->_default_dbi_connect_attributes || {} },
88             PrintError => 0,
89             AutoCommit => 1,
90           },
91       ],
92   },
93
94   'connect_info ([ \%code, \%extra_attr ])' => {
95       args => [
96           $coderef,
97           {
98             on_connect_do => [qw/a b c/],
99             PrintError => 0,
100             AutoCommit => 1,
101             on_disconnect_do => [qw/d e f/],
102           },
103           {
104             unsafe => 1,
105             auto_savepoint => 1,
106           },
107         ],
108       dbi_connect_info => [
109           $coderef,
110       ],
111   },
112
113   'connect_info ([ \%attr ])' => {
114       args => [
115           {
116             on_connect_do => [qw/a b c/],
117             PrintError => 1,
118             AutoCommit => 0,
119             on_disconnect_do => [qw/d e f/],
120             user => 'bar',
121             dsn => 'foo',
122           },
123           {
124             unsafe => 1,
125             auto_savepoint => 1,
126           },
127       ],
128       dbi_connect_info => [
129           'foo',
130           'bar',
131           undef,
132           {
133             %{$storage->_default_dbi_connect_attributes || {} },
134             PrintError => 1,
135             AutoCommit => 0,
136           },
137       ],
138       warn => qr/\QYou provided explicit AutoCommit => 0 in your connection_info/,
139   },
140   'connect_info ([ \%attr_with_coderef ])' => {
141       args => [ {
142         dbh_maker => $coderef,
143         dsn => 'blah',
144         user => 'bleh',
145         on_connect_do => [qw/a b c/],
146         on_disconnect_do => [qw/d e f/],
147       } ],
148       dbi_connect_info => [
149         $coderef
150       ],
151       warn => qr/Attribute\(s\) 'dsn', 'user' in connect_info were ignored/,
152   },
153 };
154
155 for my $type (keys %$invocations) {
156   local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK};
157
158   # we can not use a cloner portably because of the coderef
159   # so compare dumps instead
160   my $arg_dump = dump_value $invocations->{$type}{args};
161
162   warnings_exist (
163     sub { $storage->connect_info ($invocations->{$type}{args}) },
164      $invocations->{$type}{warn} || [],
165     'Warned about ignored attributes',
166   );
167
168   is (
169     $arg_dump,
170     dump_value $invocations->{$type}{args},
171     "$type didn't modify passed arguments",
172   );
173
174   is_deeply ($storage->_dbi_connect_info, $invocations->{$type}{dbi_connect_info}, "$type produced correct _dbi_connect_info");
175   ok ( (not $storage->auto_savepoint and not $storage->unsafe), "$type correctly ignored extra hashref");
176
177   is_deeply (
178     [$storage->on_connect_do, $storage->on_disconnect_do ],
179     [ [qw/a b c/], [qw/d e f/] ],
180     "$type correctly parsed DBIC specific on_[dis]connect_do",
181   );
182 }
183
184 # make sure connection-less storages do not throw on _determine_driver
185 # but work with ENV at the same time
186 SKIP: for my $env_dsn (undef, (DBICTest->_database)[0] ) {
187
188   skip 'This set of tests relies on being connected to SQLite', 1
189     if $env_dsn and $env_dsn !~ /\:SQLite\:/;
190
191   local $ENV{DBI_DSN} = $env_dsn || '';
192
193   my $s = DBICTest::Schema->connect();
194   is_deeply (
195     $s->storage->connect_info,
196     [],
197     'Starting with no explicitly passed in connect info'
198   . ($env_dsn ? ' (with DBI_DSN)' : ''),
199   );
200
201   my $sm = $s->storage->sql_maker;
202
203   ok (! $s->storage->connected, 'Storage does not appear connected after SQLMaker instance is taken');
204
205   if ($env_dsn) {
206     isa_ok($sm, 'DBIx::Class::SQLMaker');
207
208     ok ( $s->storage->_driver_determined, 'Driver determined (with DBI_DSN)');
209     isa_ok ( $s->storage, 'DBIx::Class::Storage::DBI::SQLite' );
210   }
211   else {
212     isa_ok($sm, 'DBIx::Class::SQLMaker');
213
214     ok (! $s->storage->_driver_determined, 'Driver undetermined');
215
216     throws_ok {
217       $s->storage->ensure_connected
218     } qr/You did not provide any connection_info/,
219     'sensible exception on empty conninfo connect';
220   }
221 }
222
223 done_testing;