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