Refactor sth preparation/binding - no functional changes
[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
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) {
124
125 # we can not use a cloner portably because of the coderef
126 # so compare dumps instead
127 local $Data::Dumper::Sortkeys = 1;
128 my $arg_dump = Dumper ($invocations->{$type}{args});
129
6b5e61af 130 warnings_exist (
131 sub { $storage->connect_info ($invocations->{$type}{args}) },
132 $invocations->{$type}{warn} || (),
133 'Warned about ignored attributes',
134 );
92fe2181 135
6b5e61af 136 is ($arg_dump, Dumper ($invocations->{$type}{args}), "$type didn't modify passed arguments");
92fe2181 137
138 is_deeply ($storage->_dbi_connect_info, $invocations->{$type}{dbi_connect_info}, "$type produced correct _dbi_connect_info");
139 ok ( (not $storage->auto_savepoint and not $storage->unsafe), "$type correctly ignored extra hashref");
140
141 is_deeply (
142 [$storage->on_connect_do, $storage->on_disconnect_do ],
143 [ [qw/a b c/], [qw/d e f/] ],
144 "$type correctly parsed DBIC specific on_[dis]connect_do",
145 );
146}
baa31d2f 147
6b5e61af 148done_testing;
149
efe6365b 1501;