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; |
|
19 | ||
20 |
# Variable |
|
cleanup test
|
21 |
# Variables |
22 |
my $builder; |
|
23 |
my $datas; |
|
24 |
my $sth; |
|
25 |
my $source; |
|
26 |
my @sources; |
|
27 |
my $select_source; |
|
28 |
my $insert_source; |
|
29 |
my $update_source; |
|
30 |
my $param; |
|
31 |
my $params; |
|
32 |
my $sql; |
|
33 |
my $result; |
|
34 |
my $row; |
|
35 |
my @rows; |
|
36 |
my $rows; |
|
37 |
my $query; |
|
38 |
my @queries; |
|
39 |
my $select_query; |
|
40 |
my $insert_query; |
|
41 |
my $update_query; |
|
42 |
my $ret_val; |
|
43 |
my $infos; |
|
added common test executing ...
|
44 |
my $model; |
cleanup test
|
45 |
my $model2; |
46 |
my $where; |
|
47 |
my $update_param; |
|
48 |
my $insert_param; |
|
49 |
my $join; |
|
added common test executing ...
|
50 | |
51 |
# Drop table |
|
52 |
eval { $dbi->execute('drop table table1') }; |
|
53 | ||
54 |
# Create table |
|
55 |
$dbi->execute($create_table1); |
|
56 |
$model = $dbi->create_model(table => 'table1'); |
|
57 |
$model->insert({key1 => 1, key2 => 2}); |
|
58 |
is_deeply($model->select->all, [{key1 => 1, key2 => 2}]); |
|
59 | ||
cleanup test
|
60 |
test 'DBIx::Custom::Result test'; |
61 |
$dbi->delete_all(table => 'table1'); |
|
62 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
63 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
64 |
$source = "select key1, key2 from table1"; |
|
65 |
$query = $dbi->create_query($source); |
|
66 |
$result = $dbi->execute($query); |
|
67 | ||
68 |
@rows = (); |
|
69 |
while (my $row = $result->fetch) { |
|
70 |
push @rows, [@$row]; |
|
71 |
} |
|
72 |
is_deeply(\@rows, [[1, 2], [3, 4]], "fetch"); |
|
73 | ||
74 |
$result = $dbi->execute($query); |
|
75 |
@rows = (); |
|
76 |
while (my $row = $result->fetch_hash) { |
|
77 |
push @rows, {%$row}; |
|
78 |
} |
|
79 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "fetch_hash"); |
|
80 | ||
81 |
$result = $dbi->execute($query); |
|
82 |
$rows = $result->fetch_all; |
|
83 |
is_deeply($rows, [[1, 2], [3, 4]], "fetch_all"); |
|
84 | ||
85 |
$result = $dbi->execute($query); |
|
86 |
$rows = $result->fetch_hash_all; |
|
87 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "all"); |
|
88 | ||
89 |
test 'Insert query return value'; |
|
90 |
$source = "insert into table1 {insert_param key1 key2}"; |
|
91 |
$query = $dbi->execute($source, {}, query => 1); |
|
92 |
$ret_val = $dbi->execute($query, param => {key1 => 1, key2 => 2}); |
|
93 |
ok($ret_val); |
|
94 | ||
95 |
test 'Direct query'; |
|
96 |
$dbi->delete_all(table => 'table1'); |
|
97 |
$insert_source = "insert into table1 {insert_param key1 key2}"; |
|
98 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2}); |
|
99 |
$result = $dbi->execute('select * from table1;'); |
|
100 |
$rows = $result->all; |
|
101 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
102 | ||
103 |
test 'Filter basic'; |
|
104 |
$dbi->delete_all(table => 'table1'); |
|
105 |
$dbi->register_filter(twice => sub { $_[0] * 2}, |
|
106 |
three_times => sub { $_[0] * 3}); |
|
107 | ||
108 |
$insert_source = "insert into table1 {insert_param key1 key2};"; |
|
109 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
|
110 |
$insert_query->filter({key1 => 'twice'}); |
|
111 |
$dbi->execute($insert_query, param => {key1 => 1, key2 => 2}); |
|
112 |
$result = $dbi->execute('select * from table1;'); |
|
113 |
$rows = $result->filter({key2 => 'three_times'})->all; |
|
114 |
is_deeply($rows, [{key1 => 2, key2 => 6}], "filter fetch_filter"); |
|
115 | ||
116 |
test 'Filter in'; |
|
117 |
$dbi->delete_all(table => 'table1'); |
|
118 |
$insert_source = "insert into table1 {insert_param key1 key2};"; |
|
119 |
$insert_query = $dbi->execute($insert_source, {}, query => 1); |
|
120 |
$dbi->execute($insert_query, param => {key1 => 2, key2 => 4}); |
|
121 |
$select_source = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}"; |
|
122 |
$select_query = $dbi->execute($select_source,{}, query => 1); |
|
123 |
$select_query->filter({'table1.key1' => 'twice'}); |
|
124 |
$result = $dbi->execute($select_query, param => {'table1.key1' => [1,5], 'table1.key2' => [2,4]}); |
|
125 |
$rows = $result->all; |
|
126 |
is_deeply($rows, [{key1 => 2, key2 => 4}], "filter"); |
|
127 | ||
cleanup test
|
128 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
129 |
$dbi->execute('drop table table1'); |
|
130 |
$dbi->execute($dbi->create_table1_2); |
|
131 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
132 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
133 | ||
134 |
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
135 |
$query = $dbi->execute($source, {}, query => 1); |
|
136 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
137 |
$rows = $result->all; |
|
138 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
|
139 | ||
140 |
$source = "select * from table1 where key1 = :key1 and {<> key2} and {< key3} and {> key4} and {>= key5};"; |
|
141 |
$query = $dbi->execute($source, {}, query => 1); |
|
142 |
$result = $dbi->execute($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}); |
|
143 |
$rows = $result->all; |
|
144 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag1"); |
|
145 | ||
146 |
$source = "select * from table1 where {<= key1} and {like key2};"; |
|
147 |
$query = $dbi->execute($source, {}, query => 1); |
|
148 |
$result = $dbi->execute($query, param => {key1 => 1, key2 => '%2%'}); |
|
149 |
$rows = $result->all; |
|
150 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic tag2"); |
|
151 | ||
152 |
test 'DIB::Custom::SQLTemplate in tag'; |
|
153 |
$dbi->execute('drop table table1'); |
|
154 |
$dbi->execute($dbi->create_table1_2); |
|
155 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
156 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
157 | ||
158 |
$source = "select * from table1 where {in key1 2};"; |
|
159 |
$query = $dbi->execute($source, {}, query => 1); |
|
160 |
$result = $dbi->execute($query, param => {key1 => [9, 1]}); |
|
161 |
$rows = $result->all; |
|
162 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
|
163 | ||
164 |
test 'DBIx::Custom::SQLTemplate insert tag'; |
|
165 |
$dbi->delete_all(table => 'table1'); |
|
166 |
$insert_source = 'insert into table1 {insert_param key1 key2 key3 key4 key5}'; |
|
167 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
168 | ||
169 |
$result = $dbi->execute('select * from table1;'); |
|
170 |
$rows = $result->all; |
|
171 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "basic"); |
|
172 | ||
173 |
test 'DBIx::Custom::SQLTemplate update tag'; |
|
174 |
$dbi->delete_all(table => 'table1'); |
|
175 |
$insert_source = "insert into table1 {insert_param key1 key2 key3 key4 key5}"; |
|
176 |
$dbi->execute($insert_source, param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
177 |
$dbi->execute($insert_source, param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
178 | ||
179 |
$update_source = 'update table1 {update_param key1 key2 key3 key4} where {= key5}'; |
|
180 |
$dbi->execute($update_source, param => {key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}); |
|
181 | ||
182 |
$result = $dbi->execute('select * from table1 order by key1;'); |
|
183 |
$rows = $result->all; |
|
184 |
is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5}, |
|
185 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "basic"); |
|
186 | ||
187 |
1; |