/
01basic.t
166 lines (132 loc) · 4.04 KB
/
01basic.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
164
165
166
#!/usr/bin/perl
########################################################################
# this test checks to see if the handling of sql_and_bind works, and if
# sql_and_bind is automatically created from table and where if needed
########################################################################
use strict;
use warnings;
use Test::More tests => 12;
########################################################################
# load the module / setup
########################################################################
BEGIN { use_ok "Test::DatabaseRow::Object" }
# create a fake dbh connection. The quote function in this class
# just marks the text up with "qtd<text>" so we can see what would
# have been really quoted if it was a real dbh connection
my $dbh = FakeDBI->new();
########################################################################
# coercian
########################################################################
{
my $tbr = Test::DatabaseRow::Object->new(
dbh => $dbh,
sql_and_bind => q{SELECT * FROM foo WHERE fooid = 123},
);
is($tbr->sql_and_bind->[0],
q{SELECT * FROM foo WHERE fooid = 123},
"simple test"
);
}
########################################################################
{
my $tbr = Test::DatabaseRow::Object->new(
dbh => $dbh,
sql_and_bind => [ q{SELECT * FROM foo WHERE fooid = 123} ],
);
is_deeply($tbr->sql_and_bind,
[ q{SELECT * FROM foo WHERE fooid = 123} ],
"simple test sql arrayref no bind"
);
}
########################################################################
{
my $array = [ q{SELECT * FROM foo WHERE fooid = ? AND bar = ?}, 123, 456 ];
my $tbr = Test::DatabaseRow::Object->new(
dbh => $dbh,
sql_and_bind => $array,
);
is_deeply(
$array,
[ q{SELECT * FROM foo WHERE fooid = ? AND bar = ?}, 123, 456 ],
"array passed in unaltered",
);
is_deeply(
$tbr->sql_and_bind,
[ q{SELECT * FROM foo WHERE fooid = ? AND bar = ?}, 123, 456 ],
"simple test sql arrayref with bind"
);
}
########################################################################
# from where and table
########################################################################
{
my $where = { '=' => { fooid => 123, bar => "abc" } };
my $tdr = Test::DatabaseRow::Object->new(
dbh => $dbh,
table => "foo",
where => $where
);
is_deeply(
$where,
{ '=' => { fooid => 123, bar => "abc" } },
"where datastructure unaltered"
);
is_deeply(
$tdr->sql_and_bind,
[ q{SELECT * FROM foo WHERE bar = qtd<abc> AND fooid = qtd<123>} ],
"simple equals test"
);
}
########################################################################
{
my $where = [ fooid => 123, bar => "abc" ];
my $tbr = Test::DatabaseRow::Object->new(
dbh => $dbh,
table => "foo",
where => $where
);
is_deeply(
$where,
[ fooid => 123, bar => "abc" ],
"where datastructure unaltered"
);
is_deeply( $tbr->sql_and_bind,
[ q{SELECT * FROM foo WHERE bar = qtd<abc> AND fooid = qtd<123>} ],
"simple equals test with shortcut"
);
}
########################################################################
# nulls
########################################################################
is_deeply(
Test::DatabaseRow::Object->new(
dbh => $dbh,
table => "foo",
where => [ fooid => undef ]
)->sql_and_bind,
[q{SELECT * FROM foo WHERE fooid IS NULL}],
"auto null test"
);
is_deeply(
Test::DatabaseRow::Object->new(
dbh => $dbh,
table => "foo",
where => { "=" => { fooid => undef } }
)->sql_and_bind,
[q{SELECT * FROM foo WHERE fooid IS NULL}],
"auto null test2"
);
is_deeply(
Test::DatabaseRow::Object->new(
dbh => $dbh,
table => "foo",
where => { "IS NOT" => { fooid => undef } }
)->sql_and_bind,
[q{SELECT * FROM foo WHERE fooid IS NOT NULL}],
"auto null test3"
);
########################################################################
# fake database package
package FakeDBI;
sub new { return bless {}, shift };
sub quote { return "qtd<$_[1]>" };