Merge pull request #5191 from solgenomics/topic/quality_control
[sgn.git] / t / unit_fixture / CXGN / Trial / EditFieldMap.t
blob989639054ff57e66cbc587149456fdc892ac2d61
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 BEGIN {use_ok('CXGN::Trial::FieldMap');}
17 BEGIN {use_ok('CXGN::Trial');}
18 ok(my $chado_schema = $fix->bcs_schema);
19 ok(my $phenome_schema = $fix->phenome_schema);
20 ok(my $dbh = $fix->dbh);
22 $chado_schema->txn_begin();
23 # create a location for the trial
24 ok(my $trial_location = "test_location_for_trial");
25 ok(my $location = $chado_schema->resultset('NaturalDiversity::NdGeolocation')
26    ->new({
27     description => $trial_location,
28          }));
29 ok($location->insert());
31 # create stocks for the trial
32 ok(my $accession_cvterm = $chado_schema->resultset("Cv::Cvterm")
33    ->create_with({
34        name   => 'accession',
35        cv     => 'stock_type',
37                  }));
38 my @stock_names;
39 for (my $i = 1; $i <= 10; $i++) {
40     push(@stock_names, "test_stock_for_trial".$i);
43 # create a location for the trial
44 ok(my $trial_location = "test_location_for_trial");
45 ok(my $location = $chado_schema->resultset('NaturalDiversity::NdGeolocation')
46    ->new({
47     description => $trial_location,
48          }));
49 ok($location->insert());
51 # create stocks for the trial
52 ok(my $accession_cvterm = $chado_schema->resultset("Cv::Cvterm")
53    ->create_with({
54        name   => 'accession',
55        cv     => 'stock_type',
57                  }));
58 my @stock_names;
59 for (my $i = 1; $i <= 10; $i++) {
60     push(@stock_names, "test_stock_for_fieldmap_trial".$i);
63 ok(my $organism = $chado_schema->resultset("Organism::Organism")
64    ->find_or_create( {
65        genus => 'Test_genus',
66        species => 'Test_genus test_species',
67                      }, ));
69 # create some test stocks
70 foreach my $stock_name (@stock_names) {
71     my $accession_stock = $chado_schema->resultset('Stock::Stock')
72         ->create({
73             organism_id => $organism->organism_id,
74             name       => $stock_name,
75             uniquename => $stock_name,
76             type_id     => $accession_cvterm->cvterm_id,
77                  });
80 ok(my $trial_design = CXGN::Trial::TrialDesign->new(), "create trial design object");
81 ok($trial_design->set_trial_name("new_test_trial_fieldmap_name"), "set trial name");
82 ok($trial_design->set_stock_list(\@stock_names), "set stock list");
83 ok($trial_design->set_plot_start_number(1), "set plot start number");
84 ok($trial_design->set_plot_number_increment(1), "set plot increment");
85 ok($trial_design->set_number_of_blocks(2), "set block number");
86 ok($trial_design->set_design_type("RCBD"), "set design type");
87 ok($trial_design->set_plot_layout_format("serpentine"), "set plot layout format");
88 ok($trial_design->calculate_design(), "calculate design");
89 ok(my $design = $trial_design->get_design(), "retrieve design");
91 ok(my $trial_create = CXGN::Trial::TrialCreate->new({
92     chado_schema => $chado_schema,
93     dbh => $dbh,
94     owner_id => 41,
95     design => $design,
96     program => "test",
97     trial_year => "2015",
98     trial_description => "test description",
99     trial_location => "test_location_for_trial",
100     trial_name => "new_test_trial_fieldmap_name",
101     design_type => "RCBD",
102     operator => "janedoe"
103                                                     }), "create trial object");
105 my $save = $trial_create->save_trial();
106 ok($save->{'trial_id'}, "save trial");
108 ok(my $trial_lookup = CXGN::Trial::TrialLookup->new({
109     schema => $chado_schema,
110     trial_name => "new_test_trial_fieldmap_name",
111                                                     }), "create trial lookup object");
112 ok(my $trial = $trial_lookup->get_trial());
113 ok(my $trial_id = $trial->project_id());
114 ok(my $trial_layout = CXGN::Trial::TrialLayout->new({
115     schema => $chado_schema,
116     trial_id => $trial_id,
117     experiment_type => 'field_layout'
118                                                     }), "create trial layout object");
120 #replace trial accession
121 ok(my $old_accession = "test_stock_for_fieldmap_trial1");
122 ok(my $new_accession = "test_stock_for_fieldmap_trial2");
123 ok(my $stock_id = $chado_schema->resultset('Stock::Stock')->find({'uniquename' => $old_accession})->stock_id(), "get stock id");
125 my $replace_accession_fieldmap = CXGN::Trial::FieldMap->new({
126   bcs_schema => $chado_schema,
127   trial_id => $trial_id,
128   old_accession_id => $stock_id,
129   new_accession => $new_accession,
131 ok(!$replace_accession_fieldmap->replace_trial_stock_fieldMap($new_accession, $stock_id), "replace trial accession");
134 my $trial = CXGN::Trial->new( {
135   bcs_schema => $chado_schema,
136   trial_id => $trial_id
137  });
139 #replace plot accession
140 ok(my @data = $trial->get_plots(), "get plots");
141 ok(my $old_plot_id = $data[0]->[0][0]);
142 print STDERR Dumper($old_plot_id);
143 my $plot_of_type_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'plot_of', 'stock_relationship')->cvterm_id();
144 my $accession_id = $chado_schema->resultset('Stock::Stock')->find({'uniquename' => $new_accession})->stock_id(), "get stock id";
146 my $replace_plot_accession_fieldmap = CXGN::Trial::FieldMap->new({
147   bcs_schema => $chado_schema,
148   trial_id => $trial_id,
151 ok(!$replace_plot_accession_fieldmap->replace_plot_accession_fieldMap($old_plot_id, $accession_id, $plot_of_type_id), "replace plot accession");
152 # accessions substitution
153 ok(my $plot_1_id = $data[0]->[0][0]);
154 ok(my $plot_2_id = $data[0]->[1][0]);
155 ok(my $accession_1 = "test_stock_for_fieldmap_trial1");
156 ok(my $accession_2 = "test_stock_for_fieldmap_trial2");
157 $chado_schema->txn_rollback();
158 # my $fieldmap = CXGN::Trial::FieldMap->new({
159 #   bcs_schema => $chado_schema,
160 #   trial_id => $trial_id,
161 #   first_plot_selected => $plot_1_id,
162 #   second_plot_selected => $plot_2_id,
163 #   first_accession_selected => $accession_1,
164 #   second_accession_selected => $accession_2,
165 # });
167 # ok(!$fieldmap->substitute_accession_fieldmap(), "substituting plots accessions");
169 done_testing();