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