Debian Codegraph @

t/033_table_info.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBI;
9
10 if ($DBI::VERSION < 1.635) {
11 plan skip_all => "Functionality requires DBI version 1.64 or higher";
12 }
13
14 my $dbh = DBI->connect( 'dbi:Mock:', '', '' );
15 isa_ok($dbh, 'DBI::db');
16
17 my $columns = [ 'TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS' ];
18 my $fooTable = [ undef, 'testSchema', 'foo', 'TABLE', undef ];
19 my $barView = [ undef, 'testSchema', 'bar', 'VIEW', undef ];
20 my $bazTable = [ undef, 'testSchema2', 'baz', 'TABLE', 'comment' ];
21
22 my $fooResult = {
23 TABLE_CAT => undef,
24 TABLE_SCHEM => 'testSchema',
25 TABLE_NAME => 'foo',
26 TABLE_TYPE => 'TABLE',
27 REMARKS => undef,
28 };
29
30 my $barResult = {
31 TABLE_CAT => undef,
32 TABLE_SCHEM => 'testSchema',
33 TABLE_NAME => 'bar',
34 TABLE_TYPE => 'VIEW',
35 REMARKS => undef,
36 };
37
38
39 my $sth = $dbh->table_info( undef, 'testSchema', 'foo', undef );
40
41 is_deeply( $sth->fetchall_arrayref( {} ), [], "No mocked table info should result an empty set of results being returned" );
42
43
44 $dbh->{mock_add_table_info} = {
45 cataloge => undef,
46 schema => 'testSchema',
47 table => 'foo',
48 type => undef,
49 table_info => [ $columns, $fooTable ],
50 };
51
52 $dbh->{mock_add_table_info} = {
53 cataloge => undef,
54 schema => 'testSchema',
55 table => undef,
56 type => 'VIEW',
57 table_info => [ $columns, $barView ],
58 };
59
60 $dbh->{mock_add_table_info} = {
61 cataloge => undef,
62 schema => 'testSchema',
63 table => undef,
64 type => undef,
65 table_info => [ $columns, $fooTable, $barView ],
66 };
67
68
69 $sth = $dbh->table_info( undef, 'testSchema', undef, 'VIEW' );
70
71 is_deeply( $sth->fetchall_arrayref( {} ), [ $barResult ], "The matching mock results should be returned" );
72
73 $sth = $dbh->table_info( undef, 'testSchema', undef, undef );
74
75 is_deeply( $sth->fetchall_arrayref( {} ), [ $fooResult, $barResult ], "Search based up on the schema parameter only, should return in the set of results we've already defined" );
76
77
78 $dbh->{mock_clear_table_info} = 1;
79
80 $sth = $dbh->table_info( undef, 'testSchema', 'foo', undef );
81
82 is_deeply( $sth->fetchall_arrayref( {} ), [], "Clearing the mocked table info should result in no results being returned until mock_add_table_info is used to populate the table_info again" );
83
84 $dbh->{mock_add_table_info} = {
85 schema => '%',
86 table_info => [
87 [ 'TABLE_CAT', 'TABLE_SCHEM', 'TABLE_NAME', 'TABLE_TYPE', 'REMARKS' ],
88 [ undef, 'testSchema', undef, undef, undef ],
89 [ undef, 'testSchema_2', undef, undef, undef ],
90 ],
91 };
92
93 $sth = $dbh->table_info( undef, '%', undef, undef );
94
95 is_deeply( $sth->fetchall_arrayref( {} ), [
96 {
97 TABLE_CAT => undef,
98 TABLE_SCHEM => 'testSchema',
99 TABLE_NAME => undef,
100 TABLE_TYPE => undef,
101 REMARKS => undef,
102 }, {
103 TABLE_CAT => undef,
104 TABLE_SCHEM => 'testSchema_2',
105 TABLE_NAME => undef,
106 TABLE_TYPE => undef,
107 REMARKS => undef,
108 }
109 ], "Mocking a search of schemas should return the records we've added" );
110
111
112
113 done_testing();