Lose yet another dep (Data::Dumper::Concise)
[dbsrgits/DBIx-Class.git] / t / storage / base.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
efe6365b 3use strict;
6b5e61af 4use warnings;
efe6365b 5
6use Test::More;
6b5e61af 7use Test::Warn;
f3d405dc 8use Test::Exception;
c0329273 9
efe6365b 10use DBICTest;
8fc4291e 11use DBIx::Class::_Util 'dump_value';
efe6365b 12
fcf741b1 13my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
efe6365b 14
baa31d2f 15my $storage = $schema->storage;
8b60b921 16
17is(
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};
baa31d2f 22
f3d405dc 23throws_ok {
19fb8520 24 $schema->storage->throw_exception('test_exception_42');
f3d405dc 25} qr/\btest_exception_42\b/, 'basic exception';
19fb8520 26
f3d405dc 27throws_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
59
92fe2181 60# testing various invocations of connect_info ([ ... ])
61
62my $coderef = sub { 42 };
63my $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 {
3a4c1d89 87 %{$storage->_default_dbi_connect_attributes || {} },
92fe2181 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/],
3a4c1d89 117 PrintError => 1,
118 AutoCommit => 0,
92fe2181 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 {
3a4c1d89 133 %{$storage->_default_dbi_connect_attributes || {} },
134 PrintError => 1,
135 AutoCommit => 0,
92fe2181 136 },
137 ],
6c925c72 138 warn => qr/\QYou provided explicit AutoCommit => 0 in your connection_info/,
92fe2181 139 },
8fd069b9 140 'connect_info ([ \%attr_with_coderef ])' => {
141 args => [ {
142 dbh_maker => $coderef,
6b5e61af 143 dsn => 'blah',
144 user => 'bleh',
8fd069b9 145 on_connect_do => [qw/a b c/],
146 on_disconnect_do => [qw/d e f/],
147 } ],
148 dbi_connect_info => [
149 $coderef
150 ],
6b5e61af 151 warn => qr/Attribute\(s\) 'dsn', 'user' in connect_info were ignored/,
8fd069b9 152 },
92fe2181 153};
154
155for my $type (keys %$invocations) {
eed5492f 156 local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK};
92fe2181 157
158 # we can not use a cloner portably because of the coderef
159 # so compare dumps instead
8fc4291e 160 my $arg_dump = dump_value $invocations->{$type}{args};
92fe2181 161
6b5e61af 162 warnings_exist (
163 sub { $storage->connect_info ($invocations->{$type}{args}) },
eed5492f 164 $invocations->{$type}{warn} || [],
6b5e61af 165 'Warned about ignored attributes',
166 );
92fe2181 167
8fc4291e 168 is (
169 $arg_dump,
170 dump_value $invocations->{$type}{args},
171 "$type didn't modify passed arguments",
172 );
92fe2181 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}
baa31d2f 183
d87929a4 184# make sure connection-less storages do not throw on _determine_driver
37b5ab51 185# but work with ENV at the same time
186SKIP: for my $env_dsn (undef, (DBICTest->_database)[0] ) {
3cff955a 187
188 skip 'This set of tests relies on being connected to SQLite', 1
37b5ab51 189 if $env_dsn and $env_dsn !~ /\:SQLite\:/;
444f799b 190
879ff8cb 191 local $ENV{DBI_DSN} = $env_dsn || '';
37b5ab51 192
193 my $s = DBICTest::Schema->connect();
d87929a4 194 is_deeply (
195 $s->storage->connect_info,
196 [],
37b5ab51 197 'Starting with no explicitly passed in connect info'
198 . ($env_dsn ? ' (with DBI_DSN)' : ''),
d87929a4 199 );
200
37b5ab51 201 my $sm = $s->storage->sql_maker;
202
879ff8cb 203 ok (! $s->storage->connected, 'Storage does not appear connected after SQLMaker instance is taken');
d87929a4 204
37b5ab51 205 if ($env_dsn) {
206 isa_ok($sm, 'DBIx::Class::SQLMaker');
d87929a4 207
37b5ab51 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');
d87929a4 213
37b5ab51 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 }
d87929a4 221}
222
6b5e61af 223done_testing;