TEST/Search.pm

filter: Statementstmt 3.9% 7 / 177 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 13.3% 2 / 15 Podpod 0.0% 0 / 9 totaltotal 4.4% 9 / 201 CC 1 SCAR 6.2
CC
1
Cov
4%
test_search__basic
2.0
test_search__with_a_join
2.0
test_search__with_a_filter
2.0
CRAP 1.9
« Search/Display/TEST/Table.pm ↑ Summary
1 package CGI::Widget::DBI::TEST::Search;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw/ CGI::Widget::DBI::TEST::TestCase /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub test_search__basic
7 0 ▶{
Subroutine test_search__basic: not called CC=1 SCAR=6.9
Pod undocumented
8 0 my $self = shift;
9 0 my $ws = $self->{ws};
10  
11 0 $ws->{-sql_table} = 'widgets';
12 0 $ws->{-sql_retrieve_columns} = [qw/widget_no name description size/];
13  
14 0 $ws->search();
15  
16 $self->assert_deep_equals([
17 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
18 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
19 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
20 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
21 0 ], $ws->{'results'});
22 0 $self->assert_equals(4, $ws->{'numresults'});
23 }
24  
25 sub test_search__with_a_join
26 0 ▶{
Subroutine test_search__with_a_join: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my $self = shift;
28 0 my $ws = $self->{ws};
29  
30 $ws->{-sql_table} =
31 0 'widgets w inner join widget_tools wt using (widget_no) inner join tools t using (tool_no)';
32 $ws->{-sql_retrieve_columns} =
33 0 [qw/w.widget_no w.name w.description w.size t.tool_no/, 't.name as tool_name', 't.type'];
34  
35 0 $ws->search();
36  
37 $self->assert_deep_equals([
38 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small',
39 tool_no => 2, tool_name => 'wrench', type => 'hand', },
40 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small',
41 tool_no => 1, tool_name => 'hammer', type => 'hand', },
42 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium',
43 tool_no => 5, tool_name => 'emacs', type => 'software', },
44 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium',
45 tool_no => 6, tool_name => 'apache', type => 'software', },
46 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown',
47 tool_no => 4, tool_name => 'rm', type => 'unix', },
48 0 ], $ws->{'results'});
49 0 $self->assert_equals(5, $ws->{'numresults'});
50 }
51  
52 sub test_search__with_a_filter
53 0 ▶{
Subroutine test_search__with_a_filter: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my $self = shift;
55 0 my $ws = $self->{ws};
56  
57 0 $ws->{-sql_table} = 'widgets';
58 0 $ws->{-sql_retrieve_columns} = [qw/widget_no name description size/];
59  
60 0 $ws->{-where_clause} = 'name LIKE ?';
61 0 $ws->{-bind_params} = ['c%']; # name begins with 'c'
62  
63 0 $ws->search();
64  
65 $self->assert_deep_equals([
66 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
67 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
68 0 ], $ws->{'results'});
69 0 $self->assert_equals(2, $ws->{'numresults'});
70  
71 # try a different filter
72 0 delete $ws->{'results'}; # need to clear old results or it will just return cached copy
73 0 delete $ws->{'numresults'}; # need to clear old numresults, too or it will just return cached copy
74 0 $ws->{-where_clause} = 'name LIKE ? AND size = ?';
75 0 $ws->{-bind_params} = ['c%', 'medium']; # name begins with 'c' and size is medium
76  
77 0 $ws->search();
78  
79 $self->assert_deep_equals([
80 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
81 0 ], $ws->{'results'});
82 0 $self->assert_equals(1, $ws->{'numresults'});
83 }
84  
85 sub test_search__with_join_for_dataset_and_filter
86 0 ▶{
Subroutine test_search__with_join_for_dataset_and_filter: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my $self = shift;
88 0 my $ws = $self->{ws};
89  
90 0 ▶ my $setup = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 my $ws = shift;
92 0 $ws->{-sql_table} = 'widgets';
93 0 $ws->{-sql_search_columns} = [qw/widget_no/, 'COALESCE(description, name) AS my_description'];
94 0 $ws->{-sql_join_for_dataset} = 'inner join widgets w using (widget_no)';
95 0 $ws->{-sql_retrieve_columns} = [qw/w.widget_no w.name w.description w.size/];
96  
97 0 $ws->{-where_clause} = 'size != ?';
98 0 $ws->{-bind_params} = ['unknown'];
99  
100 0 $ws->{-max_results_per_page} = 2;
101 0 };
102 0 $setup->($ws);
103  
104 0 $ws->search();
105  
106 $self->assert_deep_equals([
107 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small' },
108 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium' },
109 0 ], $ws->{'results'});
110 0 $self->assert_equals(3, $ws->{'numresults'});
111 # TODO: what happens with an -sql_join_for_dataset that increases the row count?? test this
112  
113 # reset search
114 0 $self->init_test_object();
115 0 $ws = $self->{ws};
116 0 $setup->($ws);
117  
118 0 $ws->{q}->param('search_startat', 1); # next page
119 0 $ws->search();
120  
121 $self->assert_deep_equals([
122 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium' },
123 0 ], $ws->{'results'});
124 0 $self->assert_equals(3, $ws->{'numresults'});
125 }
126  
127 sub _setup_test_search__paging
128 0 ▶{
Subroutine _setup_test_search__paging: not called CC=1 SCAR=6.9
129 0 my $self = shift;
130 0 my $ws = $self->{ws};
131 $ws->{-sql_table} =
132 0 'widgets w inner join widget_tools wt using (widget_no) inner join tools t using (tool_no)';
133 $ws->{-sql_retrieve_columns} =
134 0 [qw/w.widget_no w.name w.description w.size t.tool_no/, 't.name as tool_name', 't.type'];
135 0 $ws->{-max_results_per_page} = 2;
136 }
137  
138 sub test_search__paging
139 0 ▶{
Subroutine test_search__paging: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $self = shift;
141 0 my $ws = $self->{ws};
142  
143 0 $self->_setup_test_search__paging();
144 0 $ws->search();
145  
146 $self->assert_deep_equals([
147 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small',
148 tool_no => 2, tool_name => 'wrench', type => 'hand', },
149 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small',
150 tool_no => 1, tool_name => 'hammer', type => 'hand', },
151 0 ], $ws->{'results'});
152 0 $self->assert_equals(5, $ws->{'numresults'});
153  
154 # reset search
155 0 $self->init_test_object();
156 0 $self->_setup_test_search__paging();
157 0 $ws = $self->{ws};
158  
159 0 $ws->{q}->param('search_startat', 1);
160 0 $ws->search();
161  
162 $self->assert_deep_equals([
163 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium',
164 tool_no => 5, tool_name => 'emacs', type => 'software', },
165 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium',
166 tool_no => 6, tool_name => 'apache', type => 'software', },
167 0 ], $ws->{'results'});
168 0 $self->assert_equals(5, $ws->{'numresults'});
169  
170 # reset search without re-initializing object (test for running in persistent object environments)
171 0 delete $ws->{'results'};
172  
173 0 $ws->{q}->param('search_startat', 2);
174 0 $ws->search();
175  
176 $self->assert_deep_equals([
177 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown',
178 tool_no => 4, tool_name => 'rm', type => 'unix', },
179 0 ], $ws->{'results'});
180 0 $self->assert_equals(5, $ws->{'numresults'});
181 0 $self->assert_does_not_match(qr/sql_calc_found_rows sql_calc_found_rows/i, $ws->{'_sql'});
182 }
183  
184 sub _setup_test_search__sorting
185 0 ▶{
Subroutine _setup_test_search__sorting: not called CC=1 SCAR=6.9
186 0 my $self = shift;
187 0 my $ws = $self->{ws};
188  
189 0 $ws->{-sql_table} = 'widgets';
190 0 $ws->{-sql_retrieve_columns} = [qw/widget_no name description size/];
191 }
192  
193 sub test_search__sorting
194 0 ▶{
Subroutine test_search__sorting: not called CC=1 SCAR=6.9
Pod undocumented
195 0 my $self = shift;
196 0 my $ws = $self->{ws};
197  
198 0 $self->_setup_test_search__sorting();
199 0 $ws->{q}->param('sortby', 'description');
200 0 $ws->search();
201  
202 $self->assert_deep_equals([
203 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
204 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
205 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
206 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
207 0 ], $ws->{'results'});
208 0 $self->assert_equals(4, $ws->{'numresults'});
209  
210 # reset search
211 0 $self->init_test_object();
212 0 $self->_setup_test_search__sorting();
213 0 $ws->{q}->param('sortby', 'description');
214 0 $ws = $self->{ws};
215  
216 0 $ws->{q}->param('sortby', 'widget_no');
217 0 $ws->{q}->param('sort_reverse', 1);
218 0 $ws->search();
219  
220 $self->assert_deep_equals([
221 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
222 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
223 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
224 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
225 0 ], $ws->{'results'});
226 0 $self->assert_equals(4, $ws->{'numresults'});
227 }
228  
229 sub test_search__paging_and_sorting_together
230 0 ▶{
Subroutine test_search__paging_and_sorting_together: not called CC=1 SCAR=6.9
Pod undocumented
231 0 my $self = shift;
232 0 my $ws = $self->{ws};
233  
234 0 $self->_setup_test_search__paging();
235 0 $ws->{q}->param('sortby', 'tool_name');
236 0 $ws->search();
237  
238 $self->assert_deep_equals([
239 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium',
240 tool_no => 6, tool_name => 'apache', type => 'software', },
241 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium',
242 tool_no => 5, tool_name => 'emacs', type => 'software', },
243 0 ], $ws->{'results'});
244 0 $self->assert_equals(5, $ws->{'numresults'});
245  
246 # reset search
247 0 $self->init_test_object();
248 0 $self->_setup_test_search__paging();
249 0 $ws = $self->{ws};
250  
251 0 $ws->{q}->param('sortby', 'tool_name');
252 0 $ws->{q}->param('search_startat', 1);
253 0 $ws->search();
254  
255 $self->assert_deep_equals([
256 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small',
257 tool_no => 1, tool_name => 'hammer', type => 'hand', },
258 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown',
259 tool_no => 4, tool_name => 'rm', type => 'unix', },
260 0 ], $ws->{'results'});
261 0 $self->assert_equals(5, $ws->{'numresults'});
262  
263 # reset search without re-initializing object (test for running in persistent object environments)
264 0 delete $ws->{'results'};
265  
266 0 $ws->{q}->param('sortby', 'tool_name');
267 0 $ws->{q}->param('search_startat', 2);
268 0 $ws->search();
269  
270 $self->assert_deep_equals([
271 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small',
272 tool_no => 2, tool_name => 'wrench', type => 'hand', },
273 0 ], $ws->{'results'});
274 0 $self->assert_equals(5, $ws->{'numresults'});
275 0 $self->assert_does_not_match(qr/sql_calc_found_rows sql_calc_found_rows/i, $ws->{'_sql'});
276 }
277  
278 sub _setup_test_search__default_orderby_and_sorting
279 0 ▶{
Subroutine _setup_test_search__default_orderby_and_sorting: not called CC=1 SCAR=6.9
280 0 my $self = shift;
281 0 my $ws = $self->{ws};
282  
283 0 $ws->{-sql_table} = 'widgets';
284 0 $ws->{-sql_retrieve_columns} = [qw/widget_no name description size/];
285 0 $ws->{-default_orderby_columns} = [qw/name widget_no/];
286 }
287  
288 sub test_search__default_orderby_and_sorting
289 0 ▶{
Subroutine test_search__default_orderby_and_sorting: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my $self = shift;
291 0 my $ws = $self->{ws};
292  
293 0 $self->_setup_test_search__default_orderby_and_sorting();
294 0 $ws->search();
295  
296 $self->assert_deep_equals([
297 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
298 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
299 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
300 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
301 0 ], $ws->{'results'});
302 0 $self->assert_equals(4, $ws->{'numresults'});
303  
304 # reset search
305 0 $self->init_test_object();
306 0 $self->_setup_test_search__default_orderby_and_sorting();
307 0 $ws = $self->{ws};
308  
309 0 $ws->{q}->param('sortby', 'widget_no');
310 0 $ws->search();
311  
312 $self->assert_deep_equals([
313 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
314 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
315 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
316 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
317 0 ], $ws->{'results'});
318 0 $self->assert_equals(4, $ws->{'numresults'});
319  
320 # reset search
321 0 $self->init_test_object();
322 0 $self->_setup_test_search__default_orderby_and_sorting();
323 0 $ws = $self->{ws};
324  
325 0 $ws->{q}->param('sortby', 'size');
326 0 $ws->{q}->param('sort_reverse', 1);
327 0 $ws->search();
328  
329 $self->assert_deep_equals([
330 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
331 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
332 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
333 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
334 0 ], $ws->{'results'});
335 0 $self->assert_equals(4, $ws->{'numresults'});
336  
337 # reset search
338 0 $self->init_test_object();
339 0 $self->_setup_test_search__default_orderby_and_sorting();
340 0 $ws = $self->{ws};
341  
342 0 $ws->{q}->param('sortby', 'size');
343 0 $ws->{q}->param('sort_reverse', 0);
344 0 $ws->search();
345  
346 $self->assert_deep_equals([
347 { widget_no => 2, name => 'calendar_widget', description => "A date tracker widget", size => 'medium', },
348 { widget_no => 4, name => 'gps_widget', description => "A GPS widget", size => 'medium', },
349 { widget_no => 1, name => 'clock_widget', description => "A time keeper widget", size => 'small', },
350 { widget_no => 3, name => 'silly_widget', description => "A goofball widget", size => 'unknown', },
351 0 ], $ws->{'results'});
352 0 $self->assert_equals(4, $ws->{'numresults'});
353 }
354  
355 sub test_display_results
356 0 ▶{
Subroutine test_display_results: not called CC=1 SCAR=6.9
Pod undocumented
357 0 my $self = shift;
358 0 my $ws = $self->{ws};
359 $ws->{-display_columns} =
360 0 { widget_no => 'ID', name => 'Name', description => 'Description', size => '' };
361 0 $ws->{-sql_table} = 'widgets';
362 0 $ws->{-sql_retrieve_columns} = [qw/widget_no name description size/];
363 0 $ws->search();
364  
365 0 $self->assert_display_contains(
366 [ 'ID', 'Name', 'Description' ],
367 [ 1, 'clock_widget', 'A time keeper widget', 'small' ],
368 [ 2, 'calendar_widget', 'A date tracker widget', 'medium' ],
369 [ 3, 'silly_widget', 'A goofball widget', 'unknown' ],
370 );
371  
372 # reset search, set default ordering, and hide 'size' column
373 0 $self->init_test_object();
374 0 $ws = $self->{ws};
375 0 $ws->{-default_orderby_columns} = [qw/name widget_no/];
376 $ws->{-display_columns} =
377 0 { widget_no => 'ID', name => 'Name', description => 'Description', size => undef };
378 0 $ws->{-sql_table} = 'widgets';
379 0 $ws->{-sql_retrieve_columns} = [qw/widget_no name description size/];
380 0 $ws->search();
381  
382 0 $self->assert_display_contains(
383 [ 'ID', 'Name', 'Description' ],
384 [ 2, 'calendar_widget', 'A date tracker widget' ],
385 [ 1, 'clock_widget', 'A time keeper widget' ],
386 [ 3, 'silly_widget', 'A goofball widget' ],
387 );
388 0 $self->assert_display_does_not_contain([ 'medium' ]);
389 0 $self->assert_display_does_not_contain([ 'small' ]);
390 0 $self->assert_display_does_not_contain([ 'unknown' ]);
391 }
392  
393  
394 3 1;
« Search/Display/TEST/Table.pm ↑ Summary