/
02throws.t
163 lines (126 loc) · 4.35 KB
/
02throws.t
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
#!/usr/bin/perl -w
use strict;
# check if we can run Test::Exception
BEGIN
{
eval { require Test::Exception; Test::Exception->import };
if ($@)
{
print "1..0 # Skipped: no Test::Exception\n";
exit;
}
}
use Test::More tests => 17;
use Test::Exception;
use Test::DatabaseRow;
# check we get the correct error message
throws_ok { row_ok }
qr/Needed fetch results but no 'dbh' defined/, "no dbh";
# define a new default database
$Test::DatabaseRow::dbh = FakeDBI->new;
# no table test
throws_ok { row_ok }
qr/Needed to build SQL but no 'table' defined/, "no table";
# no where test
throws_ok { row_ok( table => "foo" ) }
qr/Needed to build SQL but no 'where' defined/, "no where";
# bad where tests
throws_ok { row_ok( table => "foo",
where => \"wibble" ) }
qr/Can't understand the argument passed in 'where'/, "bad where";
throws_ok { row_ok( table => "foo",
where => { foo => [ this => "wrong" ] } ) }
qr/Can't understand the argument passed in 'where'/, "bad where 2";
# no tests - this is okay now
#throws_ok { row_ok( table => "foo",
# where => [ fooid => 123 ] ) }
# qr/No 'tests' passed as an arguement/, "no tests";
# odd tests
throws_ok { row_ok( table => "foo",
where => [ fooid => 123 ] ,
tests => \"fish" ) }
qr/Can't understand the argument passed in 'tests': not a hashref or arrayref/, "bad tests";
# odd tests
throws_ok { row_ok( table => "foo",
where => [ fooid => 123 ] ,
tests => { foo => [ bar => "baz" ] } );
}
qr/Can't understand the argument passed in 'tests': key 'foo' didn't contain a hashref/, "bad tests 2";
throws_ok { row_ok( table => "foo",
where => [ fooid => 123 ] ,
tests => [ notpresent => 1 ] )
}
qr/No column 'notpresent' returned from table 'foo'/, "no col from build";
throws_ok { row_ok( sql => "some sql",
tests => [ notpresent => 1 ] )
}
qr/No column 'notpresent' returned from sql/, "no col from sql";
dies_ok { row_ok( dbh => FakeDBI->new(fallover => 1, "hello" => "there"),
sql => "any old gumph",
tests => [ fooid => 1 ]) } "handles problems with sql";
throws_ok { row_ok( db_results => [ { foo => "bar" } ],
tests => { invalidop => { foo => 'bar' } } ) }
qr/Invalid operator test 'invalidop': \S+/, "invalid operator";
########################################################################
# bad SQL
throws_ok { row_ok( sql => \[] ) }
qr/Can't understand the sql/;
########################################################################
# bad storage
throws_ok { row_ok( sql => "some sql", store_rows => {}) }
qr/Must pass an arrayref in 'store_rows'/, "no col from sql";
throws_ok { row_ok( sql => "some sql", store_row => \"foo") }
qr/Invalid argument passed in 'store_row'/, "no col from sql";
########################################################################
# Test::DatabaseRow::Object
{
my $tdr = Test::DatabaseRow::Object->new(where => [ foo => "bar" ], table => "buzz" );
throws_ok { $tdr->sql_and_bind }
qr/Needed to quote SQL during SQL building but no 'dbh' defined/, "quote but no dbh"
}
{
throws_ok {
my $tdr = Test::DatabaseRow::Object->new( tests => [qw( a b c)] );
} qr/Can't understand the passed test arguments/, "odd tests"
}
########################################################################
# Test::DatabaseRow::Result
throws_ok { Test::DatabaseRow::Result->new( diag => {} ) }
qr/Invalid argument to diag/, "diag invalid";
########################################################################
# fake database package
package FakeDBI;
use Data::Dumper;
sub new
{
my $class = shift;
return bless { @_ }, $class
}
sub quote { return "qtd<$_[1]>" };
sub prepare
{
my $this = shift;
# die if we need to
if ($this->fallover)
{ die "Khaaaaaaaaaaaaan!" }
return FakeSTH->new($this);
}
sub nomatch { return $_[0]->{nomatch} }
sub fallover { return $_[0]->{fallover} }
package FakeSTH;
sub new { return bless { parent => $_[1] }, $_[0] };
sub execute { return 1 };
sub fetchrow_hashref
{
my $this = shift;
my $parent = $this->{parent};
# return undef after the first call)
if ($this->{called})
{ return }
else
{ $this->{called} = 1 }
return
($parent->nomatch)
? undef
: { fooid => 123, name => "fred" }
}