combined all new cvterms
[sgn.git] / t / unit_mech / AJAX / Transformation.t
blob0cc4bf02ef938b986b137cb1bfd7333b535980eb
1 use strict;
2 use warnings;
4 use lib 't/lib';
5 use SGN::Test::Fixture;
6 use Test::More;
7 use Test::WWW::Mechanize;
8 use Data::Dumper;
9 use JSON;
10 use SGN::Model::Cvterm;
11 use CXGN::Transformation::Transformation;
13 local $Data::Dumper::Indent = 0;
15 my $f = SGN::Test::Fixture->new();
16 my $schema = $f->bcs_schema;
17 my $dbh = $schema->storage->dbh;
18 my $people_schema = $f->people_schema;
19 my $phenome_schema = $f->phenome_schema;
21 my $mech = Test::WWW::Mechanize->new;
22 my $json = JSON->new->allow_nonref;
23 my @all_new_stocks;
25 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ]);
26 my $response = decode_json $mech->content;
27 is($response->{'metadata'}->{'status'}->[2]->{'message'}, 'Login Successfull');
28 my $sgn_session_id = $response->{access_token};
30 my $location_rs = $schema->resultset('NaturalDiversity::NdGeolocation')->search({description => 'Cornell Biotech'});
31 my $location_id = $location_rs->first->nd_geolocation_id;
33 my $bp_rs = $schema->resultset('Project::Project')->search({name => 'test'});
34 my $breeding_program_id = $bp_rs->first->project_id;
36 #test adding project
37 $mech->post_ok('http://localhost:3010/ajax/transformation/add_transformation_project', [ 'project_name' => 'transformation_project_1', 'project_program_id' => 134,
38     'project_location' => 'test_location', 'year' => '2024', 'project_description' => 'test transformation' ]);
40 $response = decode_json $mech->content;
41 is($response->{'success'}, '1');
43 my $project_rs = $schema->resultset('Project::Project')->find({ name => 'transformation_project_1' });
44 my $project_id = $project_rs->project_id();
46 #test adding transformation id
48 #adding vector construct for testing
49 my $vector_construct_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "vector_construct", "stock_type")->cvterm_id();
50 ok(my $organism = $schema->resultset("Organism::Organism")->find_or_create({
51     genus   => 'Test_genus',
52     species => 'Test_genus test_species',
53 },));
55 my $new_vector_construct = $schema->resultset('Stock::Stock')->create({
56     organism_id => $organism->organism_id,
57     name => 'TT1',
58     uniquename => 'TT1',
59     type_id     => $vector_construct_type_id,
60 });
62 my $vector_construct_rs = $schema->resultset('Stock::Stock')->find({ name => 'TT1' });
63 my $vector_stock_id = $vector_construct_rs->stock_id();
64 push @all_new_stocks, $vector_stock_id;
66 my $transformation_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "transformation", "stock_type")->cvterm_id();
67 my $before_adding_transformation_id = $schema->resultset("Stock::Stock")->search({ type_id => $transformation_type_id })->count();
68 my $before_adding_transformation_id_relationship = $schema->resultset("Stock::StockRelationship")->search({})->count();
70 $mech->post_ok('http://localhost:3010/ajax/transformation/add_transformation_identifier', [ 'transformation_identifier' => 'UG1TT1', 'plant_material' => 'UG120001', 'vector_construct' => 'TT1', 'notes' => 'test', 'transformation_project_id' => $project_id]);
72 $response = decode_json $mech->content;
73 is($response->{'success'}, '1');
75 my $transformation_rs = $schema->resultset('Stock::Stock')->find({name => 'UG1TT1'});
76 my $transformation_stock_id = $transformation_rs->stock_id();
77 push @all_new_stocks, $transformation_stock_id;
79 my $after_adding_transformation_id = $schema->resultset("Stock::Stock")->search({ type_id => $transformation_type_id })->count();
80 is($after_adding_transformation_id, $before_adding_transformation_id + 1);
81 my $after_adding_transformation_id_relationship = $schema->resultset("Stock::StockRelationship")->search({})->count();
82 is($after_adding_transformation_id_relationship, $before_adding_transformation_id_relationship + 2);
84 #test adding transformants (accessions)
85 $mech->post_ok('http://localhost:3010/ajax/transformation/add_transformants', [ 'transformation_name' => 'UG1TT1', 'transformation_stock_id' => $transformation_stock_id, 'new_name_count' => 2, 'last_number' => 0 ]);
87 $response = decode_json $mech->content;
88 is($response->{'success'}, '1');
90 #retrieving transformation info
91 $mech->post_ok("http://localhost:3010/ajax/transformation/active_transformations_in_project/$project_id");
93 $response = decode_json $mech->content;
94 my $transformation = $response->{'data'};
95 my $transformation_id_count = scalar(@$transformation);
96 is($transformation_id_count, '1');
98 $mech->post_ok("http://localhost:3010/ajax/transformation/transformants/$transformation_stock_id");
100 $response = decode_json $mech->content;
101 my $transformants = $response->{'data'};
102 my $transformant_count = scalar(@$transformants);
103 is($transformant_count, '2');
105 #retrieving related stocks for vector page
106 $mech->get_ok("http://localhost:3010/stock/$vector_stock_id/datatables/vector_related_stocks");
107 $response = decode_json $mech->content;
108 my $related_stocks = $response->{'data'};
109 my $related_stock_count = scalar(@$related_stocks);
110 is($related_stock_count, '2');
112 #deleting project, transformation_id, vector_construct, transformants
113 my $project_owner = $phenome_schema->resultset('ProjectOwner')->find({ project_id => $project_id });
114 $project_owner->delete();
115 $project_rs->delete();
117 my $transformation_obj = CXGN::Transformation::Transformation->new({schema=>$schema, dbh=>$dbh, transformation_stock_id=>$transformation_stock_id});
118 my $result = $transformation_obj->get_transformants();
119 foreach my $transformant (@$result) {
120     push @all_new_stocks, $transformant->[0];
122 #print STDERR "ALL NEW STOCKS =".Dumper(\@all_new_stocks)."\n";
123 my $dbh = $schema->storage->dbh;
124 my $q = "delete from phenome.stock_owner where stock_id=?";
125 my $h = $dbh->prepare($q);
127 foreach (@all_new_stocks){
128     my $row  = $schema->resultset('Stock::Stock')->find({stock_id=>$_});
129     $h->execute($_);
130     $row->delete();
134 $f->clean_up_db();
137 done_testing();