Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / t / unit_fixture / CXGN / List.t
blobe90ba1f3bf2d6026114272a5a4f8b77f65b9f588
2 use strict;
4 use lib 't/lib';
6 use Test::More;
7 use Data::Dumper;
8 use SGN::Test::Fixture;
9 use CXGN::List;
11 my $t = SGN::Test::Fixture->new();
14 my $lists = CXGN::List::available_lists($t->dbh(), 41);
16 my @lists_sorted = sort { $a->[0] <=> $b->[0] } @$lists;
18 print STDERR Dumper \@lists_sorted;
19  is_deeply(\@lists_sorted,  [
20                [
21             '3',
22             'test_stocks',
23             undef,
24             '5',
25             '76451',
26             'accessions',
27                    '0',
28                '0001-01-01 00:00:00',
29                '0001-01-01 00:00:00'
30           ],
31           [
32             '5',
33             'accessions_for_solgs_tests',
34             undef,
35             '374',
36             '76451',
37             'accessions',
38                    '0',
39               '0001-01-01 00:00:00',
40               '0001-01-01 00:00:00'
41           ],
42           [
43             '6',
44             'accessions_for_trial2',
45             undef,
46             '307',
47             '76451',
48             'accessions',
49                   '0',
50               '0001-01-01 00:00:00',
51               '0001-01-01 00:00:00'
52           ],
53           [
54             '7',
55             'selection_acc',
56             undef,
57             '20',
58             undef,
59             undef,
60                   '0',
61               '0001-01-01 00:00:00',
62               '0001-01-01 00:00:00'
63           ],
64           [
65             12,
66             'desynonymize_test_list',
67             undef,
68             6,
69             76451,
70             'accessions',
71             0,
72               '0001-01-01 00:00:00',
73               '0001-01-01 00:00:00'
74           ],
75           [
76             13,
77             'traits',
78             undef,
79             10,
80             76455,
81             'traits',
82             0,
83               '0001-01-01 00:00:00',
84               '0001-01-01 00:00:00'
85           ],
86           [
87             '809',
88             'janedoe_1_public',
89             undef,
90             '2',
91             undef,
92             undef,
93                   '1',
94           '0001-01-01 00:00:00',
95           '0001-01-01 00:00:00'
96           ],
97           [
98             '811',
99             'janedoe_1_private',
100             undef,
101             '2',
102             undef,
103             undef,
104                   '0',
105           '0001-01-01 00:00:00',
106           '0001-01-01 00:00:00'
107           ],
108            ], "check available lists initially");
110 my $list_id = CXGN::List::create_list($t->dbh(), 'test_list2', 'test_desc', 41);
112 #print STDERR "CREATED LIST WITH ID $list_id\n";
114 my $list = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id } );
116 my $name = $list->name();
117 is($name, "test_list2", "get list name");
119 $list->name("new_test_name");
120 is($list->name(), "new_test_name");
122 $list->description("new description");
123 is($list->description(), "new description", "description change");
125 $list->type("accessions");
126 is($list->type(), 'accessions', "list type test");
128 # check if name is actually stored
129 my $list2 = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id });
130 is($list2->name(), "new_test_name", "list name store test");
131 is($list2->description(), "new description", "description store change");
132 is($list2->type(), "accessions", "list type store");
133 is($list2->type_id(), 76451, "list type_id");
134 is($list2->owner(), 41, "list owner");
135 my $error = $list->add_element("bla");
136 ok(!$error, "adding an element to the list");
138 $error = $list->add_element("blabla");
139 ok(!$error, "adding another element");
141 $error = $list->add_element("blabla");
142 ok($error, "adding a duplicate element to the list");
144 is($list->list_size(), 2, "list size check 1");
146 ok($list->exists_element("blabla"), 'exists element check');
148 my $elements = $list->elements();
149 is_deeply($elements, [ 'bla', 'blabla' ], "retrieve list elements after add");
151 $error = $list->remove_element('blabla');
152 ok(!$error, "delete element from list");
154 $elements = $list->elements();
155 is_deeply($elements, [ 'bla' ], "retrieve list elements after delete");
157 is($list->list_size(), 1, "list size check after delete");
158 ok(!$list->exists_element("blabla"), 'exists element after delete');
160 my $lists = CXGN::List::available_lists($t->dbh(), 41);
162 #print STDERR Dumper($lists);
163 @lists_sorted = sort { $a->[0] <=> $b->[0] } @$lists;
164 my @lists_minus_ids;
165 foreach (@lists_sorted){
166     shift(@$_);
167     push @lists_minus_ids, $_;
169 print STDERR Dumper \@lists_minus_ids;
170 print STDERR Dumper $lists_minus_ids[6][6];
171 my $timestamp = $lists_minus_ids[6][6];
172 my $timestamp_mod = $lists_minus_ids[6][7];
173 is_deeply(\@lists_minus_ids, [
174           [
175             'test_stocks',
176             undef,
177             5,
178             76451,
179             'accessions',
180             0,
181               '0001-01-01 00:00:00',
182               '0001-01-01 00:00:00'
183           ],
184           [
185             'accessions_for_solgs_tests',
186             undef,
187             374,
188             76451,
189             'accessions',
190             0,
191               '0001-01-01 00:00:00',
192               '0001-01-01 00:00:00'
193           ],
194           [
195             'accessions_for_trial2',
196             undef,
197             307,
198             76451,
199             'accessions',
200             0,
201               '0001-01-01 00:00:00',
202               '0001-01-01 00:00:00'
203           ],
204           [
205             'selection_acc',
206             undef,
207             20,
208             undef,
209             undef,
210             0,
211               '0001-01-01 00:00:00',
212               '0001-01-01 00:00:00'
213           ],
214           [
215             'desynonymize_test_list',
216             undef,
217             6,
218             76451,
219             'accessions',
220             0,
221               '0001-01-01 00:00:00',
222               '0001-01-01 00:00:00'
223           ],
224           [
225             'traits',
226             undef,
227             10,
228             76455,
229             'traits',
230             0,
231               '0001-01-01 00:00:00',
232               '0001-01-01 00:00:00'
233           ],
234           [
235             'new_test_name',
236             'new description',
237             1,
238             76451,
239             'accessions',
240             0,
241             $timestamp,
242             $timestamp_mod
243           ],
244           [
245             'janedoe_1_public',
246             undef,
247             2,
248             undef,
249             undef,
250             1,
251               '0001-01-01 00:00:00',
252               '0001-01-01 00:00:00'
253           ],
254           [
255             'janedoe_1_private',
256             undef,
257             2,
258             undef,
259             undef,
260             0,
261               '0001-01-01 00:00:00',
262               '0001-01-01 00:00:00'
263           ]
264         ],
265         "check available lists after additions");
269 $error = CXGN::List::delete_list($t->dbh(), 5);
271 my $lists = CXGN::List::available_lists($t->dbh(), 41);
273 #print STDERR Dumper($lists);
275 @lists_sorted = sort { $a->[0] <=> $b->[0] } @$lists;
276 @lists_sorted = sort { $a->[0] <=> $b->[0] } @$lists;
277 my @lists_minus_ids;
278 foreach (@lists_sorted){
279     shift(@$_);
280     push @lists_minus_ids, $_;
282 $timestamp_mod = $lists_minus_ids[5][7];
283 print STDERR Dumper \@lists_minus_ids;
284 is_deeply(\@lists_minus_ids, [
285           [
286             'test_stocks',
287             undef,
288             5,
289             76451,
290             'accessions',
291             0,
292               '0001-01-01 00:00:00',
293               '0001-01-01 00:00:00'
294           ],
295           [
296             'accessions_for_trial2',
297             undef,
298             307,
299             76451,
300             'accessions',
301             0,
302               '0001-01-01 00:00:00',
303               '0001-01-01 00:00:00'
304           ],
305           [
306             'selection_acc',
307             undef,
308             20,
309             undef,
310             undef,
311             0,
312               '0001-01-01 00:00:00',
313               '0001-01-01 00:00:00'
314           ],
315           [
316             'desynonymize_test_list',
317             undef,
318             6,
319             76451,
320             'accessions',
321             0,
322               '0001-01-01 00:00:00',
323               '0001-01-01 00:00:00'
324           ],
325           [
326             'traits',
327             undef,
328             10,
329             76455,
330             'traits',
331             0,
332               '0001-01-01 00:00:00',
333               '0001-01-01 00:00:00'
334           ],
335           [
336             'new_test_name',
337             'new description',
338             1,
339             76451,
340             'accessions',
341             0,
342             $timestamp,
343             $timestamp_mod
344           ],
345           [
346             'janedoe_1_public',
347             undef,
348             2,
349             undef,
350             undef,
351             1,
352               '0001-01-01 00:00:00',
353               '0001-01-01 00:00:00'
354           ],
355           [
356             'janedoe_1_private',
357             undef,
358             2,
359             undef,
360             undef,
361             0,
362               '0001-01-01 00:00:00',
363               '0001-01-01 00:00:00'
364           ]
365         ]
366           , "check available lists after deletion");
370 my $list = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id });
371 my $items = $list->retrieve_elements_with_ids($list_id);
372 $error = $list->update_element_by_id($items->[0]->[0], 'updated name');
373 ok(!$error, 'test update item');
374 $items = $list->retrieve_elements_with_ids($list_id);
375 #print STDERR Dumper $items;
376 my @items_stripped;
377 foreach (@$items){
378     ok($_->[0]);
379     push @items_stripped, $_->[1];
381 is_deeply(\@items_stripped, [
382             'updated name'
383         ], 'check updated list item');
385 my $space1 = $list->add_element(" bla1");
386 ok($list->exists_element("bla1"), 'remove leading space element check');
387 ok(!$list->exists_element(" bla1"), 'leading space removed from element');
390 my $space2 = $list->add_element("bla2 ");
391 ok($list->exists_element("bla2"), 'remove trailing space element check');
392 ok(!$list->exists_element("bla2 "), 'trailing space removed from element');
394 my $space3 = $list->add_element(" bla3 ");
395 ok($list->exists_element("bla3"), 'remove trailing and leading spaces element check');
396 ok(!$list->exists_element(" bla3 "), 'trailing and leading spaces removed from element');
398 my $space4 = $list->add_element("    ");
399 ok($space4 eq "Empty list elements are not allowed", 'element with only spaces cannot be added');
401 #test sort
402 my $list = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id } );
403 ok($list->add_bulk(['item1','item2','item1','item20','item1001','item01','item10','item010','it1num', 'it2num']), 'test add bulk');
404 ok($list->sort_items('ASC'), "sort ascending list");
405 my $list = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id } );
406 $items = $list->elements;
407 print STDERR Dumper $items;
408 is_deeply($items, [
409           'updated name',
410           'bla1',
411           'item1',
412           'item01',
413           'it1num',
414           'bla2',
415           'item2',
416           'it2num',
417           'bla3',
418           'item10',
419           'item010',
420           'item20',
421           'item1001'
422         ], 'check asc ordered items');
424 my $list = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id } );
425 ok($list->sort_items('DESC'), "sort descending list");
426 my $list = CXGN::List->new( { dbh => $t->dbh(), list_id => $list_id } );
427 $items = $list->elements;
428 print STDERR Dumper $items;
429 is_deeply($items, [
430   'item1001',
431   'item20',
432   'item10',
433   'item010',
434   'bla3',
435   'bla2',
436   'item2',
437   'it2num',
438   'bla1',
439   'item1',
440   'item01',
441   'it1num',
442   'updated name'
443 ], 'check desc ordered items');
446 done_testing();