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