File: 007_mock_attribute_aliases.t

package info (click to toggle)
libdbd-mock-perl 1.59-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 512 kB
  • sloc: perl: 1,251; makefile: 7
file content (185 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use 5.008;

use strict;
use warnings;

use Test::More;

BEGIN {
    use_ok('DBD::Mock');  
    use_ok('DBI');  
}

{ # aliasing is off
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:mysql', '', '');
    };
    ok(!$@, '... got our non-mock DB successfully');    
    isa_ok($dbh, 'DBI::db');

    ok(!defined($dbh->{mock_attribute_aliases}), '... nothing here');
    ok(!$dbh->{mock_database_name}, '... nothing here');    
}

# now turn it on
$DBD::Mock::AttributeAliasing++;

{ # but without a dbname it does nothing
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:', '', '');
    };
    ok(!$@, '... got our non-mock DB successfully');    
    isa_ok($dbh, 'DBI::db');

    ok(!defined($dbh->{mock_attribute_aliases}), '... nothing here');
    ok(!$dbh->{mock_database_name}, '... nothing here');    
}

# now test the error

eval {
    DBI->connect('dbi:Mock:Fail', '', '');
};
like($@, qr/Attribute aliases not available for \'Fail\'/, '... got the error we expected');      

# test the MySQL mock db
{
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:mysql', '', '');
    };
    ok(!$@, '... got our mock DB successfully');
    isa_ok($dbh, 'DBI::db');

    is($dbh->{mock_database_name}, 'mysql', '... and its the name we expected');        

    ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
    is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');   
    
    my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
    isa_ok($sth, 'DBI::st');
    
    $sth->execute();    
    
    is($dbh->{mysql_insertid}, 1, '... our alias works');
       
}

# and test it with the lowercasing
{
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:MySQL', '', '');
    };
    ok(!$@, '... got our mock DB successfully');
    isa_ok($dbh, 'DBI::db');

    is($dbh->{mock_database_name}, 'MySQL', '... and its the name we expected');        

    ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
    is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');   
    
    my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
    isa_ok($sth, 'DBI::st');
    
    $sth->execute();    
    
    is($dbh->{mysql_insertid}, 1, '... our alias works');
       
}

# test the MariaDB mock db
{
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:mariadb', '', '');
    };
    ok(!$@, '... got our mock DB successfully');
    isa_ok($dbh, 'DBI::db');

    is($dbh->{mock_database_name}, 'mariadb', '... and its the name we expected');        

    ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
    is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');   
    
    my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
    isa_ok($sth, 'DBI::st');
    
    $sth->execute();    
    
    is($dbh->{mariadb_insertid}, 1, '... our alias works');
       
}

# and test it with the lowercasing
{
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:MariaDB', '', '');
    };
    ok(!$@, '... got our mock DB successfully');
    isa_ok($dbh, 'DBI::db');

    is($dbh->{mock_database_name}, 'MariaDB', '... and its the name we expected');        

    ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
    is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');   
    
    my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
    isa_ok($sth, 'DBI::st');
    
    $sth->execute();    
    
    is($dbh->{mariadb_insertid}, 1, '... our alias works');

}

# test the MariaDB mock db
{
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:host=localhost;port=3306;database=mariadb', '', '');
    };
    ok(!$@, '... got our mock DB successfully');
    isa_ok($dbh, 'DBI::db');

    is($dbh->{mock_database_name}, 'mariadb', '... and its the name we expected');        

    ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
    is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');   
    
    my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
    isa_ok($sth, 'DBI::st');
    
    $sth->execute();    
    
    is($dbh->{mariadb_insertid}, 1, '... our alias works');
       
}

# and test it with the lowercasing
{
    my $dbh;
    eval {
        $dbh = DBI->connect('dbi:Mock:host=;database=MariaDB;port=', '', '');
    };
    ok(!$@, '... got our mock DB successfully');
    isa_ok($dbh, 'DBI::db');

    is($dbh->{mock_database_name}, 'MariaDB', '... and its the name we expected');        

    ok(defined($dbh->{mock_attribute_aliases}), '... got something here');
    is(ref($dbh->{mock_attribute_aliases}), 'HASH', '... and its the hash we expected');   
    
    my $sth = $dbh->prepare('INSERT INTO Foo (bar) VALUES(NULL)');
    isa_ok($sth, 'DBI::st');
    
    $sth->execute();    
    
    is($dbh->{mariadb_insertid}, 1, '... our alias works');

}

done_testing();