Debian Codegraph @

t/007_mock_attribute_aliases.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9 use_ok('DBD::Mock');
10 use_ok('DBI');
11 }
12
13 { # aliasing is off
14 my $dbh;
15 eval {
16 $dbh = DBI->connect('dbi:Mock:mysql', '', '');
17 };
18 ok(!$@, '... got our non-mock DB successfully');
19 isa_ok($dbh, 'DBI::db');
20
21 ok(!defined($dbh->{mock_attribute_aliases}), '... nothing here');
22 ok(!$dbh->{mock_database_name}, '... nothing here');
23 }
24
25 # now turn it on
26 $DBD::Mock::AttributeAliasing++;
27
28 { # but without a dbname it does nothing
29 my $dbh;
30 eval {
31 $dbh = DBI->connect('dbi:Mock:', '', '');
32 };
33 ok(!$@, '... got our non-mock DB successfully');
34 isa_ok($dbh, 'DBI::db');
35
36 ok(!defined($dbh->{mock_attribute_aliases}), '... nothing here');
37 ok(!$dbh->{mock_database_name}, '... nothing here');
38 }
39
40 # now test the error
41
42 eval {
43 DBI->connect('dbi:Mock:Fail', '', '');
44 };
45 like($@, qr/Attribute aliases not available for \'Fail\'/, '... got the error we expected');
46
47 # test the MySQL mock db
48 {
49 my $dbh;
50 eval {
51 $dbh = DBI->connect('dbi:Mock:mysql', '', '');
52 };
53 ok(!$@, '... got our mock DB successfully');
54 isa_ok($dbh, 'DBI::db');
55
56 is($dbh->{mock_database_name}, 'mysql', '... and its the name we expected');
57
58 ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
59 is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
60
61 my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
62 isa_ok($sth, 'DBI::st');
63
64 $sth->execute();
65
66 is($dbh->{mysql_insertid}, 1, '... our alias works');
67
68 }
69
70 # and test it with the lowercasing
71 {
72 my $dbh;
73 eval {
74 $dbh = DBI->connect('dbi:Mock:MySQL', '', '');
75 };
76 ok(!$@, '... got our mock DB successfully');
77 isa_ok($dbh, 'DBI::db');
78
79 is($dbh->{mock_database_name}, 'MySQL', '... and its the name we expected');
80
81 ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
82 is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
83
84 my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
85 isa_ok($sth, 'DBI::st');
86
87 $sth->execute();
88
89 is($dbh->{mysql_insertid}, 1, '... our alias works');
90
91 }
92
93 # test the MariaDB mock db
94 {
95 my $dbh;
96 eval {
97 $dbh = DBI->connect('dbi:Mock:mariadb', '', '');
98 };
99 ok(!$@, '... got our mock DB successfully');
100 isa_ok($dbh, 'DBI::db');
101
102 is($dbh->{mock_database_name}, 'mariadb', '... and its the name we expected');
103
104 ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
105 is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
106
107 my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
108 isa_ok($sth, 'DBI::st');
109
110 $sth->execute();
111
112 is($dbh->{mariadb_insertid}, 1, '... our alias works');
113
114 }
115
116 # and test it with the lowercasing
117 {
118 my $dbh;
119 eval {
120 $dbh = DBI->connect('dbi:Mock:MariaDB', '', '');
121 };
122 ok(!$@, '... got our mock DB successfully');
123 isa_ok($dbh, 'DBI::db');
124
125 is($dbh->{mock_database_name}, 'MariaDB', '... and its the name we expected');
126
127 ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
128 is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
129
130 my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
131 isa_ok($sth, 'DBI::st');
132
133 $sth->execute();
134
135 is($dbh->{mariadb_insertid}, 1, '... our alias works');
136
137 }
138
139 # test the MariaDB mock db
140 {
141 my $dbh;
142 eval {
143 $dbh = DBI->connect('dbi:Mock:host=localhost;port=3306;database=mariadb', '', '');
144 };
145 ok(!$@, '... got our mock DB successfully');
146 isa_ok($dbh, 'DBI::db');
147
148 is($dbh->{mock_database_name}, 'mariadb', '... and its the name we expected');
149
150 ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
151 is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
152
153 my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
154 isa_ok($sth, 'DBI::st');
155
156 $sth->execute();
157
158 is($dbh->{mariadb_insertid}, 1, '... our alias works');
159
160 }
161
162 # and test it with the lowercasing
163 {
164 my $dbh;
165 eval {
166 $dbh = DBI->connect('dbi:Mock:host=;database=MariaDB;port=', '', '');
167 };
168 ok(!$@, '... got our mock DB successfully');
169 isa_ok($dbh, 'DBI::db');
170
171 is($dbh->{mock_database_name}, 'MariaDB', '... and its the name we expected');
172
173 ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
174 is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');
175
176 my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
177 isa_ok($sth, 'DBI::st');
178
179 $sth->execute();
180
181 is($dbh->{mariadb_insertid}, 1, '... our alias works');
182
183 }
184
185 done_testing();