start fixing test for multi cat phenotype upload.
[sgn.git] / t / unit_fixture / CXGN / Uploading / TrialUpload.t
blob3fcd413d3fb85ebc275599c7d651ad892d884fff
1 use strict;
2 use lib 't/lib';
4 use Test::More;
5 use SGN::Test::Fixture;
6 use SimulateC;
7 use CXGN::UploadFile;
8 use CXGN::Trial;
9 use CXGN::Trial::TrialCreate;
10 use CXGN::Trial::ParseUpload;
11 use SGN::Model::Cvterm;
12 use Data::Dumper;
13 use CXGN::Location::LocationLookup;
14 use CXGN::Stock::StockLookup;
15 use CXGN::List;
16 use CXGN::Trial::TrialDesign;
17 use CXGN::BreedersToolbox::Projects;
18 use CXGN::Genotype::StoreGenotypingProject;
19 use DateTime;
20 use Test::WWW::Mechanize;
21 use LWP::UserAgent;
22 use JSON;
23 use Spreadsheet::Read;
24 use Text::CSV;
26 my $f = SGN::Test::Fixture->new();
28 for my $extension ("xls", "xlsx") {
30         my $c = SimulateC->new({ dbh => $f->dbh(),
31                 bcs_schema               => $f->bcs_schema(),
32                 metadata_schema          => $f->metadata_schema(),
33                 phenome_schema           => $f->phenome_schema(),
34                 sp_person_id             => 41 });
36         #######################################
37         #Find out table counts before adding anything, so that changes can be compared
39         my $pre_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
40         my $pre_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
41         my $pre_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
42         my $pre_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
43         my $pre_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
44         my $pre_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
45         my $pre_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
46         my $pre_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
47         my $pre_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
48         my $pre_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
51         #First Upload Excel Trial File
54         my %upload_metadata;
55         my $file_name = "t/data/trial/trial_layout_example.$extension";
56         my $time = DateTime->now();
57         my $timestamp = $time->ymd() . "_" . $time->hms();
59         #Test archive upload file
60         my $uploader = CXGN::UploadFile->new({
61                 tempfile         => $file_name,
62                 subdirectory     => 'temp_trial_upload',
63                 archive_path     => '/tmp',
64                 archive_filename => "trial_layout_example.$extension",
65                 timestamp        => $timestamp,
66                 user_id          => 41, #janedoe in fixture
67                 user_role        => 'curator'
68         });
70         ## Store uploaded temporary file in archive
71         my $archived_filename_with_path = $uploader->archive();
72         my $md5 = $uploader->get_md5($archived_filename_with_path);
73         ok($archived_filename_with_path);
74         ok($md5);
76         $upload_metadata{'archived_file'} = $archived_filename_with_path;
77         $upload_metadata{'archived_file_type'} = "trial upload file";
78         $upload_metadata{'user_id'} = $c->sp_person_id;
79         $upload_metadata{'date'} = "2014-02-14_09:10:11";
82         #parse uploaded file with wrong plugin
83         my $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path);
84         $parser->load_plugin('ParseIGDFile');
85         my $parsed_data = $parser->parse();
86         ok(!$parsed_data, "Check if parse validate igd file fails for excel");
87         ok($parser->has_parse_errors(), "Check that parser errors occur");
89         #parse uploaded file with appropriate plugin
90         $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path);
91         $parser->load_plugin('TrialExcelFormat');
92         $parsed_data = $parser->parse()->{'design'};
93         ok($parsed_data, "Check if parse validate excel file works");
94         ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
96         #print STDERR Dumper $parsed_data;
98         my $parsed_data_check = {
99                 '1' => {
100                         'plot_name'    => 'plot_name1',
101                         'stock_name'   => 'test_accession1',
102                         'col_number'   => '1',
103                         'is_a_control' => 0,
104                         'rep_number'   => '1',
105                         'block_number' => '1',
106                         'range_number' => '1',
107                         'row_number'   => '1',
108                         'plot_number'  => '1'
109                 },
110                 '6' => {
111                         'rep_number'   => '2',
112                         'is_a_control' => 0,
113                         'block_number' => '2',
114                         'plot_name'    => 'plot_name6',
115                         'stock_name'   => 'test_accession3',
116                         'col_number'   => '2',
117                         'range_number' => '2',
118                         'row_number'   => '2',
119                         'plot_number'  => '6'
120                 },
121                 '7' => {
122                         'range_number' => '2',
123                         'row_number'   => '3',
124                         'plot_number'  => '7',
125                         'plot_name'    => 'plot_name7',
126                         'stock_name'   => 'test_accession4',
127                         'col_number'   => '2',
128                         'rep_number'   => '1',
129                         'is_a_control' => 0,
130                         'block_number' => '2'
131                 },
132                 '4' => {
133                         'range_number' => '1',
134                         'plot_number'  => '4',
135                         'row_number'   => '4',
136                         'is_a_control' => 0,
137                         'rep_number'   => '2',
138                         'block_number' => '1',
139                         'plot_name'    => 'plot_name4',
140                         'col_number'   => '1',
141                         'stock_name'   => 'test_accession2'
142                 },
143                 '8' => {
144                         'range_number' => '2',
145                         'row_number'   => '4',
146                         'plot_number'  => '8',
147                         'plot_name'    => 'plot_name8',
148                         'stock_name'   => 'test_accession4',
149                         'col_number'   => '2',
150                         'rep_number'   => '2',
151                         'is_a_control' => 0,
152                         'block_number' => '2'
153                 },
154                 '2' => {
155                         'range_number' => '1',
156                         'plot_number'  => '2',
157                         'row_number'   => '2',
158                         'plot_name'    => 'plot_name2',
159                         'col_number'   => '1',
160                         'stock_name'   => 'test_accession1',
161                         'is_a_control' => 0,
162                         'rep_number'   => '2',
163                         'block_number' => '1'
164                 },
165                 '5' => {
166                         'range_number' => '2',
167                         'row_number'   => '1',
168                         'plot_number'  => '5',
169                         'plot_name'    => 'plot_name5',
170                         'stock_name'   => 'test_accession3',
171                         'col_number'   => '2',
172                         'is_a_control' => 0,
173                         'rep_number'   => '1',
174                         'block_number' => '2'
175                 },
176                 '3' => {
177                         'stock_name'   => 'test_accession2',
178                         'col_number'   => '1',
179                         'plot_name'    => 'plot_name3',
180                         'block_number' => '1',
181                         'is_a_control' => 0,
182                         'rep_number'   => '1',
183                         'row_number'   => '3',
184                         'plot_number'  => '3',
185                         'range_number' => '1'
186                 }
187         };
189         is_deeply($parsed_data, $parsed_data_check, 'check trial excel parse data');
191         my $trial_create = CXGN::Trial::TrialCreate
192                 ->new({
193                 chado_schema      => $c->bcs_schema(),
194                 dbh               => $c->dbh(),
195                 owner_id          => 41,
196                 trial_year        => "2016",
197                 trial_description => "Trial Upload Test",
198                 trial_location    => "test_location",
199                 trial_name        => "Trial_upload_test",
200                 design_type       => "RCBD",
201                 design            => $parsed_data,
202                 program           => "test",
203                 upload_trial_file => $archived_filename_with_path,
204                 operator          => "janedoe"
205         });
207         my $save = $trial_create->save_trial();
209         ok($save->{'trial_id'}, "check that trial_create worked");
210         my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
211         ok($project_name == "Trial_upload_test", "check that trial_create really worked");
213         my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
214         ok($project_desc == "Trial Upload Test", "check that trial_create really worked");
216         my $post_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
217         my $post1_project_diff = $post_project_count - $pre_project_count;
218         print STDERR "Project: " . $post1_project_diff . "\n";
219         ok($post1_project_diff == 1, "check project table after upload excel trial");
221         my $post_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
222         my $post1_nd_experiment_diff = $post_nd_experiment_count - $pre_nd_experiment_count;
223         print STDERR "NdExperiment: " . $post1_nd_experiment_diff . "\n";
224         ok($post1_nd_experiment_diff == 1, "check ndexperiment table after upload excel trial");
226         my $post_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
227         my $post1_nd_experiment_proj_diff = $post_nd_experiment_proj_count - $pre_nd_experiment_proj_count;
228         print STDERR "NdExperimentProject: " . $post1_nd_experiment_proj_diff . "\n";
229         ok($post1_nd_experiment_proj_diff == 1, "check ndexperimentproject table after upload excel trial");
231         my $post_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
232         my $post1_nd_experimentprop_diff = $post_nd_experimentprop_count - $pre_nd_experimentprop_count;
233         print STDERR "NdExperimentprop: " . $post1_nd_experimentprop_diff . "\n";
234         ok($post1_nd_experimentprop_diff == 0, "check ndexperimentprop table after upload excel trial");
236         my $post_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
237         my $post1_project_prop_diff = $post_project_prop_count - $pre_project_prop_count;
238         print STDERR "Projectprop: " . $post1_project_prop_diff . "\n";
239         ok($post1_project_prop_diff == 4, "check projectprop table after upload excel trial");
241         my $post_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
242         my $post1_stock_diff = $post_stock_count - $pre_stock_count;
243         print STDERR "Stock: " . $post1_stock_diff . "\n";
244         ok($post1_stock_diff == 8, "check stock table after upload excel trial");
246         my $post_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
247         my $post1_stock_prop_diff = $post_stock_prop_count - $pre_stock_prop_count;
248         print STDERR "Stockprop: " . $post1_stock_prop_diff . "\n";
249         ok($post1_stock_prop_diff == 48, "check stockprop table after upload excel trial");
251         my $post_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
252         my $post1_stock_relationship_diff = $post_stock_relationship_count - $pre_stock_relationship_count;
253         print STDERR "StockRelationship: " . $post1_stock_relationship_diff . "\n";
254         ok($post1_stock_relationship_diff == 8, "check stockrelationship table after upload excel trial");
256         my $post_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
257         my $post1_nd_experiment_stock_diff = $post_nd_experiment_stock_count - $pre_nd_experiment_stock_count;
258         print STDERR "NdExperimentStock: " . $post1_nd_experiment_stock_diff . "\n";
259         ok($post1_nd_experiment_stock_diff == 8, "check ndexperimentstock table after upload excel trial");
261         my $post_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
262         my $post1_project_relationship_diff = $post_project_relationship_count - $pre_project_relationship_count;
263         print STDERR "ProjectRelationship: " . $post1_project_relationship_diff . "\n";
264         ok($post1_project_relationship_diff == 1, "check projectrelationship table after upload excel trial");
267         #Upload IGD Trial File
269         $file_name = 't/data/genotype_trial_upload/CASSAVA_GS_74Template.csv';
270         #parse uploaded file with wrong plugin
271         $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $file_name);
272         $parser->load_plugin('TrialExcelFormat');
273         my $rtn = $parser->parse();
274         $parsed_data = $rtn->{'design'};
275         ok(!$parsed_data, "Check if parse validate excel fails for igd parser");
276         ok($parser->has_parse_errors(), "Check that parser errors occur");
278         #parse uploaded file with appropriate plugin
279         $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $file_name);
280         $parser->load_plugin('ParseIGDFile');
281         my $meta = $parser->parse();
282         ok($meta, "Check if parse validate excel file works");
284         print STDERR "CHECK =" . Dumper($meta) . "\n";
286         my $parsed_data_check = {
287                 'blank_well'   => 'F05',
288                 'trial_name'   => 'CASSAVA_GS_74',
289                 'user_id'      => 'I.Rabbi@cgiar.org',
290                 'project_name' => 'NEXTGENCASSAVA'
291         };
293         is_deeply($meta, $parsed_data_check, 'check igd file parse data');
295         my $list_id = 4;
296         my $list = CXGN::List->new({ dbh => $c->dbh(), list_id => $list_id });
297         my $elements = $list->elements();
299         my $slu = CXGN::Stock::StockLookup->new({ schema => $c->bcs_schema });
301         # remove non-word characters from names as required by
302         # IGD naming conventions. Store new names as synonyms.
303         #
305         foreach my $e (@$elements) {
306                 my $submission_name = $e;
307                 $submission_name =~ s/\W/\_/g;
309                 print STDERR "Replacing element $e with $submission_name\n";
310                 $slu->set_stock_name($e);
311                 my $s = $slu->get_stock();
312                 $slu->set_stock_name($submission_name);
314                 print STDERR "Storing synonym $submission_name for $e\n";
315                 $slu->set_stock_name($e);
316                 eval {
317                         #my $rs = $slu->_get_stock_resultset();
318                         $s->create_stockprops(
319                                 { igd_synonym => $submission_name },
320                                 { autocreate  => 1,
321                                         'cv.name' => 'local',
322                                 });
323                 };
324         }
326         my $td = CXGN::Trial::TrialDesign->new({ schema => $c->bcs_schema });
328         $td->set_stock_list($elements);
329         $td->set_block_size(96);
330         $td->set_blank($meta->{blank_well});
331         $td->set_design_type("genotyping_plate");
332         $td->set_trial_name($meta->{trial_name});
334         my $design;
335         $td->calculate_design();
336         $design = $td->get_design();
338         #print STDERR Dumper $design;
340         my $igd_design_check = {
341                 'A05' => {
342                         'stock_name'  => 'test_accession5',
343                         'col_number'  => 5,
344                         'is_blank'    => 0,
345                         'row_number'  => 'A',
346                         'plot_number' => 'A05',
347                         'plot_name'   => 'CASSAVA_GS_74_A05'
348                 },
349                 'A04' => {
350                         'plot_number' => 'A04',
351                         'plot_name'   => 'CASSAVA_GS_74_A04',
352                         'col_number'  => 4,
353                         'stock_name'  => 'test_accession4',
354                         'is_blank'    => 0,
355                         'row_number'  => 'A'
356                 },
357                 'A02' => {
358                         'is_blank'    => 0,
359                         'row_number'  => 'A',
360                         'col_number'  => 2,
361                         'stock_name'  => 'test_accession2',
362                         'plot_name'   => 'CASSAVA_GS_74_A02',
363                         'plot_number' => 'A02'
364                 },
365                 'A01' => {
366                         'stock_name'  => 'test_accession1',
367                         'col_number'  => 1,
368                         'row_number'  => 'A',
369                         'is_blank'    => 0,
370                         'plot_name'   => 'CASSAVA_GS_74_A01',
371                         'plot_number' => 'A01'
372                 },
373                 'F05' => {
374                         'plot_name'   => 'CASSAVA_GS_74_F05_BLANK',
375                         'plot_number' => 'F05',
376                         'is_blank'    => 1,
377                         'row_number'  => 'F',
378                         'stock_name'  => 'BLANK',
379                         'col_number'  => 5
380                 },
381                 'A03' => {
382                         'plot_name'   => 'CASSAVA_GS_74_A03',
383                         'plot_number' => 'A03',
384                         'stock_name'  => 'test_accession3',
385                         'col_number'  => 3,
386                         'row_number'  => 'A',
387                         'is_blank'    => 0
388                 }
389         };
391         is_deeply($design, $igd_design_check, "check igd design");
393         #genotyping project for igd
394         my $chado_schema = $c->bcs_schema;
395         my $location_rs = $chado_schema->resultset('NaturalDiversity::NdGeolocation')->search({ description => 'Cornell Biotech' });
396         my $location_id = $location_rs->first->nd_geolocation_id;
398         my $bp_rs = $chado_schema->resultset('Project::Project')->find({ name => 'test' });
399         my $breeding_program_id = $bp_rs->project_id();
401         my $add_genotyping_project = CXGN::Genotype::StoreGenotypingProject->new({
402                 chado_schema        => $chado_schema,
403                 dbh                 => $c->dbh(),
404                 project_name        => 'test_genotyping_project_2',
405                 breeding_program_id => $breeding_program_id,
406                 project_facility    => 'igd',
407                 data_type           => 'snp',
408                 year                => '2022',
409                 project_description => 'genotyping project for test',
410                 nd_geolocation_id   => $location_id,
411                 owner_id            => 41
412         });
413         ok(my $store_return = $add_genotyping_project->store_genotyping_project(), "store genotyping project");
415         my $gp_rs = $chado_schema->resultset('Project::Project')->find({ name => 'test_genotyping_project_2' });
416         my $genotyping_project_id = $gp_rs->project_id();
417         my $trial = CXGN::Trial->new({ bcs_schema => $chado_schema, trial_id => $genotyping_project_id });
419         #editing genotyping project details
420         my $new_year = '2021';
421         my $new_description = 'new genotyping project for test';
422         $trial->set_year($new_year);
423         $trial->set_description($new_description);
425         my $location_data = $trial->get_location();
426         my $location_name = $location_data->[1];
427         my $description = $trial->get_description();
428         my $genotyping_facility = $trial->get_genotyping_facility();
429         my $plate_year = $trial->get_year();
430         is($plate_year, '2021');
431         is($description, 'new genotyping project for test');
433         my $program_object = CXGN::BreedersToolbox::Projects->new({ schema => $chado_schema });
434         my $breeding_program_data = $program_object->get_breeding_programs_by_trial($genotyping_project_id);
435         my $breeding_program_name = $breeding_program_data->[0]->[1];
437         my $trial_create = CXGN::Trial::TrialCreate
438                 ->new({
439                 chado_schema                  => $chado_schema,
440                 dbh                           => $c->dbh(),
441                 owner_id                      => 41,
442                 trial_year                    => $plate_year,
443                 trial_location                => $location_name,
444                 program                       => $breeding_program_name,
445                 trial_description             => "Test Genotyping Plate Upload",
446                 design_type                   => 'genotyping_plate',
447                 design                        => $design,
448                 trial_name                    => "test_genotyping_trial_upload",
449                 is_genotyping                 => 1,
450                 genotyping_user_id            => $meta->{user_id} || "unknown",
451                 genotyping_project_id         => $genotyping_project_id,
452                 genotyping_facility_submitted => 'no',
453                 genotyping_facility           => $genotyping_facility,
454                 genotyping_plate_format       => '96',
455                 genotyping_plate_sample_type  => 'DNA',
456                 operator                      => "janedoe"
457         });
459         my $save = $trial_create->save_trial();
461         ok($save->{'trial_id'}, "check that trial_create worked");
462         my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
463         ok($project_name == "test_genotyping_trial_upload", "check that trial_create really worked for igd trial");
465         my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
466         ok($project_desc == "Test Genotyping Plate Upload", "check that trial_create really worked for igd trial");
468         $post_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
469         my $post2_project_diff = $post_project_count - $pre_project_count;
470         print STDERR "Project: " . $post2_project_diff . "\n";
471         ok($post2_project_diff == 3, "check project table after upload igd trial");
473         $post_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
474         my $post2_nd_experiment_diff = $post_nd_experiment_count - $pre_nd_experiment_count;
475         print STDERR "NdExperiment: " . $post2_nd_experiment_diff . "\n";
476         ok($post2_nd_experiment_diff == 2, "check ndexperiment table after upload igd trial");
478         $post_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
479         my $post2_nd_experiment_proj_diff = $post_nd_experiment_proj_count - $pre_nd_experiment_proj_count;
480         print STDERR "NdExperimentProject: " . $post2_nd_experiment_proj_diff . "\n";
481         ok($post2_nd_experiment_proj_diff == 2, "check ndexperimentproject table after upload igd trial");
483         $post_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
484         my $post2_nd_experimentprop_diff = $post_nd_experimentprop_count - $pre_nd_experimentprop_count;
485         print STDERR "NdExperimentprop: " . $post2_nd_experimentprop_diff . "\n";
486         ok($post2_nd_experimentprop_diff == 1, "check ndexperimentprop table after upload igd trial");
488         $post_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
489         my $post2_project_prop_diff = $post_project_prop_count - $pre_project_prop_count;
490         print STDERR "Projectprop: " . $post2_project_prop_diff . "\n";
491         ok($post2_project_prop_diff == 15, "check projectprop table after adding genotyping project and uploading igd trial");
493         $post_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
494         my $post2_stock_diff = $post_stock_count - $pre_stock_count;
495         print STDERR "Stock: " . $post2_stock_diff . "\n";
496         ok($post2_stock_diff == 14, "check stock table after upload igd trial");
498         $post_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
499         my $post2_stock_prop_diff = $post_stock_prop_count - $pre_stock_prop_count;
500         print STDERR "Stockprop: " . $post2_stock_prop_diff . "\n";
501         ok($post2_stock_prop_diff == 84, "check stockprop table after upload igd trial");
503         $post_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
504         my $post2_stock_relationship_diff = $post_stock_relationship_count - $pre_stock_relationship_count;
505         print STDERR "StockRelationship: " . $post2_stock_relationship_diff . "\n";
506         ok($post2_stock_relationship_diff == 14, "check stockrelationship table after upload igd trial");
508         $post_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
509         my $post2_nd_experiment_stock_diff = $post_nd_experiment_stock_count - $pre_nd_experiment_stock_count;
510         print STDERR "NdExperimentStock: " . $post2_nd_experiment_stock_diff . "\n";
511         ok($post2_nd_experiment_stock_diff == 14, "check ndexperimentstock table after upload igd trial");
513         $post_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
514         my $post2_project_relationship_diff = $post_project_relationship_count - $pre_project_relationship_count;
515         print STDERR "ProjectRelationship: " . $post2_project_relationship_diff . "\n";
516         ok($post2_project_relationship_diff == 4, "check projectrelationship table after adding genotyping project and uploading igd trial");
519         #############################
520         # Upload a trial with seedlot info filled
522         my %upload_metadata;
523         my $file_name = "t/data/trial/trial_layout_with_seedlot_example.$extension";
524         my $time = DateTime->now();
525         my $timestamp = $time->ymd() . "_" . $time->hms();
527         #Test archive upload file
528         my $uploader = CXGN::UploadFile->new({
529                 tempfile         => $file_name,
530                 subdirectory     => 'temp_trial_upload',
531                 archive_path     => '/tmp',
532                 archive_filename => "trial_layout_with_seedlot_example.$extension",
533                 timestamp        => $timestamp,
534                 user_id          => 41, #janedoe in fixture
535                 user_role        => 'curator'
536         });
538         ## Store uploaded temporary file in archive
539         my $archived_filename_with_path = $uploader->archive();
540         my $md5 = $uploader->get_md5($archived_filename_with_path);
541         ok($archived_filename_with_path);
542         ok($md5);
544         $upload_metadata{'archived_file'} = $archived_filename_with_path;
545         $upload_metadata{'archived_file_type'} = "trial upload file";
546         $upload_metadata{'user_id'} = $c->sp_person_id;
547         $upload_metadata{'date'} = "2014-02-14_09:10:11";
550         #parse uploaded file with appropriate plugin
551         $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path);
552         $parser->load_plugin('TrialExcelFormat');
553         $rtn = $parser->parse();
554         $parsed_data = $rtn->{'design'};
555         ok($parsed_data, "Check if parse validate excel file works");
556         ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
558         #print STDERR Dumper $parsed_data;
560         my $parsed_data_check = {
561                 '7' => {
562                         'is_a_control'              => 0,
563                         'num_seed_per_plot'         => '12',
564                         'block_number'              => '2',
565                         'rep_number'                => '1',
566                         'col_number'                => '2',
567                         'plot_name'                 => 'plot_with_seedlot_name7',
568                         'stock_name'                => 'test_accession4',
569                         'seedlot_name'              => 'test_accession4_001',
570                         'plot_number'               => '7',
571                         'range_number'              => '2',
572                         'weight_gram_seed_per_plot' => 0,
573                         'row_number'                => '3'
574                 },
575                 '4' => {
576                         'row_number'                => '4',
577                         'weight_gram_seed_per_plot' => '5',
578                         'range_number'              => '1',
579                         'seedlot_name'              => 'test_accession2_001',
580                         'plot_number'               => '4',
581                         'rep_number'                => '2',
582                         'num_seed_per_plot'         => '12',
583                         'block_number'              => '1',
584                         'is_a_control'              => 0,
585                         'stock_name'                => 'test_accession2',
586                         'plot_name'                 => 'plot_with_seedlot_name4',
587                         'col_number'                => '1'
588                 },
589                 '1' => {
590                         'row_number'                => '1',
591                         'weight_gram_seed_per_plot' => 0,
592                         'range_number'              => '1',
593                         'seedlot_name'              => 'test_accession1_001',
594                         'plot_number'               => '1',
595                         'rep_number'                => '1',
596                         'num_seed_per_plot'         => '12',
597                         'block_number'              => '1',
598                         'is_a_control'              => 0,
599                         'stock_name'                => 'test_accession1',
600                         'plot_name'                 => 'plot_with_seedlot_name1',
601                         'col_number'                => '1'
602                 },
603                 '5' => {
604                         'range_number'              => '2',
605                         'row_number'                => '1',
606                         'weight_gram_seed_per_plot' => 0,
607                         'plot_number'               => '5',
608                         'seedlot_name'              => 'test_accession3_001',
609                         'plot_name'                 => 'plot_with_seedlot_name5',
610                         'col_number'                => '2',
611                         'stock_name'                => 'test_accession3',
612                         'rep_number'                => '1',
613                         'is_a_control'              => 0,
614                         'num_seed_per_plot'         => '12',
615                         'block_number'              => '2'
616                 },
617                 '2' => {
618                         'block_number'              => '1',
619                         'num_seed_per_plot'         => '12',
620                         'is_a_control'              => 0,
621                         'rep_number'                => '2',
622                         'stock_name'                => 'test_accession1',
623                         'col_number'                => '1',
624                         'plot_name'                 => 'plot_with_seedlot_name2',
625                         'plot_number'               => '2',
626                         'seedlot_name'              => 'test_accession1_001',
627                         'row_number'                => '2',
628                         'weight_gram_seed_per_plot' => 0,
629                         'range_number'              => '1'
630                 },
631                 '3' => {
632                         'weight_gram_seed_per_plot' => '4',
633                         'row_number'                => '3',
634                         'range_number'              => '1',
635                         'plot_number'               => '3',
636                         'seedlot_name'              => 'test_accession2_001',
637                         'rep_number'                => '1',
638                         'block_number'              => '1',
639                         'num_seed_per_plot'         => '12',
640                         'is_a_control'              => 0,
641                         'stock_name'                => 'test_accession2',
642                         'plot_name'                 => 'plot_with_seedlot_name3',
643                         'col_number'                => '1'
644                 },
645                 '6' => {
646                         'col_number'                => '2',
647                         'plot_name'                 => 'plot_with_seedlot_name6',
648                         'stock_name'                => 'test_accession3',
649                         'is_a_control'              => 0,
650                         'num_seed_per_plot'         => '12',
651                         'block_number'              => '2',
652                         'rep_number'                => '2',
653                         'seedlot_name'              => 'test_accession3_001',
654                         'plot_number'               => '6',
655                         'range_number'              => '2',
656                         'row_number'                => '2',
657                         'weight_gram_seed_per_plot' => 0
658                 },
659                 '8' => {
660                         'seedlot_name'              => 'test_accession4_001',
661                         'plot_number'               => '8',
662                         'weight_gram_seed_per_plot' => 0,
663                         'row_number'                => '4',
664                         'range_number'              => '2',
665                         'block_number'              => '2',
666                         'num_seed_per_plot'         => '12',
667                         'is_a_control'              => 0,
668                         'rep_number'                => '2',
669                         'stock_name'                => 'test_accession4',
670                         'col_number'                => '2',
671                         'plot_name'                 => 'plot_with_seedlot_name8'
672                 }
673         };
675         is_deeply($parsed_data, $parsed_data_check, 'check trial excel parse data');
677         my $trial_create = CXGN::Trial::TrialCreate
678                 ->new({
679                 chado_schema      => $c->bcs_schema(),
680                 dbh               => $c->dbh(),
681                 owner_id          => 41,
682                 trial_year        => "2016",
683                 trial_description => "Trial Upload Test",
684                 trial_location    => "test_location",
685                 trial_name        => "Trial_upload_with_seedlot_test",
686                 design_type       => "RCBD",
687                 design            => $parsed_data,
688                 program           => "test",
689                 upload_trial_file => $archived_filename_with_path,
690                 operator          => "janedoe"
691         });
693         $trial_create->save_trial();
695         ok($trial_create, "check that trial_create worked");
696         my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
697         ok($project_name == "Trial_upload_test", "check that trial_create really worked");
699         my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
700         ok($project_desc == "Trial Upload Test", "check that trial_create really worked");
702         my $post_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
703         my $post1_project_diff = $post_project_count - $pre_project_count;
704         print STDERR "Project: " . $post1_project_diff . "\n";
705         ok($post1_project_diff == 4, "check project table after third upload excel trial");
707         my $post_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
708         my $post1_nd_experiment_diff = $post_nd_experiment_count - $pre_nd_experiment_count;
709         print STDERR "NdExperiment: " . $post1_nd_experiment_diff . "\n";
710         ok($post1_nd_experiment_diff == 3, "check ndexperiment table after upload excel trial");
712         my $post_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
713         my $post1_nd_experiment_proj_diff = $post_nd_experiment_proj_count - $pre_nd_experiment_proj_count;
714         print STDERR "NdExperimentProject: " . $post1_nd_experiment_proj_diff . "\n";
715         ok($post1_nd_experiment_proj_diff == 3, "check ndexperimentproject table after upload excel trial");
717         my $post_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
718         my $post1_nd_experimentprop_diff = $post_nd_experimentprop_count - $pre_nd_experimentprop_count;
719         print STDERR "NdExperimentprop: " . $post1_nd_experimentprop_diff . "\n";
720         ok($post1_nd_experimentprop_diff == 1, "check ndexperimentprop table after upload excel trial");
722         my $post_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
723         my $post1_project_prop_diff = $post_project_prop_count - $pre_project_prop_count;
724         print STDERR "Projectprop: " . $post1_project_prop_diff . "\n";
725         ok($post1_project_prop_diff == 19, "check projectprop table after upload excel trial");
727         my $post_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
728         my $post1_stock_diff = $post_stock_count - $pre_stock_count;
729         print STDERR "Stock: " . $post1_stock_diff . "\n";
730         ok($post1_stock_diff == 22, "check stock table after upload excel trial");
732         my $post_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
733         my $post1_stock_prop_diff = $post_stock_prop_count - $pre_stock_prop_count;
734         print STDERR "Stockprop: " . $post1_stock_prop_diff . "\n";
735         #ok($post1_stock_prop_diff == 133, "check stockprop table after upload excel trial");
737         my $post_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
738         my $post1_stock_relationship_diff = $post_stock_relationship_count - $pre_stock_relationship_count;
739         print STDERR "StockRelationship: " . $post1_stock_relationship_diff . "\n";
740         ok($post1_stock_relationship_diff == 30, "check stockrelationship table after upload excel trial");
742         my $post_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
743         my $post1_nd_experiment_stock_diff = $post_nd_experiment_stock_count - $pre_nd_experiment_stock_count;
744         print STDERR "NdExperimentStock: " . $post1_nd_experiment_stock_diff . "\n";
745         ok($post1_nd_experiment_stock_diff == 22, "check ndexperimentstock table after upload excel trial");
747         my $post_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
748         my $post1_project_relationship_diff = $post_project_relationship_count - $pre_project_relationship_count;
749         print STDERR "ProjectRelationship: " . $post1_project_relationship_diff . "\n";
750         ok($post1_project_relationship_diff == 5, "check projectrelationship table after upload excel trial");
752         #adding new genotyping project
753         my $add_genotyping_project_2 = CXGN::Genotype::StoreGenotypingProject->new({
754                 chado_schema        => $chado_schema,
755                 dbh                 => $c->dbh(),
756                 project_name        => 'test_genotyping_project_4',
757                 breeding_program_id => $breeding_program_id,
758                 project_facility    => 'igd',
759                 data_type           => 'snp',
760                 year                => '2022',
761                 project_description => 'genotyping project for test',
762                 nd_geolocation_id   => $location_id,
763                 owner_id            => 41
764         });
765         ok(my $store_return_2 = $add_genotyping_project_2->store_genotyping_project(), "store genotyping project");
767         my $gp_rs_2 = $chado_schema->resultset('Project::Project')->find({ name => 'test_genotyping_project_4' });
768         my $genotyping_project_id_2 = $gp_rs_2->project_id();
770         my $mech = Test::WWW::Mechanize->new;
771         $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username" => "janedoe", "password" => "secretpw", "grant_type" => "password" ]);
772         my $response = decode_json $mech->content;
773         print STDERR Dumper $response;
774         my $sgn_session_id = $response->{access_token};
775         print STDERR $sgn_session_id . "\n";
777         my $file = $f->config->{basepath} . "/t/data/genotype_trial_upload/genotype_trial_upload.$extension";
778         my $ua = LWP::UserAgent->new;
779         $response = $ua->post(
780                 'http://localhost:3010/ajax/breeders/parsegenotypetrial',
781                 Content_Type => 'form-data',
782                 Content      => [
783                         genotyping_trial_layout_upload => [
784                                 $file,
785                                 "genotype_trial_upload.$extension",
786                                 Content_Type => ($extension eq "xls") ? 'application/vnd.ms-excel' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
787                         ],
788                         "sgn_session_id"               => $sgn_session_id,
789                         "genotyping_trial_name"        => '2018TestPlate02'
790                 ]
791         );
793         ok($response->is_success);
794         my $message = $response->decoded_content;
795         my $message_hash = decode_json $message;
797         is_deeply($message_hash, {
798                 'success' => '1',
799                 'design'  => {
800                         'A01' => {
801                                 'concentration'       => '5',
802                                 'acquisition_date'    => '2018/02/16',
803                                 'dna_person'          => 'nmorales',
804                                 'volume'              => '10',
805                                 'col_number'          => '1',
806                                 'plot_name'           => '2018TestPlate02_A01',
807                                 'ncbi_taxonomy_id'    => '9001',
808                                 'stock_name'          => 'KASESE_TP2013_885',
809                                 'notes'               => 'test well A01',
810                                 'is_blank'            => 0,
811                                 'extraction'          => 'CTAB',
812                                 'plot_number'         => 'A01',
813                                 'row_number'          => 'A',
814                                 'tissue_type'         => 'leaf',
815                                 'facility_identifier' => undef
816                         },
817                         'A03' => {
818                                 'notes'               => 'test well A03',
819                                 'is_blank'            => 0,
820                                 'stock_name'          => 'KASESE_TP2013_1671',
821                                 'ncbi_taxonomy_id'    => '9001',
822                                 'plot_name'           => '2018TestPlate02_A03',
823                                 'tissue_type'         => 'leaf',
824                                 'row_number'          => 'A',
825                                 'plot_number'         => 'A03',
826                                 'extraction'          => 'CTAB',
827                                 'volume'              => '10',
828                                 'dna_person'          => 'nmorales',
829                                 'concentration'       => '5',
830                                 'acquisition_date'    => '2018/02/16',
831                                 'col_number'          => '3',
832                                 'facility_identifier' => undef
833                         },
834                         'A02' => {
835                                 'extraction'          => undef,
836                                 'plot_number'         => 'A02',
837                                 'row_number'          => 'A',
838                                 'tissue_type'         => 'stem',
839                                 'stock_name'          => 'BLANK',
840                                 'notes'               => 'test blank',
841                                 'is_blank'            => 1,
842                                 'ncbi_taxonomy_id'    => undef,
843                                 'plot_name'           => '2018TestPlate02_A02',
844                                 'col_number'          => '2',
845                                 'volume'              => undef,
846                                 'acquisition_date'    => '2018/02/16',
847                                 'concentration'       => undef,
848                                 'dna_person'          => 'nmorales',
849                                 'facility_identifier' => undef
850                         }
851                 }
852         });
854         my $project = $c->bcs_schema()->resultset("Project::Project")->find({ name => 'test' });
855         my $location = $c->bcs_schema()->resultset("NaturalDiversity::NdGeolocation")->find({ description => 'test_location' });
857         my $plate_data = {
858                 design                     => $message_hash->{design},
859                 genotyping_facility_submit => 'yes',
860                 name                       => 'test_genotype_upload_trial1',
861                 genotyping_project_id      => $genotyping_project_id_2,
862                 sample_type                => 'DNA',
863                 plate_format               => '96'
864         };
866         $mech->post_ok('http://localhost:3010/ajax/breeders/storegenotypetrial', [ "sgn_session_id" => $sgn_session_id, plate_data => encode_json($plate_data) ]);
867         $response = decode_json $mech->content;
868         #print STDERR Dumper $response;
870         ok($response->{trial_id});
872         my $file = $f->config->{basepath} . "/t/data/genotype_trial_upload/CoordinateTemplate.csv";
873         my $ua = LWP::UserAgent->new;
874         $response = $ua->post(
875                 'http://localhost:3010/ajax/breeders/parsegenotypetrial',
876                 Content_Type => 'form-data',
877                 Content      => [
878                         genotyping_trial_layout_upload_coordinate_template => [ $file, 'genotype_trial_upload', Content_Type => 'application/vnd.ms-excel', ],
879                         "sgn_session_id"                                   => $sgn_session_id,
880                         "genotyping_trial_name"                            => "18DNA00101"
881                 ]
882         );
884         #print STDERR Dumper $response;
885         ok($response->is_success);
886         my $message = $response->decoded_content;
887         my $message_hash = decode_json $message;
888         #print STDERR Dumper $message_hash;
890         is_deeply($message_hash, {
891                 'success' => '1',
892                 'design'  => {
893                         'B12' => {
894                                 'notes'               => 'newplate',
895                                 'ncbi_taxonomy_id'    => 'NA',
896                                 'dna_person'          => 'gbauchet',
897                                 'is_blank'            => 1,
898                                 'concentration'       => 'NA',
899                                 'plot_number'         => 'B12',
900                                 'volume'              => 'NA',
901                                 'tissue_type'         => 'leaf',
902                                 'plot_name'           => '18DNA00101_B12',
903                                 'extraction'          => 'NA',
904                                 'row_number'          => 'B',
905                                 'col_number'          => '12',
906                                 'acquisition_date'    => '8/23/2018',
907                                 'stock_name'          => 'BLANK',
908                                 'facility_identifier' => undef
909                         },
910                         'A01' => {
911                                 'stock_name'          => 'KASESE_TP2013_1671',
912                                 'acquisition_date'    => '8/23/2018',
913                                 'col_number'          => '01',
914                                 'row_number'          => 'A',
915                                 'extraction'          => 'NA',
916                                 'plot_name'           => '18DNA00101_A01',
917                                 'tissue_type'         => 'leaf',
918                                 'plot_number'         => 'A01',
919                                 'volume'              => 'NA',
920                                 'dna_person'          => 'gbauchet',
921                                 'is_blank'            => 0,
922                                 'concentration'       => 'NA',
923                                 'ncbi_taxonomy_id'    => 'NA',
924                                 'notes'               => 'newplate',
925                                 'facility_identifier' => undef
926                         },
927                         'B01' => {
928                                 'plot_name'           => '18DNA00101_B01',
929                                 'extraction'          => 'NA',
930                                 'row_number'          => 'B',
931                                 'col_number'          => '01',
932                                 'stock_name'          => 'KASESE_TP2013_1671',
933                                 'acquisition_date'    => '8/23/2018',
934                                 'ncbi_taxonomy_id'    => 'NA',
935                                 'notes'               => 'newplate',
936                                 'dna_person'          => 'gbauchet',
937                                 'is_blank'            => 0,
938                                 'concentration'       => 'NA',
939                                 'plot_number'         => 'B01',
940                                 'volume'              => 'NA',
941                                 'tissue_type'         => 'leaf',
942                                 'facility_identifier' => undef
943                         },
944                         'C01' => {
945                                 'col_number'          => '01',
946                                 'acquisition_date'    => '8/23/2018',
947                                 'stock_name'          => 'KASESE_TP2013_885',
948                                 'extraction'          => 'NA',
949                                 'row_number'          => 'C',
950                                 'plot_name'           => '18DNA00101_C01',
951                                 'tissue_type'         => 'leaf',
952                                 'is_blank'            => 0,
953                                 'dna_person'          => 'gbauchet',
954                                 'concentration'       => 'NA',
955                                 'plot_number'         => 'C01',
956                                 'volume'              => 'NA',
957                                 'ncbi_taxonomy_id'    => 'NA',
958                                 'notes'               => 'newplate',
959                                 'facility_identifier' => undef
960                         },
961                         'D01' => {
962                                 'ncbi_taxonomy_id'    => 'NA',
963                                 'notes'               => 'newplate',
964                                 'plot_number'         => 'D01',
965                                 'volume'              => 'NA',
966                                 'dna_person'          => 'gbauchet',
967                                 'is_blank'            => 0,
968                                 'concentration'       => 'NA',
969                                 'tissue_type'         => 'leaf',
970                                 'plot_name'           => '18DNA00101_D01',
971                                 'row_number'          => 'D',
972                                 'extraction'          => 'NA',
973                                 'acquisition_date'    => '8/23/2018',
974                                 'stock_name'          => 'KASESE_TP2013_885',
975                                 'col_number'          => '01',
976                                 'facility_identifier' => undef
977                         }
978                 }
979         }, 'test upload parse of coordinate genotyping plate');
981         my $plate_data = {
982                 design                     => $message_hash->{design},
983                 genotyping_facility_submit => 'no',
984                 name                       => 'test_genotype_upload_coordinate_trial101',
985                 genotyping_project_id      => $genotyping_project_id_2,
986                 sample_type                => 'DNA',
987                 plate_format               => '96'
988         };
991         $mech->post_ok('http://localhost:3010/ajax/breeders/storegenotypetrial', [ "sgn_session_id" => $sgn_session_id, plate_data => encode_json($plate_data) ]);
992         $response = decode_json $mech->content;
993         #print STDERR Dumper $response;
995         ok($response->{trial_id});
997         my $file = $f->config->{basepath} . "/t/data/genotype_trial_upload/CoordinatePlateUpload.csv";
998         my $ua = LWP::UserAgent->new;
999         $response = $ua->post(
1000                 'http://localhost:3010/ajax/breeders/parsegenotypetrial',
1001                 Content_Type => 'form-data',
1002                 Content      => [
1003                         genotyping_trial_layout_upload_coordinate => [ $file, 'genotype_trial_upload', Content_Type => 'application/vnd.ms-excel', ],
1004                         "sgn_session_id"                          => $sgn_session_id,
1005                         "genotyping_trial_name"                   => "18DNA00001"
1006                 ]
1007         );
1009         #print STDERR Dumper $response;
1010         ok($response->is_success);
1011         my $message = $response->decoded_content;
1012         my $message_hash = decode_json $message;
1013         #print STDERR Dumper $message_hash;
1015         is_deeply($message_hash, {
1016                 'design'  => {
1017                         'B01' => {
1018                                 'ncbi_taxonomy_id'    => 'NA',
1019                                 'is_blank'            => 0,
1020                                 'acquisition_date'    => '2018-02-06',
1021                                 'plot_name'           => '18DNA00001_B01',
1022                                 'col_number'          => '01',
1023                                 'notes'               => '',
1024                                 'extraction'          => 'CTAB',
1025                                 'tissue_type'         => 'leaf',
1026                                 'volume'              => 'NA',
1027                                 'concentration'       => 'NA',
1028                                 'stock_name'          => 'test_accession1',
1029                                 'plot_number'         => 'B01',
1030                                 'row_number'          => 'B',
1031                                 'dna_person'          => 'Trevor_Rife',
1032                                 'facility_identifier' => undef
1033                         },
1034                         'B04' => {
1035                                 'tissue_type'         => 'leaf',
1036                                 'extraction'          => 'CTAB',
1037                                 'notes'               => '',
1038                                 'col_number'          => '04',
1039                                 'acquisition_date'    => '2018-02-06',
1040                                 'plot_name'           => '18DNA00001_B04',
1041                                 'ncbi_taxonomy_id'    => 'NA',
1042                                 'is_blank'            => 1,
1043                                 'row_number'          => 'B',
1044                                 'dna_person'          => 'Trevor_Rife',
1045                                 'plot_number'         => 'B04',
1046                                 'stock_name'          => 'BLANK',
1047                                 'concentration'       => 'NA',
1048                                 'volume'              => 'NA',
1049                                 'facility_identifier' => undef
1050                         },
1051                         'C01' => {
1052                                 'is_blank'            => 0,
1053                                 'ncbi_taxonomy_id'    => 'NA',
1054                                 'plot_name'           => '18DNA00001_C01',
1055                                 'acquisition_date'    => '2018-02-06',
1056                                 'notes'               => '',
1057                                 'col_number'          => '01',
1058                                 'extraction'          => 'CTAB',
1059                                 'tissue_type'         => 'leaf',
1060                                 'volume'              => 'NA',
1061                                 'concentration'       => 'NA',
1062                                 'stock_name'          => 'test_accession2',
1063                                 'plot_number'         => 'C01',
1064                                 'dna_person'          => 'Trevor_Rife',
1065                                 'row_number'          => 'C',
1066                                 'facility_identifier' => undef
1067                         },
1068                         'C04' => {
1069                                 'ncbi_taxonomy_id'    => 'NA',
1070                                 'is_blank'            => 1,
1071                                 'plot_name'           => '18DNA00001_C04',
1072                                 'acquisition_date'    => '2018-02-06',
1073                                 'notes'               => '',
1074                                 'col_number'          => '04',
1075                                 'tissue_type'         => 'leaf',
1076                                 'extraction'          => 'CTAB',
1077                                 'volume'              => 'NA',
1078                                 'stock_name'          => 'BLANK',
1079                                 'concentration'       => 'NA',
1080                                 'plot_number'         => 'C04',
1081                                 'dna_person'          => 'Trevor_Rife',
1082                                 'row_number'          => 'C',
1083                                 'facility_identifier' => undef
1084                         },
1085                         'A01' => {
1086                                 'is_blank'            => 0,
1087                                 'ncbi_taxonomy_id'    => 'NA',
1088                                 'acquisition_date'    => '2018-02-06',
1089                                 'plot_name'           => '18DNA00001_A01',
1090                                 'notes'               => '',
1091                                 'col_number'          => '01',
1092                                 'extraction'          => 'CTAB',
1093                                 'tissue_type'         => 'leaf',
1094                                 'volume'              => 'NA',
1095                                 'concentration'       => 'NA',
1096                                 'stock_name'          => 'test_accession1',
1097                                 'plot_number'         => 'A01',
1098                                 'dna_person'          => 'Trevor_Rife',
1099                                 'row_number'          => 'A',
1100                                 'facility_identifier' => undef
1101                         },
1102                         'D01' => {
1103                                 'dna_person'          => 'Trevor_Rife',
1104                                 'row_number'          => 'D',
1105                                 'plot_number'         => 'D01',
1106                                 'stock_name'          => 'test_accession2',
1107                                 'concentration'       => 'NA',
1108                                 'volume'              => 'NA',
1109                                 'tissue_type'         => 'leaf',
1110                                 'extraction'          => 'CTAB',
1111                                 'col_number'          => '01',
1112                                 'notes'               => '',
1113                                 'acquisition_date'    => '2018-02-06',
1114                                 'plot_name'           => '18DNA00001_D01',
1115                                 'ncbi_taxonomy_id'    => 'NA',
1116                                 'is_blank'            => 0,
1117                                 'facility_identifier' => undef
1118                         }
1119                 },
1120                 'success' => '1'
1121         }, 'test upload parse of coordinate genotyping plate');
1123         my $plate_data = {
1124                 design                     => $message_hash->{design},
1125                 genotyping_facility_submit => 'no',
1126                 name                       => 'test_genotype_upload_coordinate_trial1',
1127                 genotyping_project_id      => $genotyping_project_id,
1128                 sample_type                => 'DNA',
1129                 plate_format               => '96'
1130         };
1132         $mech->post_ok('http://localhost:3010/ajax/breeders/storegenotypetrial', [ "sgn_session_id" => $sgn_session_id, plate_data => encode_json($plate_data) ]);
1133         $response = decode_json $mech->content;
1134         #print STDERR "RESPONSE: ".Dumper $response;
1136         ok($response->{trial_id});
1137         my $geno_trial_id = $response->{trial_id};
1138         $mech->get_ok("http://localhost:3010/breeders/trial/$geno_trial_id/download/layout?format=intertekxls&dataLevel=plate");
1139         my $intertek_download = $mech->content;
1140         my $contents = ReadData $intertek_download;
1141         #print STDERR Dumper $contents;
1142         is($contents->[0]->{'type'}, 'xls', "check that type of file is correct #1");
1143         is($contents->[0]->{'sheets'}, '1', "check that type of file is correct #2");
1145         my $columns = $contents->[1]->{'cell'};
1146         #print STDERR Dumper scalar(@$columns);
1147         ok(scalar(@$columns) == 7, "check number of col in created file.");
1149         #print STDERR Dumper $columns;
1150         is_deeply($columns, [
1151                 [],
1152                 [
1153                         undef,
1154                         'Sample ID',
1155                         '18DNA00001_A01|||test_accession1',
1156                         '18DNA00001_B01|||test_accession1',
1157                         '18DNA00001_B04|||BLANK',
1158                         '18DNA00001_C01|||test_accession2',
1159                         '18DNA00001_C04|||BLANK',
1160                         '18DNA00001_D01|||test_accession2'
1161                 ],
1162                 [
1163                         undef,
1164                         'Plate ID',
1165                         'test_genotype_upload_coordinate_trial1',
1166                         'test_genotype_upload_coordinate_trial1',
1167                         'test_genotype_upload_coordinate_trial1',
1168                         'test_genotype_upload_coordinate_trial1',
1169                         'test_genotype_upload_coordinate_trial1',
1170                         'test_genotype_upload_coordinate_trial1'
1171                 ],
1172                 [
1173                         undef,
1174                         'Well location',
1175                         'A01',
1176                         'B01',
1177                         'B04',
1178                         'C01',
1179                         'C04',
1180                         'D01'
1181                 ],
1182                 [
1183                         undef,
1184                         'Subject Barcode',
1185                         'test_accession1',
1186                         'test_accession1',
1187                         'BLANK',
1188                         'test_accession2',
1189                         'BLANK',
1190                         'test_accession2'
1191                 ],
1192                 [
1193                         undef,
1194                         'Plate Barcode',
1195                         'test_genotype_upload_coordinate_trial1',
1196                         'test_genotype_upload_coordinate_trial1',
1197                         'test_genotype_upload_coordinate_trial1',
1198                         'test_genotype_upload_coordinate_trial1',
1199                         'test_genotype_upload_coordinate_trial1',
1200                         'test_genotype_upload_coordinate_trial1'
1201                 ],
1202                 [
1203                         undef,
1204                         'Comments',
1205                         'Notes:  AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1206                         'Notes:  AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1207                         'Notes:  AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1208                         'Notes:  AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1209                         'Notes:  AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1210                         'Notes:  AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: '
1211                 ]
1212         ], 'test intertek genotyping plate download');
1214         $mech->get_ok("http://localhost:3010/breeders/trial/$geno_trial_id/download/layout?format=dartseqcsv&dataLevel=plate");
1215         my $intertek_download = $mech->content;
1216         #print STDERR Dumper $intertek_download;
1217         my @intertek_download = split "\n", $intertek_download;
1218         #print STDERR Dumper \@intertek_download;
1220         is_deeply(\@intertek_download, [
1221                 'PlateID,Row,Column,Organism,Species,Genotype,Tissue,Comments',
1222                 'test_genotype_upload_coordinate_trial1,A,01,tomato,"Solanum lycopersicum",18DNA00001_A01|||test_accession1,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"',
1223                 'test_genotype_upload_coordinate_trial1,B,01,tomato,"Solanum lycopersicum",18DNA00001_B01|||test_accession1,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"',
1224                 'test_genotype_upload_coordinate_trial1,C,01,tomato,"Solanum lycopersicum",18DNA00001_C01|||test_accession2,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"',
1225                 'test_genotype_upload_coordinate_trial1,D,01,tomato,"Solanum lycopersicum",18DNA00001_D01|||test_accession2,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"'
1226         ]);
1229         #Upload trial with management factors
1231         my $file_name_with_managementfactors = "t/data/trial/trial_layout_example_with_management_factor.$extension";
1233         #Test archive upload file
1234         my $uploader = CXGN::UploadFile->new({
1235                 tempfile         => $file_name_with_managementfactors,
1236                 subdirectory     => 'temp_trial_upload',
1237                 archive_path     => '/tmp',
1238                 archive_filename => "trial_layout_example_with_management_factor.$extension",
1239                 timestamp        => $timestamp,
1240                 user_id          => 41, #janedoe in fixture
1241                 user_role        => 'curator'
1242         });
1244         ## Store uploaded temporary file in archive
1245         my $management_factor_archived_filename_with_path = $uploader->archive();
1246         my $md5_management_factor = $uploader->get_md5($management_factor_archived_filename_with_path);
1247         ok($management_factor_archived_filename_with_path);
1248         ok($md5_management_factor);
1250         $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $management_factor_archived_filename_with_path);
1251         $parser->load_plugin('TrialExcelFormat');
1252         $rtn = $parser->parse();
1253         $parsed_data = $rtn->{'design'};
1254         ok($parsed_data, "Check if parse validate excel file works");
1255         ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
1257         #print STDERR Dumper $parsed_data;
1259         my $parsed_data_check_with_management_factor = {
1260                 '7'          => {
1261                         'plot_number'  => '7',
1262                         'col_number'   => '2',
1263                         'block_number' => '2',
1264                         'rep_number'   => '1',
1265                         'is_a_control' => 0,
1266                         'stock_name'   => 'test_accession4',
1267                         'row_number'   => '3',
1268                         'range_number' => '2',
1269                         'plot_name'    => 'trial_management_factor_plot_name7'
1270                 },
1271                 '5'          => {
1272                         'col_number'   => '2',
1273                         'plot_number'  => '5',
1274                         'rep_number'   => '1',
1275                         'block_number' => '2',
1276                         'is_a_control' => 0,
1277                         'stock_name'   => 'test_accession3',
1278                         'row_number'   => '1',
1279                         'range_number' => '2',
1280                         'plot_name'    => 'trial_management_factor_plot_name5'
1281                 },
1282                 '3'          => {
1283                         'block_number' => '1',
1284                         'rep_number'   => '1',
1285                         'plot_number'  => '3',
1286                         'col_number'   => '1',
1287                         'range_number' => '1',
1288                         'plot_name'    => 'trial_management_factor_plot_name3',
1289                         'row_number'   => '3',
1290                         'stock_name'   => 'test_accession2',
1291                         'is_a_control' => 0
1292                 },
1293                 '4'          => {
1294                         'stock_name'   => 'test_accession2',
1295                         'is_a_control' => 0,
1296                         'row_number'   => '4',
1297                         'plot_name'    => 'trial_management_factor_plot_name4',
1298                         'range_number' => '1',
1299                         'col_number'   => '1',
1300                         'plot_number'  => '4',
1301                         'block_number' => '1',
1302                         'rep_number'   => '2'
1303                 },
1304                 '8'          => {
1305                         'rep_number'   => '2',
1306                         'block_number' => '2',
1307                         'plot_number'  => '8',
1308                         'col_number'   => '2',
1309                         'row_number'   => '4',
1310                         'range_number' => '2',
1311                         'plot_name'    => 'trial_management_factor_plot_name8',
1312                         'stock_name'   => 'test_accession4',
1313                         'is_a_control' => 0
1314                 },
1315                 '1'          => {
1316                         'block_number' => '1',
1317                         'rep_number'   => '1',
1318                         'plot_number'  => '1',
1319                         'col_number'   => '1',
1320                         'is_a_control' => 0,
1321                         'stock_name'   => 'test_accession1',
1322                         'plot_name'    => 'trial_management_factor_plot_name1',
1323                         'range_number' => '1',
1324                         'row_number'   => '1'
1325                 },
1326                 '2'          => {
1327                         'plot_name'    => 'trial_management_factor_plot_name2',
1328                         'range_number' => '1',
1329                         'row_number'   => '2',
1330                         'stock_name'   => 'test_accession1',
1331                         'is_a_control' => 0,
1332                         'plot_number'  => '2',
1333                         'col_number'   => '1',
1334                         'block_number' => '1',
1335                         'rep_number'   => '2'
1336                 },
1337                 'treatments' => {
1338                         'manage_factor2' => {
1339                                 'new_treatment_stocks' => [
1340                                         'trial_management_factor_plot_name3',
1341                                         'trial_management_factor_plot_name4',
1342                                         'trial_management_factor_plot_name5'
1343                                 ]
1344                         },
1345                         'fert_factor1'   => {
1346                                 'new_treatment_stocks' => [
1347                                         'trial_management_factor_plot_name1',
1348                                         'trial_management_factor_plot_name2',
1349                                         'trial_management_factor_plot_name3',
1350                                         'trial_management_factor_plot_name6',
1351                                         'trial_management_factor_plot_name7',
1352                                         'trial_management_factor_plot_name8'
1353                                 ]
1354                         }
1355                 },
1356                 '6'          => {
1357                         'row_number'   => '2',
1358                         'range_number' => '2',
1359                         'plot_name'    => 'trial_management_factor_plot_name6',
1360                         'stock_name'   => 'test_accession3',
1361                         'is_a_control' => 0,
1362                         'plot_number'  => '6',
1363                         'col_number'   => '2',
1364                         'rep_number'   => '2',
1365                         'block_number' => '2'
1366                 }
1367         };
1369         is_deeply($parsed_data, $parsed_data_check_with_management_factor, 'check trial excel parse data');
1371         my $trial_create_with_management_factor = CXGN::Trial::TrialCreate
1372                 ->new({
1373                 chado_schema      => $c->bcs_schema(),
1374                 dbh               => $c->dbh(),
1375                 trial_year        => "2016",
1376                 trial_description => "Trial Upload Test with Management Factors",
1377                 trial_location    => "test_location",
1378                 trial_name        => "Trial_upload_test_with_management_factor",
1379                 design_type       => "RCBD",
1380                 design            => $parsed_data,
1381                 program           => "test",
1382                 upload_trial_file => $management_factor_archived_filename_with_path,
1383                 operator          => "janedoe",
1384                 owner_id          => 41
1385         });
1387         my $save_with_management_factor = $trial_create_with_management_factor->save_trial();
1389         ok($save_with_management_factor->{'trial_id'}, "check that trial_create worked with management factor");
1390         my $project_name_with_management_factor = $c->bcs_schema()->resultset('Project::Project')->find({ project_id => $save_with_management_factor->{'trial_id'} })->name();
1391         ok($project_name_with_management_factor == "Trial_upload_test_with_management_factor", "check that trial_create really worked");
1393         my $trial_with_management_factor = CXGN::Trial->new({ bcs_schema => $c->bcs_schema, trial_id => $save_with_management_factor->{'trial_id'} });
1394         my $management_factors = $trial_with_management_factor->get_treatments();
1395         #print STDERR Dumper $management_factors;
1396         is(scalar(@$management_factors), 2);
1398         my $trial_management_factor1 = CXGN::Trial->new({ bcs_schema => $c->bcs_schema, trial_id => $management_factors->[0]->[0] });
1399         my $management_factor_name1 = $trial_management_factor1->name();
1400         #print STDERR Dumper $management_factor_name1;
1401         is($management_factor_name1, "Trial_upload_test_with_management_factor_fert_factor1");
1402         my $management_factor_plots1 = $trial_management_factor1->get_plots();
1403         #print STDERR Dumper $management_factor_plots1;
1404         is(scalar(@$management_factor_plots1), 6);
1406         my $trial_management_factor2 = CXGN::Trial->new({ bcs_schema => $c->bcs_schema, trial_id => $management_factors->[1]->[0] });
1407         my $management_factor_name2 = $trial_management_factor2->name();
1408         #print STDERR Dumper $management_factor_name2;
1409         is($management_factor_name2, "Trial_upload_test_with_management_factor_manage_factor2");
1410         my $management_factor_plots2 = $trial_management_factor2->get_plots();
1411         #print STDERR Dumper $management_factor_plots2;
1412         is(scalar(@$management_factor_plots2), 3);
1414         #test deleting genotyping project with genotyping plate
1415         my $schema = $f->bcs_schema();
1416         my $before_deleting_genotyping_project = $schema->resultset("Project::Project")->search({})->count();
1418         $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_project_id . '/delete/genotyping_project');
1419         $response = decode_json $mech->content;
1420         is($response->{'error'}, 'Cannot delete genotyping project with associated genotyping plates.');
1422         my $after_deleting_genotyping_project = $schema->resultset("Project::Project")->search({})->count();
1423         is($after_deleting_genotyping_project, $before_deleting_genotyping_project);
1425         #test deleting empty genotyping project
1426         #first deleting associated genotyping plates
1427         my $genotyping_plate_id_1 = $schema->resultset("Project::Project")->find({ name => 'test_genotyping_trial_upload' })->project_id;
1428         $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_plate_id_1 . '/delete/layout');
1429         $response = decode_json $mech->content;
1430         is($response->{'success'}, '1');
1432         my $genotyping_plate_id_4 = $schema->resultset("Project::Project")->find({ name => 'test_genotype_upload_coordinate_trial1' })->project_id;
1433         $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_plate_id_4 . '/delete/layout');
1434         $response = decode_json $mech->content;
1435         is($response->{'success'}, '1');
1437         #delete empty genotyping_project
1438         $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_project_id . '/delete/genotyping_project');
1439         $response = decode_json $mech->content;
1440         is($response->{'success'}, '1');
1442         my $after_deleting_empty_genotyping_project = $schema->resultset("Project::Project")->search({})->count();
1443         #deleting 2 associated genotyping plates and genotyping project
1444         is($after_deleting_empty_genotyping_project, $before_deleting_genotyping_project - 3);
1447         #Upload Trial with flexible column headers, entry Numbers, and auto generating plot names
1448         my %upload_metadata;
1449         my $file_name = "t/data/trial/trial_layout_example_flexible.$extension";
1450         my $time = DateTime->now();
1451         my $timestamp = $time->ymd() . "_" . $time->hms();
1452         my $trial_name = "Trial_upload_test_flexible";
1454         #Test archive upload file
1455         my $uploader = CXGN::UploadFile->new({
1456                 tempfile         => $file_name,
1457                 subdirectory     => 'temp_trial_upload',
1458                 archive_path     => '/tmp',
1459                 archive_filename => "trial_layout_example_flexible.$extension",
1460                 timestamp        => $timestamp,
1461                 user_id          => 41, #janedoe in fixture
1462                 user_role        => 'curator'
1463         });
1465         ## Store uploaded temporary file in archive
1466         my $archived_filename_with_path = $uploader->archive();
1467         my $md5 = $uploader->get_md5($archived_filename_with_path);
1468         ok($archived_filename_with_path);
1469         ok($md5);
1471         $upload_metadata{'archived_file'} = $archived_filename_with_path;
1472         $upload_metadata{'archived_file_type'} = "trial upload file";
1473         $upload_metadata{'user_id'} = $c->sp_person_id;
1474         $upload_metadata{'date'} = "2014-02-14_09:10:11";
1476         #parse uploaded file with appropriate plugin
1477         $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path, trial_name => $trial_name);
1478         $parser->load_plugin('TrialExcelFormat');
1479         my $p = $parser->parse();
1480         $parsed_data = $p->{'design'};
1481         my $entry_numbers = $p->{'entry_numbers'};
1482         ok($parsed_data, "Check if parse validate excel file works");
1483         ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
1485         my $parsed_data_check = {
1486                 '1' => {
1487                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_1',
1488                         'stock_name'   => 'test_accession1',
1489                         'col_number'   => '1',
1490                         'is_a_control' => 0,
1491                         'rep_number'   => '1',
1492                         'block_number' => '1',
1493                         'range_number' => '1',
1494                         'row_number'   => '1',
1495                         'plot_number'  => '1'
1496                 },
1497                 '6' => {
1498                         'rep_number'   => '2',
1499                         'is_a_control' => 0,
1500                         'block_number' => '2',
1501                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_6',
1502                         'stock_name'   => 'test_accession3',
1503                         'col_number'   => '2',
1504                         'range_number' => '2',
1505                         'row_number'   => '2',
1506                         'plot_number'  => '6'
1507                 },
1508                 '7' => {
1509                         'range_number' => '2',
1510                         'row_number'   => '3',
1511                         'plot_number'  => '7',
1512                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_7',
1513                         'stock_name'   => 'test_accession4',
1514                         'col_number'   => '2',
1515                         'rep_number'   => '1',
1516                         'is_a_control' => 0,
1517                         'block_number' => '2'
1518                 },
1519                 '4' => {
1520                         'range_number' => '1',
1521                         'plot_number'  => '4',
1522                         'row_number'   => '4',
1523                         'is_a_control' => 0,
1524                         'rep_number'   => '2',
1525                         'block_number' => '1',
1526                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_4',
1527                         'col_number'   => '1',
1528                         'stock_name'   => 'test_accession2'
1529                 },
1530                 '8' => {
1531                         'range_number' => '2',
1532                         'row_number'   => '4',
1533                         'plot_number'  => '8',
1534                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_8',
1535                         'stock_name'   => 'test_accession4',
1536                         'col_number'   => '2',
1537                         'rep_number'   => '2',
1538                         'is_a_control' => 0,
1539                         'block_number' => '2'
1540                 },
1541                 '2' => {
1542                         'range_number' => '1',
1543                         'plot_number'  => '2',
1544                         'row_number'   => '2',
1545                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_2',
1546                         'col_number'   => '1',
1547                         'stock_name'   => 'test_accession1',
1548                         'is_a_control' => 0,
1549                         'rep_number'   => '2',
1550                         'block_number' => '1'
1551                 },
1552                 '5' => {
1553                         'range_number' => '2',
1554                         'row_number'   => '1',
1555                         'plot_number'  => '5',
1556                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_5',
1557                         'stock_name'   => 'test_accession3',
1558                         'col_number'   => '2',
1559                         'is_a_control' => 0,
1560                         'rep_number'   => '1',
1561                         'block_number' => '2'
1562                 },
1563                 '3' => {
1564                         'stock_name'   => 'test_accession2',
1565                         'col_number'   => '1',
1566                         'plot_name'    => 'Trial_upload_test_flexible-PLOT_3',
1567                         'block_number' => '1',
1568                         'is_a_control' => 0,
1569                         'rep_number'   => '1',
1570                         'row_number'   => '3',
1571                         'plot_number'  => '3',
1572                         'range_number' => '1'
1573                 }
1574         };
1575         my $entry_numbers_check = {
1576                 'test_accession4' => '4',
1577                 'test_accession2' => '2',
1578                 'test_accession3' => '3',
1579                 'test_accession1' => '1'
1580         };
1582         is_deeply($parsed_data, $parsed_data_check, 'check trial excel parse data');
1583         is_deeply($entry_numbers, $entry_numbers_check, 'check trial excel entry numbers');
1585         my $trial_create = CXGN::Trial::TrialCreate
1586                 ->new({
1587                 chado_schema      => $c->bcs_schema(),
1588                 dbh               => $c->dbh(),
1589                 owner_id          => 41,
1590                 trial_year        => "2016",
1591                 trial_description => "Trial Upload Test Flexible",
1592                 trial_location    => "test_location",
1593                 trial_name        => $trial_name,
1594                 design_type       => "RCBD",
1595                 design            => $parsed_data,
1596                 program           => "test",
1597                 upload_trial_file => $archived_filename_with_path,
1598                 operator          => "janedoe"
1599         });
1601         my $save = $trial_create->save_trial();
1603         ok($save->{'trial_id'}, "check that trial_create worked");
1604         my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
1605         ok($project_name == $trial_name, "check that trial_create really worked");
1607         my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
1608         ok($project_desc == "Trial Upload Test Flexible", "check that trial_create really worked");
1610         $f->clean_up_db();
1613 done_testing();