added common test executing ...
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use DBIx::Custom; |
|
5 | ||
6 |
my $dbi; |
|
7 | ||
8 |
plan skip_all => $ENV{DBIX_CUSTOM_SKIP_MESSAGE} || 'common.t is always skipped' |
|
9 |
unless $ENV{DBIX_CUSTOM_TEST_RUN} |
|
10 |
&& eval { $dbi = DBIx::Custom->connect; 1 }; |
|
11 | ||
12 |
plan 'no_plan'; |
|
13 | ||
cleanup test
|
14 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
15 |
sub test { print "# $_[0]\n" } |
|
16 | ||
added common test executing ...
|
17 |
# Constant |
18 |
my $create_table1 = $dbi->create_table1; |
|
cleanup test
|
19 |
my $create_table1_2 = $dbi->create_table1_2; |
20 | ||
added common test executing ...
|
21 | |
22 |
# Variable |
|
cleanup test
|
23 |
# Variables |
24 |
my $builder; |
|
25 |
my $datas; |
|
26 |
my $sth; |
|
27 |
my $source; |
|
28 |
my @sources; |
|
29 |
my $select_source; |
|
30 |
my $insert_source; |
|
31 |
my $update_source; |
|
32 |
my $param; |
|
33 |
my $params; |
|
34 |
my $sql; |
|
35 |
my $result; |
|
36 |
my $row; |
|
37 |
my @rows; |
|
38 |
my $rows; |
|
39 |
my $query; |
|
40 |
my @queries; |
|
41 |
my $select_query; |
|
42 |
my $insert_query; |
|
43 |
my $update_query; |
|
44 |
my $ret_val; |
|
45 |
my $infos; |
|
added common test executing ...
|
46 |
my $model; |
cleanup test
|
47 |
my $model2; |
48 |
my $where; |
|
49 |
my $update_param; |
|
50 |
my $insert_param; |
|
51 |
my $join; |
|
added common test executing ...
|
52 | |
53 |
# Drop table |
|
54 |
eval { $dbi->execute('drop table table1') }; |
|
55 | ||
56 |
# Create table |
|
57 |
$dbi->execute($create_table1); |
|
58 |
$model = $dbi->create_model(table => 'table1'); |
|
59 |
$model->insert({key1 => 1, key2 => 2}); |
|
60 |
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]); |
|
61 | ||
cleanup test
|
62 |
test 'DBIx::Custom::Result test'; |
63 |
$dbi->delete_all(table => 'table1'); |
|
64 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
65 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
66 |
$source = "select key1, key2 from table1"; |
|
67 |
$query = $dbi->create_query($source); |
|
68 |
$result = $dbi->execute($query); |
|
69 | ||
70 |
@rows = (); |
|
71 |
while (my $row = $result->fetch) { |
|
72 |
push @rows, [@$row]; |
|
73 |
} |
|
74 |
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch"); |
|
75 | ||
76 |
$result = $dbi->execute($query); |
|
77 |
@rows = (); |
|
78 |
while (my $row = $result->fetch_hash) { |
|
79 |
push @rows, {%$row}; |
|
80 |
} |
|
81 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash"); |
|
82 | ||
83 |
$result = $dbi->execute($query); |
|
84 |
$rows = $result->fetch_all; |
|
85 |
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all"); |
|
86 | ||
87 |
$result = $dbi->execute($query); |
|
88 |
$rows = $result->fetch_hash_all; |
|
89 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all"); |
|
90 | ||
91 |
test 'Insert query return value'; |
|
92 |
$source = "insert into table1 {insert_param key1 key2}"; |
|
93 |
$query = $dbi->execute($source, {}, query => 1); |
|
94 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
|
95 |
ok($ret_val); |
|
96 | ||
97 |
test 'Direct query'; |
|
98 |
$dbi->delete_all(table => 'table1'); |
|
99 |
$insert_source = "insert into table1 {insert_param key1 key2}"; |
|
100 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2}); |
|
101 |
$result = $dbi->execute('select * from table1;'); |
|
102 |
$rows = $result->all; |
|
103 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
104 | ||
105 |
test 'Filter basic'; |
|
106 |
$dbi->delete_all(table => 'table1'); |
|
107 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
108 |
three_times => sub { $_[0] * 3}); |
|
109 | ||
110 |
$insert_source = "insert into table1 {insert_param key1 key2};"; |
|
111 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
|
112 |
$insert_query->filter({key1 => 'twice'}); |
|
113 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
114 |
$result = $dbi->execute('select * from table1;'); |
|
115 |
$rows = $result->filter({key2 => 'three_times'})->all; |
|
116 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter"); |
|
117 | ||
118 |
test 'Filter in'; |
|
119 |
$dbi->delete_all(table => 'table1'); |
|
120 |
$insert_source = "insert into table1 {insert_param key1 key2};"; |
|
121 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
|
122 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
|
123 |
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
124 |
$select_query = $dbi->execute($select_source,{}, query => 1); |
|
125 |
$select_query->filter({'table1.key1' => 'twice'}); |
|
126 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
127 |
$rows = $result->all; |
|
128 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter"); |
|
129 | ||
cleanup test
|
130 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
131 |
$dbi->execute('drop table table1'); |
|
cleanup test
|
132 |
$dbi->execute($create_table1_2); |
cleanup test
|
133 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
134 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
135 | ||
136 |
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
137 |
$query = $dbi->execute($source, {}, query => 1); |
|
138 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
139 |
$rows = $result->all; |
|
140 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
|
141 | ||
142 |
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
143 |
$query = $dbi->execute($source, {}, query => 1); |
|
144 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
145 |
$rows = $result->all; |
|
146 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
|
147 | ||
148 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
|
149 |
$query = $dbi->execute($source, {}, query => 1); |
|
150 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
|
151 |
$rows = $result->all; |
|
152 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2"); |
|
153 | ||
154 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
155 |
$dbi->execute('drop table table1'); |
|
cleanup test
|
156 |
$dbi->execute($create_table1_2); |
cleanup test
|
157 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
158 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
159 | ||
160 |
$source = "select * from table1 where {in key1 2};"; |
|
161 |
$query = $dbi->execute($source, {}, query => 1); |
|
162 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
|
163 |
$rows = $result->all; |
|
164 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
|
165 | ||
166 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
167 |
$dbi->delete_all(table => 'table1'); |
|
168 |
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
|
169 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
170 | ||
171 |
$result = $dbi->execute('select * from table1;'); |
|
172 |
$rows = $result->all; |
|
173 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
|
174 | ||
175 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
176 |
$dbi->delete_all(table => 'table1'); |
|
177 |
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
|
178 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
179 |
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
180 | ||
181 |
$update_source = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
|
182 |
$dbi->execute($update_source, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
183 | ||
184 |
$result = $dbi->execute('select * from table1 order by key1;'); |
|
185 |
$rows = $result->all; |
|
186 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
187 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic"); |
|
188 | ||
cleanup test
|
189 |
test 'Named placeholder'; |
190 |
$dbi->execute('drop table table1'); |
|
191 |
$dbi->execute($create_table1_2); |
|
192 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
193 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
194 | ||
195 |
$source = "select * from table1 where key1 = :key1 and key2 = :key2"; |
|
196 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
197 |
$rows = $result->all; |
|
198 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
199 | ||
200 |
$source = "select * from table1 where key1 = \n:key1\n and key2 = :key2"; |
|
201 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
202 |
$rows = $result->all; |
|
203 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
204 | ||
205 |
$source = "select * from table1 where key1 = :key1 or key1 = :key1"; |
|
206 |
$result = $dbi->execute($source, param => {key1 => [1, 2]}); |
|
207 |
$rows = $result->all; |
|
208 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
209 | ||
210 |
$source = "select * from table1 where key1 = :table1.key1 and key2 = :table1.key2"; |
|
211 |
$result = $dbi->execute( |
|
212 |
$source, |
|
213 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
214 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
215 |
); |
|
216 |
$rows = $result->all; |
|
217 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
218 | ||
219 |
$dbi->execute('drop table table1'); |
|
220 |
$dbi->execute($create_table1); |
|
221 |
$dbi->insert(table => 'table1', param => {key1 => '2011-10-14 12:19:18', key2 => 2}); |
|
222 |
$source = "select * from table1 where key1 = '2011-10-14 12:19:18' and key2 = :key2"; |
|
223 |
$result = $dbi->execute( |
|
224 |
$source, |
|
225 |
param => {'key2' => 2}, |
|
226 |
); |
|
227 | ||
228 |
$rows = $result->all; |
|
229 |
is_deeply($rows, [{key1 => '2011-10-14 12:19:18', key2 => 2}]); |
|
230 | ||
231 |
$dbi->delete_all(table => 'table1'); |
|
232 |
$dbi->insert(table => 'table1', param => {key1 => 'a:b c:d', key2 => 2}); |
|
233 |
$source = "select * from table1 where key1 = 'a\\:b c\\:d' and key2 = :key2"; |
|
234 |
$result = $dbi->execute( |
|
235 |
$source, |
|
236 |
param => {'key2' => 2}, |
|
237 |
); |
|
238 |
$rows = $result->all; |
|
239 |
is_deeply($rows, [{key1 => 'a:b c:d', key2 => 2}]); |
|
240 | ||
cleanup test
|
241 |
1; |