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