Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / t / unit_fixture / CXGN / Stock / MergeStock.t
blob53c7e055465de77cb48010dcc8a96a9f0cbcc092
2 use strict;
4 use lib 't/lib';
6 use Test::More qw| no_plan|;
7 use Data::Dumper;
8 use SGN::Test::Fixture;
9 use CXGN::Stock;
11 my $f = SGN::Test::Fixture->new();
12 my $schema = $f->bcs_schema();
14 $schema->txn_begin();
16 eval {
17     my $this_stock_id = 39041;
18     my $other_stock_id = 38844;
19     
20     my $stock = CXGN::Stock->new( { schema => $schema, stock_id => $this_stock_id } );
21     
22     my $initial_counts = get_counts($this_stock_id);
23     
24     # try self merge, should fail:
25     my $error = $stock->merge($this_stock_id);
26     is($error, "Error: cannot merge stock into itself", "merge stock into itself test");
27     
28     my $initial_counts_other = get_counts($other_stock_id);
29     
30     $error = $stock->merge(38844);
31     is($error, undef, "merge stock should give no error");
32     
33     # all data should be transferred, so these need to add up
34     my $combined_counts = get_counts($this_stock_id);
35         
36     $initial_counts_other->{prop_count}++; # take added synonym into account
37     
38     foreach my $k (keys %$combined_counts) {
39         print STDERR "Checking key $k...\n";
40         is($combined_counts->{$k}, ($initial_counts->{$k} + $initial_counts_other->{$k}), "$k test");
41     }  
44 print STDERR "ERROR = $@\n";
45 $schema->txn_rollback();
46     
47 done_testing();
50 sub get_counts {
51     my $stock_id = shift;
52     my $stock_rel_object_id_count = $schema->resultset("Stock::StockRelationship")->search( { object_id => $stock_id })->count();
53     my $stock_rel_subject_id_count = $schema->resultset("Stock::StockRelationship")->search( { subject_id => $stock_id })->count();
54     my $stock_nd_experiment_count = $schema->resultset("NaturalDiversity::NdExperimentStock")->search( { stock_id => $stock_id })->count();
55     my $stock_prop_count = $schema->resultset("Stock::Stockprop")->search( { stock_id => $stock_id })->count();
57     my $data =  {
58         object_id_count => $stock_rel_object_id_count,
59         subject_id_count => $stock_rel_subject_id_count,
60         nd_experiment_count => $stock_nd_experiment_count,
61         prop_count => $stock_prop_count,
62     };
64     print STDERR Dumper($data);
66     return $data;