b4fd7892c8f363a70cea1c72916f0540f33aef65
[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 Data::Dumper;
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   local $Data::Dumper::Sortkeys = 1;
161   my $arg_dump = Dumper ($invocations->{$type}{args});
162
163   warnings_exist (
164     sub { $storage->connect_info ($invocations->{$type}{args}) },
165      $invocations->{$type}{warn} || [],
166     'Warned about ignored attributes',
167   );
168
169   is ($arg_dump, Dumper ($invocations->{$type}{args}), "$type didn't modify passed arguments");
170
171   is_deeply ($storage->_dbi_connect_info, $invocations->{$type}{dbi_connect_info}, "$type produced correct _dbi_connect_info");
172   ok ( (not $storage->auto_savepoint and not $storage->unsafe), "$type correctly ignored extra hashref");
173
174   is_deeply (
175     [$storage->on_connect_do, $storage->on_disconnect_do ],
176     [ [qw/a b c/], [qw/d e f/] ],
177     "$type correctly parsed DBIC specific on_[dis]connect_do",
178   );
179 }
180
181 # make sure connection-less storages do not throw on _determine_driver
182 # but work with ENV at the same time
183 SKIP: for my $env_dsn (undef, (DBICTest->_database)[0] ) {
184
185   skip 'This set of tests relies on being connected to SQLite', 1
186     if $env_dsn and $env_dsn !~ /\:SQLite\:/;
187
188   local $ENV{DBI_DSN} = $env_dsn || '';
189
190   my $s = DBICTest::Schema->connect();
191   is_deeply (
192     $s->storage->connect_info,
193     [],
194     'Starting with no explicitly passed in connect info'
195   . ($env_dsn ? ' (with DBI_DSN)' : ''),
196   );
197
198   my $sm = $s->storage->sql_maker;
199
200   ok (! $s->storage->connected, 'Storage does not appear connected after SQLMaker instance is taken');
201
202   if ($env_dsn) {
203     isa_ok($sm, 'DBIx::Class::SQLMaker');
204
205     ok ( $s->storage->_driver_determined, 'Driver determined (with DBI_DSN)');
206     isa_ok ( $s->storage, 'DBIx::Class::Storage::DBI::SQLite' );
207   }
208   else {
209     isa_ok($sm, 'DBIx::Class::SQLMaker');
210
211     ok (! $s->storage->_driver_determined, 'Driver undetermined');
212
213     throws_ok {
214       $s->storage->ensure_connected
215     } qr/You did not provide any connection_info/,
216     'sensible exception on empty conninfo connect';
217   }
218 }
219
220 done_testing;