start fixing test for multi cat phenotype upload.
[sgn.git] / t / unit_fixture / CXGN / Trial / MultiTrialCreate.t
blobbeeca888269d8609788a621552891e9b72d48f36
1 use strict;
3 use Test::More;
4 use lib 't/lib';
5 use SGN::Test::Fixture;
6 use Data::Dumper;
8 my $fix = SGN::Test::Fixture->new();
10 is(ref($fix->config()), "HASH", 'hashref check');
12 BEGIN {use_ok('CXGN::Trial::TrialCreate');}
13 BEGIN {use_ok('CXGN::Trial::TrialLayout');}
14 BEGIN {use_ok('CXGN::Trial::TrialDesign');}
15 BEGIN {use_ok('CXGN::Trial::TrialLookup');}
16 ok(my $chado_schema = $fix->bcs_schema);
17 ok(my $phenome_schema = $fix->phenome_schema);
18 ok(my $dbh = $fix->dbh);
20 # create locations for the trial
21 my @multi_location;
22 for (my $i = 1; $i <= 2; $i++) {
23   ok(my $trial_location = "test_location_for_multi_trial".$i);
24   push @multi_location, $trial_location;
27 foreach my $multi_trial_loc (@multi_location) {
28   ok(my $location = $chado_schema->resultset('NaturalDiversity::NdGeolocation')
29      ->new({
30       description => $multi_trial_loc,
31          }));
32   ok($location->insert());
35 # create stocks for the trial
36 ok(my $accession_cvterm = $chado_schema->resultset("Cv::Cvterm")
37    ->create_with({
38        name   => 'accession',
39        cv     => 'stock_type',
41                  }));
43 my @stock_names;
44 for (my $i = 1; $i <= 10; $i++) {
45     push(@stock_names, "test_stock_for_multi_trial".$i);
48 ok(my $organism = $chado_schema->resultset("Organism::Organism")
49    ->find_or_create( {
50        genus => 'Test_genus',
51        species => 'Test_genus test_species',
52                      }, ));
54 # create some test stocks
55 foreach my $stock_name (@stock_names) {
56     my $accession_stock = $chado_schema->resultset('Stock::Stock')
57         ->create({
58             organism_id => $organism->organism_id,
59             name       => $stock_name,
60             uniquename => $stock_name,
61             type_id     => $accession_cvterm->cvterm_id,
62                  });
65 #create multilocation trial
66 my @multi_design;
67 my $design_index = 0;
68 foreach my $trial_location (@multi_location) {
69   ok(my $trial_design = CXGN::Trial::TrialDesign->new(), "create trial design object");
70   ok($trial_design->set_trial_name("test_multi_trial_name".$trial_location), "set trial name");
71   ok($trial_design->set_stock_list(\@stock_names), "set stock list");
72   ok($trial_design->set_plot_start_number(1), "set plot start number");
73   ok($trial_design->set_plot_number_increment(1), "set plot increment");
74   ok($trial_design->set_number_of_blocks(2), "set block number");
75   ok($trial_design->set_design_type("RCBD"), "set design type");
76   ok($trial_design->calculate_design(), "calculate design");
77   ok(my $design = $trial_design->get_design(), "retrieve design");
78   push @multi_design, $design;
80     ok(my $trial_create = CXGN::Trial::TrialCreate->new({
81         chado_schema => $chado_schema,
82         dbh => $dbh,
83         owner_id => 41,
84         design => $multi_design[$design_index],
85         program => "test",
86         trial_year => "2016",
87         trial_description => "multilocation test description",
88         #trial_location => "test_location_for_trial",
89         trial_location => $trial_location,
90         trial_name => "test_multi_trial_name".$trial_location,
91         design_type => "RCBD",
92         operator => "janedoe"
93                                                     }), "create trial object");
95     my $save = $trial_create->save_trial();
96     ok($save->{'trial_id'}, "save trial");
98 $design_index++;
100 ok(my $trial_lookup = CXGN::Trial::TrialLookup->new({
101     schema => $chado_schema,
102     trial_name => "test_multi_trial_name".$trial_location,
103                                                     }), "create trial lookup object");
104 ok(my $trial = $trial_lookup->get_trial());
105 ok(my $trial_id = $trial->project_id());
106 ok(my $trial_layout = CXGN::Trial::TrialLayout->new({
107     schema => $chado_schema,
108     trial_id => $trial_id,
109     experiment_type => 'field_layout'
110                                                     }), "create trial layout object");
111 print STDERR Dumper($trial_layout->get_design());
113 ok(my $accession_names = $trial_layout->get_accession_names(), "retrieve accession names2");
115 my %stocks = map { $_ => 1 } @stock_names;
117 foreach my $acc (@$accession_names) {
118     ok(exists($stocks{$acc->{accession_name}}), "check accession names $acc->{accession_name}");
123 done_testing();