start fixing test for multi cat phenotype upload.
[sgn.git] / t / unit_fixture / CXGN / VerifyDeletion / TrialDeletion.t
bloba97a763d109d34622f83c037b180131cf4611f1a
3 # Tests trial deletion functions as they are used from the trial detail page through AJAX requests
5 use strict;
6 use warnings;
8 use lib 't/lib';
9 use SGN::Test::Fixture;
10 use Test::More;
11 use Test::WWW::Mechanize;
13 #Needed to update IO::Socket::SSL
14 use Data::Dumper;
15 use JSON;
16 use URI::Encode qw(uri_encode uri_decode);
17 use CXGN::Chado::Stock;
18 use LWP::UserAgent;
19 use CXGN::List;
20 use CXGN::Stock::Accession;
21 use CXGN::Trial;
22 local $Data::Dumper::Indent = 0;
24 my $f = SGN::Test::Fixture->new();
25 my $schema = $f->bcs_schema;
26 my $metadata_schema = $f->metadata_schema;
27 my $phenome_schema = $f->phenome_schema;
29 my $mech = Test::WWW::Mechanize->new;
30 my $response;
31 my $json = JSON->new->allow_nonref;
33 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ]);
34 $response = decode_json $mech->content;
35 print STDERR Dumper $response;
36 is($response->{'metadata'}->{'status'}->[2]->{'message'}, 'Login Successfull');
37 my $sgn_session_id = $response->{access_token};
39 my $project_rs = $schema->resultset("Project::Project")->find({name=>'CASS_6Genotypes_Sampling_2015'});
40 my $project_id = $project_rs->project_id;
42 my $trial = CXGN::Trial->new({
43     bcs_schema => $schema,
44     metadata_schema => $metadata_schema,
45     phenome_schema => $phenome_schema,
46     trial_id => $project_id
47 });
49 my $rs = $schema->resultset('Phenotype::Phenotype')->search();
50 my $total_phenotypes_in_db_before = $rs->count();
52 my $phenotype_count_before = $trial->phenotype_count();
54 my $traits_assayed = $trial->get_traits_assayed();
55 my @trait_ids;
56 foreach (@$traits_assayed){
57     push @trait_ids, $_->[0];
61 $mech->post_ok('http://localhost:3010/ajax/breeders/trial/'.$project_id.'/delete_single_trait', [ "traits_id"=> encode_json(\@trait_ids) ]);
62 $response = decode_json $mech->content;
63 print STDERR Dumper $response;
64 is_deeply($response, {'success' => 1});
65 sleep(5);
67 my $phenotype_count_after_single_trait_delete = $trial->phenotype_count();
71 print STDERR "PHENO COUNTS: $phenotype_count_before, $phenotype_count_after_single_trait_delete\n";
72 $mech->get_ok('http://localhost:3010/ajax/breeders/trial/'.$project_id.'/delete/phenotypes');
73 $response = decode_json $mech->content;
74 print STDERR Dumper $response;
75 my $phenotype_count_after_delete = $trial->phenotype_count();
76 is($phenotype_count_after_delete, 0, 'successfull phenotype deletion');
78 sleep(10);
80 my $phenotypes_deleted = $phenotype_count_before - $phenotype_count_after_delete;
82 my $rs = $schema->resultset('Phenotype::Phenotype')->search();
83 my $total_phenotypes_in_db_after = $rs->count();
85 is($total_phenotypes_in_db_before - $total_phenotypes_in_db_after, $phenotypes_deleted, 'check if only trial phenotypes were deleted');
87 $mech->get_ok('http://localhost:3010/ajax/breeders/trial/'.$project_id.'/delete/layout');
88 sleep(10);
90 #$mech->get_ok('http://localhost:3010/ajax/breeders/trial/'.$project_id.'/delete/layout');
91 #$response = decode_json $mech->content;
92 #print STDERR Dumper $response;
93 #ok($response->{'error'});
95 #sleep(10);
97 $response = decode_json $mech->content;
98 print STDERR Dumper $response;
99 is_deeply($response, {'message' => 'Successfully deleted trial data.','success' => 1}, 'test trial layout + entry deletion');
101 my $project_rs_after_delete = $schema->resultset("Project::Project")->find({name=>'CASS_6Genotypes_Sampling_2015'});
102 ok(!$project_rs_after_delete);
104 my $plot_rs_after_delete = $schema->resultset("Stock::Stock")->find({name=>'CASS_6Genotypes_103'});
105 ok(!$plot_rs_after_delete);
107 done_testing();