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