6 use SGN::Test::Fixture;
8 use Test::WWW::Mechanize;
12 use Spreadsheet::Read;
15 use Text::CSV ("csv");
17 my $f = SGN::Test::Fixture->new();
18 my $schema = $f->bcs_schema;
19 my $people_schema = $f->people_schema();
21 my $mech = Test::WWW::Mechanize->new;
26 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ], 'login with brapi call');
28 $response = decode_json $mech->content;
30 is($response->{'userDisplayName'}, 'Jane Doe', 'check login name');
32 # create a suitable dataset
34 my $ds = CXGN::Dataset->new( { schema=> $schema, people_schema => $people_schema });
36 $ds->trials( [ 139, 141 ]);
39 my $dataset_id = $ds->sp_dataset_id();
41 $mech->get_ok('http://localhost:3010/tools/heritability', 'load heritability input page');
43 $mech->get_ok('http://localhost:3010/ajax/heritability/shared_phenotypes?dataset_id='.$dataset_id, 'get common traits for dataset');
45 my $sp_data = JSON::Any->decode($mech->content());
47 my $trait_id = $sp_data->{options}->[0]->[0];
49 $mech->get_ok('http://localhost:3010/ajax/heritability/generate_results?dataset_id='.$dataset_id.'&trait_id='.$trait_id, 'run the heritability analysis');
51 my $rdata = JSON::Any->decode($mech->content());
53 print STDERR "RDATA: ".Dumper($rdata);
55 ok($rdata->{h2Table}, "h2TableJson returned");
56 ok($rdata->{h2CsvTable}, "h2CsvTable returned");
58 # check if files were created
59 ok( -e "static/".$rdata->{h2Table}, "table created");
60 ok( -e "static/".$rdata->{h2CsvTable}, "csv table file created");
62 my $test_basic_h2_file = csv(in => "static/".$rdata->{h2CsvTable});
64 is(@$test_basic_h2_file[1]->[1], 'dry matter content percentage', "check value of row name in a table");
65 is(@$test_basic_h2_file[1]->[5], '49.92', "check value of Vres fresh.root.weight in a table");
67 # run test for dataset with outliers but with false outliers parameter
68 my $outliers_included_dataset_id = 1;
69 my $outliers_included_trait_id = "fresh root weight";
70 $mech->get_ok('http://localhost:3010/ajax/heritability/generate_results?dataset_id='.$outliers_included_dataset_id.'&trait_id='.$outliers_included_trait_id, 'run the heritability analysis');
72 my $rdata_full_set = JSON::Any->decode($mech->content());
73 ok($rdata_full_set->{h2Table}, "h2TableJson returned");
74 ok($rdata_full_set->{h2CsvTable}, "h2CsvTable returned");
76 my $test_included_h2_file = csv(in => "static/".$rdata_full_set->{h2CsvTable});
77 is(@$test_included_h2_file[2]->[1], 'fresh root weight', "check value of row name in a table");
78 is(@$test_included_h2_file[2]->[5], '15.542', "check value of Heritability fresh.root.weight in a table");
80 # run test for dataset with outliers but with false outliers parameter
81 my $outliers_excluded_dataset_id = 1;
82 my $outliers_excluded_trait_id = "dry matter content percentage";
84 # run test for dataset with outliers but with true outliers parameter
85 $mech->get_ok('http://localhost:3010/ajax/heritability/generate_results?dataset_id='.$outliers_excluded_dataset_id.'&trait_id='.$outliers_excluded_trait_id.'&dataset_trait_outliers=1', 'run the heritability analysis');
86 my $rdata_excluded_set = JSON::Any->decode($mech->content());
88 # check if names are created in jsn response
89 ok($rdata_excluded_set->{h2Table}, "h2TableJson returned");
90 ok($rdata_excluded_set->{h2CsvTable}, "h2CsvTable returned");
92 # check if files were created
93 ok( -e "static/".$rdata_excluded_set->{h2Table}, "table created");
94 ok( -e "static/".$rdata_excluded_set->{h2CsvTable}, "csv table file created");
96 # check if values are changed for set with outliers excluded
97 my $test_excluded_h2_file = csv(in => "static/".$rdata_excluded_set->{h2CsvTable});
98 is(@$test_excluded_h2_file[2]->[1], 'fresh root weight', "check value of row name in a table");
99 is(@$test_excluded_h2_file[2]->[5], '8.864', "check value of Heritability fresh.root.weight in a table");
101 # remove changes to the database