packaging one directory
|
1 |
use strict; |
2 |
use warnings; |
|
3 | ||
4 |
use Test::More 'no_plan'; |
|
5 | ||
add all tests
|
6 |
use DBIx::Custom::SQLTemplate; |
packaging one directory
|
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 $sql_tmpl; |
|
17 |
my $query; |
|
18 |
my $ret_val; |
|
19 |
my $clone; |
|
20 | ||
21 |
test "Various template pattern"; |
|
22 |
$datas = [ |
|
23 |
# Basic tests |
|
24 |
{ name => 'placeholder basic', |
|
25 |
tmpl => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
26 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
add all tests
|
27 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
packaging one directory
|
28 |
}, |
29 |
{ |
|
30 |
name => 'placeholder in', |
|
31 |
tmpl => "{in k1 3};", |
|
32 |
sql_expected => "k1 in (?, ?, ?);", |
|
add all tests
|
33 |
columns_expected => [qw/k1 k1 k1/] |
packaging one directory
|
34 |
}, |
35 |
|
|
36 |
# Table name |
|
37 |
{ |
|
38 |
name => 'placeholder with table name', |
|
39 |
tmpl => "{= a.k1} {= a.k2}", |
|
40 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
add all tests
|
41 |
columns_expected => [qw/a.k1 a.k2/] |
packaging one directory
|
42 |
}, |
43 |
{ |
|
44 |
name => 'placeholder in with table name', |
|
45 |
tmpl => "{in a.k1 2} {in b.k2 2}", |
|
46 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
add all tests
|
47 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
packaging one directory
|
48 |
}, |
49 |
{ |
|
50 |
name => 'not contain tag', |
|
51 |
tmpl => "aaa", |
|
52 |
sql_expected => "aaa;", |
|
add all tests
|
53 |
columns_expected => [], |
packaging one directory
|
54 |
} |
55 |
]; |
|
56 | ||
57 |
for (my $i = 0; $i < @$datas; $i++) { |
|
58 |
my $data = $datas->[$i]; |
|
add all tests
|
59 |
my $sql_tmpl = DBIx::Custom::SQLTemplate->new; |
packaging one directory
|
60 |
my $query = $sql_tmpl->create_query($data->{tmpl}); |
61 |
is($query->{sql}, $data->{sql_expected}, "$test : $data->{name} : sql"); |
|
add all tests
|
62 |
is_deeply($query->{columns}, $data->{columns_expected}, "$test : $data->{name} : columns"); |
packaging one directory
|
63 |
} |
64 | ||
65 | ||
66 |
test 'Original tag processor'; |
|
add all tests
|
67 |
$sql_tmpl = DBIx::Custom::SQLTemplate->new; |
packaging one directory
|
68 | |
add all tests
|
69 |
$ret_val = $sql_tmpl->resist_tag_processor( |
packaging one directory
|
70 |
p => sub { |
71 |
my ($tag_name, $args) = @_; |
|
72 |
|
|
73 |
my $expand = "$tag_name ? $args->[0] $args->[1]"; |
|
add all tests
|
74 |
my $columns = [2]; |
75 |
return ($expand, $columns); |
|
packaging one directory
|
76 |
} |
77 |
); |
|
78 | ||
79 |
$query = $sql_tmpl->create_query("{p a b}"); |
|
add all tests
|
80 |
is($query->{sql}, "p ? a b;", "$test : resist_tag_processor sql"); |
81 |
is_deeply($query->{columns}, [2], "$test : resist_tag_processor columns"); |
|
82 |
isa_ok($ret_val, 'DBIx::Custom::SQLTemplate'); |
|
packaging one directory
|
83 | |
84 | ||
85 |
test "Tag processor error case"; |
|
add all tests
|
86 |
$sql_tmpl = DBIx::Custom::SQLTemplate->new; |
packaging one directory
|
87 | |
88 | ||
89 |
eval{$sql_tmpl->create_query("{a }")}; |
|
90 |
like($@, qr/Tag '{a }' in SQL template is not exist/, "$test : tag_processor not exist"); |
|
91 | ||
add all tests
|
92 |
$sql_tmpl->resist_tag_processor({ |
packaging one directory
|
93 |
q => 'string' |
94 |
}); |
|
95 | ||
96 |
eval{$sql_tmpl->create_query("{q}", {})}; |
|
97 |
like($@, qr/Tag processor 'q' must be code reference/, "$test : tag_processor not code ref"); |
|
98 | ||
add all tests
|
99 |
$sql_tmpl->resist_tag_processor({ |
packaging one directory
|
100 |
r => sub {} |
101 |
}); |
|
102 | ||
103 |
eval{$sql_tmpl->create_query("{r}")}; |
|
add all tests
|
104 |
like($@, qr/\QTag processor 'r' must return (\E\$expand\Q, \E\$columns\Q)/, "$test : tag processor return noting"); |
packaging one directory
|
105 | |
add all tests
|
106 |
$sql_tmpl->resist_tag_processor({ |
packaging one directory
|
107 |
s => sub { return ("a", "")} |
108 |
}); |
|
109 | ||
110 |
eval{$sql_tmpl->create_query("{s}")}; |
|
add all tests
|
111 |
like($@, qr/\QTag processor 's' must return (\E\$expand\Q, \E\$columns\Q)/, "$test : tag processor return not array columns"); |
packaging one directory
|
112 | |
add all tests
|
113 |
$sql_tmpl->resist_tag_processor( |
packaging one directory
|
114 |
t => sub {return ("a", [])} |
115 |
); |
|
116 | ||
117 |
eval{$sql_tmpl->create_query("{t ???}")}; |
|
118 |
like($@, qr/Tag '{t }' arguments cannot contain '?'/, "$test : cannot contain '?' in tag argument"); |
|
119 | ||
120 | ||
121 |
test 'General error case'; |
|
add all tests
|
122 |
$sql_tmpl = DBIx::Custom::SQLTemplate->new; |
123 |
$sql_tmpl->resist_tag_processor( |
|
packaging one directory
|
124 |
a => sub { |
125 |
return ("? ? ?", [[],[]]); |
|
126 |
} |
|
127 |
); |
|
128 |
eval{$sql_tmpl->create_query("{a}")}; |
|
129 |
like($@, qr/Placeholder count in SQL created by tag processor 'a' must be same as key informations count/, "$test placeholder count is invalid"); |
|
130 | ||
131 | ||
132 |
test 'Default tag processor Error case'; |
|
133 |
eval{$sql_tmpl->create_query("{= }")}; |
|
134 |
like($@, qr/You must be pass key as argument to tag '{= }'/, "$test : basic '=' : key not exist"); |
|
135 | ||
136 |
eval{$sql_tmpl->create_query("{in }")}; |
|
137 |
like($@, qr/You must be pass key as first argument of tag '{in }'/, "$test : in : key not exist"); |
|
138 | ||
139 |
eval{$sql_tmpl->create_query("{in a}")}; |
|
add all tests
|
140 |
like($@, qr/\QYou must be pass value count as second argument of tag '{in }'\E\n\QUsage: {in \E\$key\Q \E\$count\Q}/, |
packaging one directory
|
141 |
"$test : in : key not exist"); |
142 | ||
143 |
eval{$sql_tmpl->create_query("{in a r}")}; |
|
add all tests
|
144 |
like($@, qr/\QYou must be pass value count as second argument of tag '{in }'\E\n\QUsage: {in \E\$key\Q \E\$count\Q}/, |
packaging one directory
|
145 |
"$test : in : key not exist"); |
146 | ||
147 | ||
148 |
test 'Clone'; |
|
add all tests
|
149 |
$sql_tmpl = DBIx::Custom::SQLTemplate->new; |
packaging one directory
|
150 |
$sql_tmpl |
151 |
->tag_start('[') |
|
152 |
->tag_end(']') |
|
153 |
->tag_syntax('syntax') |
|
154 |
->tag_processors({a => 1, b => 2}); |
|
155 | ||
156 |
$clone = $sql_tmpl->clone; |
|
157 |
is($clone->tag_start, $sql_tmpl->tag_start, "$test : tag_start"); |
|
158 |
is($clone->tag_end, $sql_tmpl->tag_end, "$test : tag_end"); |
|
159 |
is($clone->tag_syntax, $sql_tmpl->tag_syntax, "$test : tag_syntax"); |
|
160 | ||
161 |
is_deeply( scalar $clone->tag_processors, scalar $sql_tmpl->tag_processors, |
|
162 |
"$test : tag_processors deep clone"); |
|
163 | ||
164 |
isnt($clone->tag_processors, $sql_tmpl->tag_processors, |
|
165 |
"$test : tag_processors reference not copy"); |
|
166 | ||
167 |
$sql_tmpl->tag_processors(undef); |
|
168 | ||
169 |
$clone = $sql_tmpl->clone; |
|
170 |
is_deeply(scalar $clone->tag_processors, {}, "$test tag_processor undef copy"); |
|
171 | ||
172 | ||
173 | ||
174 |
__END__ |
|
175 | ||
176 | ||
177 |