Debian Codegraph @

t/002_dr_handle.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 my $drh = DBI->install_driver("Mock");
14 isa_ok($drh, 'DBI::dr');
15
16 is($drh->{Name}, 'Mock', '... got the right name');
17 is($drh->{Version}, $DBD::Mock::VERSION, '... got the right version');
18 is($drh->{Attribution},
19 'DBD Mock driver by Chris Winters & Stevan Little (orig. from Tim Bunce)',
20 '... got the right attribution');
21
22 # make sure we always get the same one back
23 {
24 my $drh2 = DBI->install_driver("Mock");
25 isa_ok($drh2, 'DBI::dr');
26
27 is($drh, $drh2, '... got the same driver');
28 }
29
30 is_deeply(
31 [ $drh->data_sources() ],
32 [ 'DBI:Mock:' ],
33 '... got the expected data sources');
34
35 $drh->{mock_data_sources} = [ 'test', 'DBI:Mock:mysql' ];
36
37 is_deeply(
38 [ $drh->data_sources() ],
39 [ 'DBI:Mock:test', 'DBI:Mock:mysql' ],
40 '... got the expected data sources');
41
42 $drh->{mock_add_data_sources} = 'foo';
43
44 is_deeply(
45 [ $drh->data_sources() ],
46 [ 'DBI:Mock:test', 'DBI:Mock:mysql', 'DBI:Mock:foo' ],
47 '... got the expected data sources');
48
49 { # connect through the driver handle
50 my $dbh = $drh->connect();
51 isa_ok($dbh, 'DBI::db');
52
53 is($dbh->{Driver}, $drh, '... our driver is as we expect');
54
55 $dbh->disconnect();
56 }
57
58 { # check the mock_connect_fail attribute
59 is($drh->{mock_connect_fail}, 0, '... the default is set not to fail');
60
61 # make sure the this only affects the initial connect
62 my $_dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
63 isa_ok($_dbh, 'DBI::db');
64
65 # now no more connections
66 $drh->{mock_connect_fail} = 1;
67 is($drh->{mock_connect_fail}, 1, '... we are set to fail');
68
69 eval {
70 DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
71 };
72 ok($@, '... could not connect (as expected)');
73 like($@,
74 qr/^DBI connect\(\'\'\,\'\'\,\.\.\.\) failed\: Could not connect to mock database/, #'
75 '... got the error we expected too');
76
77 # make sure the handle we created before the change works
78 eval { $_dbh->prepare( "SELECT foo FROM bar" ) };
79 ok(!$@, '... we should not have an exception here');
80
81 $drh->{mock_connect_fail} = 0;
82 is($drh->{'mock_connect_fail'}, 0, '... we are set not to fail');
83
84 my $dbh;
85 eval {
86 $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
87 };
88 ok(!$@, '... could connect (as expected)');
89 isa_ok($dbh, 'DBI::db');
90 }
91
92 { # check other attributes
93 $drh->{mock_nothing} = 100;
94 ok(!defined($drh->{mock_nothing}), '... we only support our attributes');
95
96 $drh->{nothing} = 100;
97 ok(!defined($drh->{nothing}), '... we only support our attributes');
98 }
99
100 done_testing();