fixed tests
|
1 |
use strict; |
2 |
use warnings; |
|
3 | ||
4 |
use Test::More 'no_plan'; |
|
5 | ||
cleanup
|
6 |
use DBIx::Custom; |
fixed tests
|
7 | |
8 |
# Function for test name |
|
table object call dbi object...
|
9 |
sub test{ print "# $_[0]\n" } |
fixed tests
|
10 | |
11 |
# Variable for test |
|
12 |
my $datas; |
|
13 |
my $builder; |
|
14 |
my $query; |
|
15 |
my $ret_val; |
|
added tests
|
16 |
my $source; |
17 |
my $tree; |
|
fixed tests
|
18 | |
19 |
test "Various source pattern"; |
|
20 |
$datas = [ |
|
21 |
# Basic tests |
|
22 |
{ name => 'placeholder basic', |
|
23 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
24 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
25 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
|
26 |
}, |
|
27 |
{ |
|
28 |
name => 'placeholder in', |
|
29 |
source => "{in k1 3};", |
|
30 |
sql_expected => "k1 in (?, ?, ?);", |
|
31 |
columns_expected => [qw/k1 k1 k1/] |
|
32 |
}, |
|
33 |
|
|
34 |
# Table name |
|
35 |
{ |
|
36 |
name => 'placeholder with table name', |
|
37 |
source => "{= a.k1} {= a.k2}", |
|
38 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
39 |
columns_expected => [qw/a.k1 a.k2/] |
|
40 |
}, |
|
41 |
{ |
|
42 |
name => 'placeholder in with table name', |
|
43 |
source => "{in a.k1 2} {in b.k2 2}", |
|
44 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
45 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
|
46 |
}, |
|
47 |
{ |
|
48 |
name => 'not contain tag', |
|
49 |
source => "aaa", |
|
50 |
sql_expected => "aaa;", |
|
51 |
columns_expected => [], |
|
52 |
} |
|
53 |
]; |
|
54 | ||
55 |
for (my $i = 0; $i < @$datas; $i++) { |
|
56 |
my $data = $datas->[$i]; |
|
cleanup
|
57 |
my $builder = DBIx::Custom->new->query_builder; |
fixed tests
|
58 |
my $query = $builder->build_query($data->{source}); |
cleanup
|
59 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
60 |
is_deeply($query->{columns}, $data->{columns_expected}, "$data->{name} : columns"); |
|
fixed tests
|
61 |
} |
62 | ||
63 | ||
cleanup
|
64 |
test 'Original tag'; |
65 |
$builder = DBIx::Custom->new->query_builder; |
|
fixed tests
|
66 | |
cleanup
|
67 |
$ret_val = $builder->register_tag( |
fixed tests
|
68 |
p => sub { |
69 |
my @args = @_; |
|
70 |
|
|
71 |
my $expand = "? $args[0] $args[1]"; |
|
72 |
my $columns = [2]; |
|
73 |
return [$expand, $columns]; |
|
74 |
} |
|
75 |
); |
|
76 | ||
77 |
$query = $builder->build_query("{p a b}"); |
|
cleanup
|
78 |
is($query->{sql}, "? a b;", "register_tag sql"); |
79 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
|
fixed tests
|
80 |
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
81 | ||
82 | ||
cleanup
|
83 |
test "Tag error case"; |
84 |
$builder = DBIx::Custom->new->query_builder; |
|
fixed tests
|
85 | |
add tests
|
86 |
eval{$builder->build_query('{? }')}; |
cleanup
|
87 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
fixed tests
|
88 | |
89 |
eval{$builder->build_query("{a }")}; |
|
select, update, and delete w...
|
90 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
fixed tests
|
91 | |
cleanup
|
92 |
$builder->register_tag({ |
fixed tests
|
93 |
q => 'string' |
94 |
}); |
|
95 | ||
96 |
eval{$builder->build_query("{q}", {})}; |
|
cleanup
|
97 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
fixed tests
|
98 | |
cleanup
|
99 |
$builder->register_tag({ |
fixed tests
|
100 |
r => sub {} |
101 |
}); |
|
102 | ||
103 |
eval{$builder->build_query("{r}")}; |
|
cleanup
|
104 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
fixed tests
|
105 | |
cleanup
|
106 |
$builder->register_tag({ |
fixed tests
|
107 |
s => sub { return ["a", ""]} |
108 |
}); |
|
109 | ||
110 |
eval{$builder->build_query("{s}")}; |
|
cleanup
|
111 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
fixed tests
|
112 | |
cleanup
|
113 |
$builder->register_tag( |
fixed tests
|
114 |
t => sub {return ["a", []]} |
115 |
); |
|
116 | ||
117 | ||
118 |
test 'General error case'; |
|
cleanup
|
119 |
$builder = DBIx::Custom->new->query_builder; |
120 |
$builder->register_tag( |
|
fixed tests
|
121 |
a => sub { |
122 |
return ["? ? ?", ['']]; |
|
123 |
} |
|
124 |
); |
|
125 |
eval{$builder->build_query("{a}")}; |
|
cleanup
|
126 |
like($@, qr/\QPlaceholder count in "? ? ?" must be same as column count 1/, "placeholder count is invalid"); |
fixed tests
|
127 | |
128 | ||
cleanup
|
129 |
test 'Default tag Error case'; |
fixed tests
|
130 |
eval{$builder->build_query("{= }")}; |
cleanup
|
131 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
fixed tests
|
132 | |
133 |
eval{$builder->build_query("{in }")}; |
|
cleanup
|
134 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
fixed tests
|
135 | |
136 |
eval{$builder->build_query("{in a}")}; |
|
137 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
cleanup
|
138 |
"in : key not exist"); |
fixed tests
|
139 | |
140 |
eval{$builder->build_query("{in a r}")}; |
|
141 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
cleanup
|
142 |
"in : key not exist"); |
fixed tests
|
143 | |
added tests
|
144 |
test 'variouse source'; |
145 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;"; |
|
146 |
$query = $builder->build_query($source); |
|
cleanup
|
147 |
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1"); |
removed DBIx::Custom::Query ...
|
148 | |
added tests
|
149 |
$source = "abc;"; |
150 |
$query = $builder->build_query($source); |
|
cleanup
|
151 |
is($query->sql, 'abc;', "basic : 2"); |
removed DBIx::Custom::Query ...
|
152 | |
added tests
|
153 |
$source = "{= a}"; |
154 |
$query = $builder->build_query($source); |
|
cleanup
|
155 |
is($query->sql, 'a = ?;', "only tag"); |
fixed DBIx::Custom::QueryBui...
|
156 | |
157 |
$source = "000;"; |
|
158 |
$query = $builder->build_query($source); |
|
cleanup
|
159 |
is($query->sql, '000;', "contain 0 value"); |
added tests
|
160 | |
161 |
$source = "a {= b} }"; |
|
162 |
eval{$builder->build_query($source)}; |
|
cleanup
|
163 |
like($@, qr/unexpected "}"/, "error : 1"); |
added tests
|
164 | |
165 |
$source = "a {= {}"; |
|
166 |
eval{$builder->build_query($source)}; |
|
cleanup
|
167 |
like($@, qr/unexpected "{"/, "error : 2"); |
removed DBIx::Custom::Query ...
|
168 | |
fixed DBIx::Custom::QueryBui...
|
169 |