start fixing test for multi cat phenotype upload.
[sgn.git] / t / unit_fixture / CXGN / Trial / Folder.t
blob93aba1508f4f39fd8c52e15025847addcb9b2657
2 use strict;
3 use lib 't/lib';
5 use Test::More;
6 use SGN::Test::Fixture;
8 use Data::Dumper;
10 use CXGN::Trial;
11 use CXGN::Trial::TrialLayout;
12 use CXGN::Trial::TrialDesign;
13 use CXGN::Trial::TrialCreate;
14 use CXGN::Trial::Folder;
16 my $f = SGN::Test::Fixture->new();
17 my $schema = $f->bcs_schema;
19 #create folder with no parent folder
20 my $test_breeding_program = $schema->resultset("Project::Project")->find( { name => 'test' });
21 my $folder = CXGN::Trial::Folder->create({
22   bcs_schema => $schema,
23   parent_folder_id => 0,
24   name => 'Ftest',
25   breeding_program_id => $test_breeding_program->project_id(),
26 });
28 my $F1_id = $folder->folder_id;
29 ok($F1_id, "created folder has id");
31 my $F1_name = $folder->name;
32 ok($F1_name eq 'Ftest', "created folder name is right");
34 my $F1_type = $folder->folder_type;
35 ok($F1_type eq 'folder', "created folder type is right");
37 my $F1_is_folder = $folder->is_folder;
38 ok($F1_is_folder == 1, "created folder is_folder");
40 my $F1_children = $folder->children;
41 ok(scalar(@$F1_children) == 0, "created folder has no children");
43 my $F1_bp = $folder->breeding_program->name;
44 ok($F1_bp eq 'test', "created folder in right breeding program");
46 my $F1_pf = $folder->project_parent;
47 ok(!$F1_pf, "created folder has no parent folder");
50 #confirm build
51 my $folder = CXGN::Trial::Folder->new({
52   bcs_schema => $schema,
53   folder_id => $F1_id
54 });
56 my $F1_folder_id = $folder->folder_id;
57 ok($F1_id == $F1_folder_id, "built folder has id");
59 $F1_name = $folder->name;
60 ok($F1_name eq 'Ftest', "built folder name is right");
62 $F1_type = $folder->folder_type;
63 ok($F1_type eq 'folder', "built folder type is right");
65 $F1_is_folder = $folder->is_folder;
66 ok($F1_is_folder == 1, "built folder is_folder");
68 $F1_children = $folder->children;
69 ok(scalar(@$F1_children) == 0, "built folder has no children");
71 $F1_bp = $folder->breeding_program->name;
72 ok($F1_bp eq 'test', "built folder in right breeding program");
74 $F1_pf = $folder->project_parent->name;
75 ok($F1_pf eq 'test', "built folder has no parent folder, except for the breeding program");
78 #create another folder. this folder will be used as a parent folder to the previous folder
79 my $parent_folder = CXGN::Trial::Folder->create({
80   bcs_schema => $schema,
81   parent_folder_id => 0,
82   name => 'F2',
83   breeding_program_id => $test_breeding_program->project_id(),
84 });
86 my $F2_id = $parent_folder->folder_id();
88 #move folder F1 in to folder F2
89 $parent_folder = CXGN::Trial::Folder->new({
90   bcs_schema => $schema,
91   folder_id => $F2_id,
92 });
94 $folder->associate_parent($F2_id);
96 $F1_bp = $folder->breeding_program->name;
97 ok($F1_bp eq 'test', "folder in right breeding program");
99 $F1_pf = $folder->project_parent->name;
100 ok($F1_pf eq 'F2', "folder has parent folder");
103 #confirm folder build for a trial
104 my $test_trial = $schema->resultset("Project::Project")->find( { name => 'test_trial' });
105 my $trial_folder = CXGN::Trial::Folder->new({
106   bcs_schema => $schema,
107   folder_id => $test_trial->project_id()
110 my $trial_folder_id = $trial_folder->folder_id;
111 ok($trial_folder_id == $test_trial->project_id(), "built trial folder has id");
113 my $trial_folder_name = $trial_folder->name;
114 ok($trial_folder_name eq 'test_trial', "built trial folder name is right");
116 my $trial_folder_type = $trial_folder->folder_type;
117 ok($trial_folder_type eq 'trial', "built trial folder type is right");
119 my $trial_folder_is_folder = $trial_folder->is_folder;
120 ok(!$trial_folder_is_folder, "built trial folder not is_folder");
122 my $trial_folder_children = $trial_folder->children;
123 ok(scalar(@$trial_folder_children) == 0, "built trial folder has no children");
125 my $trial_folder_bp = $trial_folder->breeding_program->name;
126 ok($trial_folder_bp eq 'test', "built trial folder in right breeding program");
128 my $trial_folder_pf = $trial_folder->project_parent->name;
129 ok($trial_folder_pf eq 'test', "built trial folder has parent folder");
132 #place test_trial into folder F2
133 $trial_folder->associate_parent($F2_id);
135 $trial_folder_bp = $trial_folder->breeding_program->name;
136 ok($trial_folder_bp eq 'test', "trial folder in right breeding program");
138 $trial_folder_pf = $trial_folder->project_parent->name;
139 ok($trial_folder_pf eq 'F2', "trial folder has parent folder");
141 $parent_folder = CXGN::Trial::Folder->new({
142   bcs_schema => $schema,
143   folder_id => $F2_id,
146 my $folder = CXGN::Trial::Folder->new({
147   bcs_schema => $schema,
148   folder_id => $F1_id
151 my $parent_folder_children = $parent_folder->children();
152 ok(scalar(@$parent_folder_children) == 2, "folder has 2 children");
153 ok(@$parent_folder_children[0]->name eq 'Ftest', "folder has child named 'F1'");
154 ok(@$parent_folder_children[1]->name eq 'test_trial', "folder has child named 'test_trial'");
156 my $folder_children = $folder->children();
157 ok(scalar(@$folder_children) == 0, "folder still has no children");
160 #move test_trial from folder F2 to F1
161 $trial_folder->associate_parent($folder->folder_id());
163 my $trial_folder = CXGN::Trial::Folder->new({
164   bcs_schema => $schema,
165   folder_id => $test_trial->project_id()
168 $trial_folder_bp = $trial_folder->breeding_program->name;
169 ok($trial_folder_bp eq 'test', "trial folder in right breeding program");
171 $trial_folder_pf = $trial_folder->project_parent->name;
172 ok($trial_folder_pf eq 'Ftest', "trial folder has parent folder");
174 $parent_folder = CXGN::Trial::Folder->new({
175   bcs_schema => $schema,
176   folder_id => $F2_id,
179 my $folder = CXGN::Trial::Folder->new({
180   bcs_schema => $schema,
181   folder_id => $F1_id
184 my $parent_folder_children = $parent_folder->children();
185 ok(scalar(@$parent_folder_children) == 1, "folder has 1 child");
186 ok(@$parent_folder_children[0]->name eq 'Ftest', "folder has child named 'F1'");
188 my $folder_children = $folder->children();
189 ok(scalar(@$folder_children) == 1, "folder has 1 child");
190 ok(@$folder_children[0]->name eq 'test_trial', "folder has child named 'test_trial'");
193 #create a third folder F3, to test moving F1 to F3
194 my $folder3 = CXGN::Trial::Folder->create({
195   bcs_schema => $schema,
196   parent_folder_id => $F2_id,
197   name => 'F3',
198   breeding_program_id => $test_breeding_program->project_id(),
201 my $folder3_id = $folder3->folder_id;
202 ok($folder3_id, "created folder3 has id");
204 my $folder3_name = $folder3->name;
205 ok($folder3_name eq 'F3', "created folder3 name is right");
207 my $folder3_type = $folder3->folder_type;
208 ok($folder3_type eq 'folder', "created folder3 type is right");
210 my $folder3_is_folder = $folder3->is_folder;
211 ok($folder3_is_folder == 1, "created folder3 is_folder");
213 my $folder3_children = $folder3->children;
214 ok(scalar(@$folder3_children) == 0, "created folder3 has no children");
216 my $folder3_bp = $folder3->breeding_program->name;
217 ok($folder3_bp eq 'test', "created folder3 in right breeding program");
219 my $folder3_pf = $folder3->project_parent->name;
220 ok($folder3_pf eq 'F2', "created folder3 has parent folder F2");
223 #move folder F1 into F3
224 $folder->associate_parent($folder3_id);
226 $folder3 = CXGN::Trial::Folder->new({
227   bcs_schema => $schema,
228   folder_id => $folder3_id
231 $folder3_name = $folder3->name;
232 ok($folder3_name eq 'F3', "folder3 name is right");
234 $folder3_type = $folder3->folder_type;
235 ok($folder3_type eq 'folder', "folder3 type is right");
237 $folder3_is_folder = $folder3->is_folder;
238 ok($folder3_is_folder == 1, "folder3 is_folder");
240 $folder3_children = $folder3->children;
241 ok(scalar(@$folder3_children) == 1, "folder3 has 1 child");
242 ok(@$folder3_children[0]->name eq 'Ftest', "folder3 has child named 'F1'");
244 $folder3_bp = $folder3->breeding_program->name;
245 ok($folder3_bp eq 'test', "folder3 in right breeding program");
247 $folder3_pf = $folder3->project_parent->name;
248 ok($folder3_pf eq 'F2', "folder3 has parent folder F2");
250 my $folder = CXGN::Trial::Folder->new({
251   bcs_schema => $schema,
252   folder_id => $F1_id
255 $folder3_name = $folder->name;
256 ok($folder3_name eq 'Ftest', "folder name is right");
258 $folder3_type = $folder->folder_type;
259 ok($folder3_type eq 'folder', "folder type is right");
261 $folder3_is_folder = $folder->is_folder;
262 ok($folder3_is_folder == 1, "folder is_folder");
264 $folder3_children = $folder->children;
265 ok(scalar(@$folder3_children) == 1, "folder has 1 child");
266 ok(@$folder3_children[0]->name eq 'test_trial', "folder has child named 'F1'");
268 $folder3_bp = $folder->breeding_program->name;
269 ok($folder3_bp eq 'test', "folder in right breeding program");
271 $folder3_pf = $folder->project_parent->name;
272 ok($folder3_pf eq 'F3', "folder has parent folder F3");
275 #Structure at this point should be F2->F3->F1->test_trial
277 #Make folder F1 parent of folder F3
278 my $folder = CXGN::Trial::Folder->new({
279   bcs_schema => $schema,
280   folder_id => $F1_id
283 my $folder3 = CXGN::Trial::Folder->new({
284   bcs_schema => $schema,
285   folder_id => $folder3_id
288 $folder3->associate_parent($folder->folder_id());
290 $folder3 = CXGN::Trial::Folder->new({
291   bcs_schema => $schema,
292   folder_id => $folder3_id
295 $folder3_name = $folder3->name;
296 ok($folder3_name eq 'F3', "folder3 name is right");
298 $folder3_type = $folder3->folder_type;
299 ok($folder3_type eq 'folder', "folder3 type is right");
301 $folder3_is_folder = $folder3->is_folder;
302 ok($folder3_is_folder == 1, "folder3 is_folder");
304 $folder3_children = $folder3->children;
305 ok(scalar(@$folder3_children) == 0, "folder3 has 0 child");
307 $folder3_bp = $folder3->breeding_program->name;
308 ok($folder3_bp eq 'test', "folder3 in right breeding program");
310 $folder3_pf = $folder3->project_parent->name;
311 ok($folder3_pf eq 'Ftest', "folder3 has parent folder F1");
313 my $folder = CXGN::Trial::Folder->new({
314   bcs_schema => $schema,
315   folder_id => $F1_id
318 my $folder_name = $folder->name;
319 ok($folder_name eq 'Ftest', "folder name is right");
321 my $folder_type = $folder->folder_type;
322 ok($folder_type eq 'folder', "folder type is right");
324 my $folder_is_folder = $folder->is_folder;
325 ok($folder_is_folder == 1, "folder is_folder");
327 my $folder_children = $folder->children;
328 ok(scalar(@$folder_children) == 2, "folder has 2 child");
329 #print STDERR Dumper @$folder_children[0]->name;
330 #print STDERR Dumper @$folder_children[1]->name;
331 ok(@$folder_children[1]->name eq 'test_trial', "folder has child named 'test_trial'");
332 ok(@$folder_children[0]->name eq 'F3', "folder has child named 'F3'");
334 my $folder_bp = $folder->breeding_program->name;
335 ok($folder_bp eq 'test', "folder in right breeding program");
337 my $folder_pf = $folder->project_parent->name;
338 ok($folder_pf eq 'test', "folder has no parent except for bp");
340 my $folder2 = CXGN::Trial::Folder->new({
341   bcs_schema => $schema,
342   folder_id => $F2_id,
345 my $folder2_name = $folder2->name;
346 ok($folder2_name eq 'F2', "folder name is right");
348 my $folder2_type = $folder2->folder_type;
349 ok($folder2_type eq 'folder', "folder type is right");
351 my $folder2_is_folder = $folder2->is_folder;
352 ok($folder2_is_folder == 1, "folder is_folder");
354 my $folder2_children = $folder2->children;
355 ok(scalar(@$folder2_children) == 0, "folder has 0 child");
357 my $folder2_bp = $folder2->breeding_program->name;
358 ok($folder2_bp eq 'test', "folder in right breeding program");
360 my $folder2_pf = $folder2->project_parent->name;
361 ok($folder2_pf eq 'test', "folder has no parent except for bp");
364 #move test_trial to no folder
365 $trial_folder->associate_parent(0);
367 my $trial_folder = CXGN::Trial::Folder->new({
368   bcs_schema => $schema,
369   folder_id => $test_trial->project_id()
372 $trial_folder_bp = $trial_folder->breeding_program->name;
373 ok($trial_folder_bp eq 'test', "trial folder in right breeding program");
375 $trial_folder_pf = $trial_folder->project_parent->name;
376 ok($trial_folder_pf eq 'test', "trial folder has no parent folder, except for the breeding program");
378 $folder2 = CXGN::Trial::Folder->new({
379   bcs_schema => $schema,
380   folder_id => $F2_id,
383 $folder = CXGN::Trial::Folder->new({
384   bcs_schema => $schema,
385   folder_id => $F1_id
388 $folder3 = CXGN::Trial::Folder->new({
389   bcs_schema => $schema,
390   folder_id => $folder3_id
393 my $folder2_children = $folder2->children();
394 ok(scalar(@$folder2_children) == 0, "folder has 0 child");
396 my $folder3_children = $folder3->children();
397 ok(scalar(@$folder3_children) == 0, "folder has 0 child");
399 my $folder_children = $folder->children();
400 ok(scalar(@$folder_children) == 1, "folder has 1 child");
401 ok(@$folder_children[0]->name eq 'F3', "folder has child named 'F3'");
404 #at this point structure is 
405 #test->
406 #    ->F2
407 #    ->F1->F3
408 #    ->test_trial
410 #move F1 into F2
411 $folder2 = CXGN::Trial::Folder->new({
412   bcs_schema => $schema,
413   folder_id => $F2_id,
416 $folder = CXGN::Trial::Folder->new({
417   bcs_schema => $schema,
418   folder_id => $F1_id
421 $folder->associate_parent($folder2->folder_id);
424 #move test_trial into F3
425 $trial_folder = CXGN::Trial::Folder->new({
426   bcs_schema => $schema,
427   folder_id => $test_trial->project_id()
430 $folder3 = CXGN::Trial::Folder->new({
431   bcs_schema => $schema,
432   folder_id => $folder3_id
435 $trial_folder->associate_parent($folder3->folder_id());
437 #structure is 
438 #test->F2->F1->F3->test_trial
440 #move F2 into F3
441 $folder2->associate_parent($folder3->folder_id);
443 #structure is
444 #test->F3->
445 #        ->test_trial
446 #        ->F2->F1
448 my $folder = CXGN::Trial::Folder->new({
449   bcs_schema => $schema,
450   folder_id => $folder3_id
453 my $folder_name = $folder->name;
454 ok($folder_name eq 'F3', "folder name is right");
456 my $folder_type = $folder->folder_type;
457 ok($folder_type eq 'folder', "folder type is right");
459 my $folder_is_folder = $folder->is_folder;
460 ok($folder_is_folder == 1, "folder is_folder");
462 my $folder_children = $folder->children;
463 ok(scalar(@$folder_children) == 2, "folder has 2 child");
464 #print STDERR Dumper @$folder_children[0]->name;
465 #print STDERR Dumper @$folder_children[1]->name;
466 ok(@$folder_children[1]->name eq 'test_trial', "folder has child named 'test_trial'");
467 ok(@$folder_children[0]->name eq 'F2', "folder has child named 'F2'");
469 my $folder_bp = $folder->breeding_program->name;
470 ok($folder_bp eq 'test', "folder in right breeding program");
472 my $folder_pf = $folder->project_parent->name;
473 ok($folder_pf eq 'test', "folder has no parent except for bp");
475 my $folder = CXGN::Trial::Folder->new({
476   bcs_schema => $schema,
477   folder_id => $F2_id
480 my $folder_name = $folder->name;
481 ok($folder_name eq 'F2', "folder name is right");
483 my $folder_type = $folder->folder_type;
484 ok($folder_type eq 'folder', "folder type is right");
486 my $folder_is_folder = $folder->is_folder;
487 ok($folder_is_folder == 1, "folder is_folder");
489 my $folder_children = $folder->children;
490 ok(scalar(@$folder_children) == 1, "folder has 1 child");
491 #print STDERR Dumper @$folder_children[0]->name;
492 ok(@$folder_children[0]->name eq 'Ftest', "folder has child named 'F1'");
494 my $folder_bp = $folder->breeding_program->name;
495 ok($folder_bp eq 'test', "folder in right breeding program");
497 my $folder_pf = $folder->project_parent->name;
498 ok($folder_pf eq 'F3', "folder has parent F3");
500 my $folder = CXGN::Trial::Folder->new({
501   bcs_schema => $schema,
502   folder_id => $F1_id
505 my $folder_name = $folder->name;
506 ok($folder_name eq 'Ftest', "folder name is right");
508 my $folder_type = $folder->folder_type;
509 ok($folder_type eq 'folder', "folder type is right");
511 my $folder_is_folder = $folder->is_folder;
512 ok($folder_is_folder == 1, "folder is_folder");
514 my $folder_children = $folder->children;
515 ok(scalar(@$folder_children) == 0, "folder has 0 child");
517 my $folder_bp = $folder->breeding_program->name;
518 ok($folder_bp eq 'test', "folder in right breeding program");
520 my $folder_pf = $folder->project_parent->name;
521 ok($folder_pf eq 'F2', "folder has parent F2");
524 # Delete a folder
526 my $folder = CXGN::Trial::Folder->new({
527   bcs_schema => $schema,
528   folder_id => $F2_id
531 my $delete_folder = $folder->delete_folder();
532 ok(!$delete_folder, "could not delete folder because of children.");
534 my $folder = CXGN::Trial::Folder->new({
535   bcs_schema => $schema,
536   folder_id => $F1_id
539 my $delete_folder = $folder->delete_folder();
540 ok($delete_folder == 1, "folder deleted.");
544 my $folder4 = CXGN::Trial::Folder->create({
545   bcs_schema => $schema,
546   name => 'F4',
547   breeding_program_id => $test_breeding_program->project_id(),
548   folder_for_trials => 1,
549   folder_for_crosses => 1
551 my $F4_id = $folder4->folder_id;
553 $folder = CXGN::Trial::Folder->new({
554   bcs_schema => $schema,
555   folder_id => $F4_id
557 ok($folder->folder_for_trials == 1, "check create folder with folder_for_trials");
558 ok($folder->folder_for_crosses == 1, "check create folder with folder_for_crosses");
560 $folder->set_folder_content_type('folder_for_trials', 0);
561 $folder->set_folder_content_type('folder_for_crosses', 0);
563 $folder = CXGN::Trial::Folder->new({
564   bcs_schema => $schema,
565   folder_id => $F4_id
567 ok(!$folder->folder_for_trials, "check unset folder_for_trials");
568 ok(!$folder->folder_for_crosses, "check unset folder_for_crosses");
570 my $delete_folder = $folder->delete_folder();
571 ok($delete_folder == 1, "folder deleted.");
573 done_testing();