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