Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / t / unit_fixture / CXGN / Onto.t
blob300868dc59da7871df096d4a9bb8167854e45e55
2 use strict;
4 use lib 't/lib';
6 use Test::More;
7 use Data::Dumper;
8 use SGN::Test::Fixture;
9 use CXGN::Onto;
10 use SGN::Model::Cvterm;
11 use Test::WWW::Mechanize;
12 use JSON;
14 my $mech = Test::WWW::Mechanize->new;
15 my $json = JSON->new->allow_nonref;
17 my $t = SGN::Test::Fixture->new();
18 my $schema = $t->bcs_schema();
20 my $onto = CXGN::Onto->new( { schema => $t->bcs_schema() });
22 my $cv_type = 'attribute_ontology';
23 my @results = $onto->get_root_nodes($cv_type);
24 #print STDERR "test 1 results " .Dumper @results;
26 is_deeply(@results, [
27                59,
28                'CHEBI:00000 chebi_compounds'
29              ], 'onto get root nodes test');
31 =for comment
32 my $cv_id = 58; # CASSTISS ontology with cvprop 'object'
33 @results = $onto->get_terms($cv_id);
34 print STDERR "test 2 results: @results";
35 my @cass_tissues = ["[77181,'cass fibrous root|CASSTISS:0000011']",
36                     "[77176,'cass lower leaf|CASSTISS:0000006']",
37                     "[77171,'cass lower stem bark|CASSTISS:0000010']",
38                     "[77174,'cass lower stem whole|CASSTISS:0000009']",
39                     "[77178,'cass pre-storage root|CASSTISS:0000012']",
40                     "[77180,'cass sink leaf|CASSTISS:0000004']",
41                     "[77170,'cass source leaf|CASSTISS:0000005']",
42                     "[77168,'cass storage root|CASSTISS:0000013']",
43                     "[77172,'cass upper stem|CASSTISS:0000007']"];
44                     # problem with this data structure
45 is_deeply(@results, @cass_tissues, 'onto get terms test');
46 =cut
48 my @allowed_composed_cvs = ('trait','toy');
49 my $composable_cvterm_delimiter = '|';
50 my $composable_cvterm_format = 'concise';
51 my $traits1 = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
52     object => [],
53     attribute => [],
54     method => [],
55     unit => [],
56     trait => [70775,76555],
57     tod => [],
58     toy => [77526,77499],
59     gen => [],
60 });
61 #print STDERR Dumper $traits1;
62 is_deeply($traits1, {
63           'existing_traits' => [],
64           'new_traits' => [
65                             [
66                               [
67                                 70775,
68                                 77526
69                               ],
70                               'anthocyanin pigmentation visual rating 0-3|month 1'
71                             ],
72                             [
73                               [
74                                 70775,
75                                 77499
76                               ],
77                               'anthocyanin pigmentation visual rating 0-3|month 10'
78                             ],
79                             [
80                               [
81                                 76555,
82                                 77526
83                               ],
84                               'ease of harvest assessment 1-3|month 1'
85                             ],
86                             [
87                               [
88                                 76555,
89                                 77499
90                               ],
91                               'ease of harvest assessment 1-3|month 10'
92                             ]
93                           ]
94         }, 'check composed trait creation 1');
96 @allowed_composed_cvs = ('trait','toy','tod','object');
97 $composable_cvterm_delimiter = '||';
98 $composable_cvterm_format = 'concise';
99 my $traits2 = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
100   object => [77176, 77180],
101   attribute => [],
102   method => [],
103   unit => [],
104   trait => [70775,76555],
105   tod => [77536],
106   toy => [77526,77499],
107   gen => [],
109 #print STDERR Dumper $traits2;
110 is_deeply($traits2, {
111           'existing_traits' => [],
112           'new_traits' => [
113                             [
114                               [
115                                 70775,
116                                 77499,
117                                 77536,
118                                 77176
119                               ],
120                               'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass lower leaf'
121                             ],
122                             [
123                               [
124                                 70775,
125                                 77499,
126                                 77536,
127                                 77180
128                               ],
129                               'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass sink leaf'
130                             ],
131                             [
132                               [
133                                 70775,
134                                 77526,
135                                 77536,
136                                 77176
137                               ],
138                               'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass lower leaf'
139                             ],
140                             [
141                               [
142                                 70775,
143                                 77526,
144                                 77536,
145                                 77180
146                               ],
147                               'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass sink leaf'
148                             ],
149                             [
150                               [
151                                 76555,
152                                 77499,
153                                 77536,
154                                 77176
155                               ],
156                               'ease of harvest assessment 1-3||month 10||end of day||cass lower leaf'
157                             ],
158                             [
159                               [
160                                 76555,
161                                 77499,
162                                 77536,
163                                 77180
164                               ],
165                               'ease of harvest assessment 1-3||month 10||end of day||cass sink leaf'
166                             ],
167                             [
168                               [
169                                 76555,
170                                 77526,
171                                 77536,
172                                 77176
173                               ],
174                               'ease of harvest assessment 1-3||month 1||end of day||cass lower leaf'
175                             ],
176                             [
177                               [
178                                 76555,
179                                 77526,
180                                 77536,
181                                 77180
182                               ],
183                               'ease of harvest assessment 1-3||month 1||end of day||cass sink leaf'
184                             ]
185                           ]
186         }, 'check composed trait creation 2');
188 @allowed_composed_cvs = ('trait','toy','tod','object');
189 $composable_cvterm_delimiter = '||';
190 $composable_cvterm_format = 'extended';
191 my $traits3 = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
192   object => [77171, 77174],
193   attribute => [],
194   method => [],
195   unit => [],
196   trait => [70775,76555],
197   tod => [77536],
198   toy => [77526,77499],
199   gen => [],
201 #print STDERR Dumper $traits3;
202 is_deeply($traits3, {
203           'existing_traits' => [],
204           'new_traits' => [
205                             [
206                               [
207                                 70775,
208                                 77499,
209                                 77536,
210                                 77171
211                               ],
212                               'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
213                             ],
214                             [
215                               [
216                                 70775,
217                                 77499,
218                                 77536,
219                                 77174
220                               ],
221                               'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
222                             ],
223                             [
224                               [
225                                 70775,
226                                 77526,
227                                 77536,
228                                 77171
229                               ],
230                               'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
231                             ],
232                             [
233                               [
234                                 70775,
235                                 77526,
236                                 77536,
237                                 77174
238                               ],
239                               'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
240                             ],
241                             [
242                               [
243                                 76555,
244                                 77499,
245                                 77536,
246                                 77171
247                               ],
248                               'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
249                             ],
250                             [
251                               [
252                                 76555,
253                                 77499,
254                                 77536,
255                                 77174
256                               ],
257                               'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
258                             ],
259                             [
260                               [
261                                 76555,
262                                 77526,
263                                 77536,
264                                 77171
265                               ],
266                               'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
267                             ],
268                             [
269                               [
270                                 76555,
271                                 77526,
272                                 77536,
273                                 77174
274                               ],
275                               'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
276                             ]
277                           ]
278         }, 'check composed trait creation 3');
280 my $new_traits2 = $traits2->{new_traits};
281 my %new_composed_terms2;
282 foreach (@$new_traits2){
283   $new_composed_terms2{$_->[1]} = join ',', @{$_->[0]};
285 my $results2 = $onto->store_composed_term(\%new_composed_terms2);
286 #print STDERR Dumper $results2;
287 my @results2_names;
288 foreach (@$results2){
289     ok($_->[0], 'check that cvterm_id saved');
290     push @results2_names, $_->[1];
292 print STDERR Dumper \@results2_names;
293 is_deeply(\@results2_names, [
294           'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass lower leaf|COMP:0000014',
295           'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass sink leaf|COMP:0000015',
296           'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass lower leaf|COMP:0000016',
297           'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass sink leaf|COMP:0000017',
298           'ease of harvest assessment 1-3||month 10||end of day||cass lower leaf|COMP:0000018',
299           'ease of harvest assessment 1-3||month 10||end of day||cass sink leaf|COMP:0000019',
300           'ease of harvest assessment 1-3||month 1||end of day||cass lower leaf|COMP:0000020',
301           'ease of harvest assessment 1-3||month 1||end of day||cass sink leaf|COMP:0000021'
302         ], 'check store composed terms 2');
304 my $new_traits3 = $traits3->{new_traits};
305 my %new_composed_terms3;
306 foreach (@$new_traits3){
307     $new_composed_terms3{$_->[1]} = join ',', @{$_->[0]};
309 my $results3 = $onto->store_composed_term(\%new_composed_terms3);
310 #print STDERR Dumper $results3;
311 my @results3_names;
312 foreach (@$results3){
313     ok($_->[0], 'check that cvterm_id saved');
314     push @results3_names, $_->[1];
316 print STDERR Dumper \@results3_names;
317 is_deeply(\@results3_names, [
318           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000022',
319           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000023',
320           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000024',
321           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000025',
322           'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000026',
323           'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000027',
324           'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000028',
325           'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000029'
326         ], 'test save composed terms 3');
328 my $traits3_duplicate = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
329   object => [77171, 77174],
330   attribute => [],
331   method => [],
332   unit => [],
333   trait => [70775,76555],
334   tod => [77536],
335   toy => [77526,77499],
336   gen => [],
338 #print STDERR Dumper $traits3_duplicate;
339 my $new_traits = $traits3_duplicate->{new_traits};
340 my $existing_traits = $traits3_duplicate->{existing_traits};
341 my @check_names;
342 foreach (@$existing_traits){
343     ok($_->[0], 'check that cvterm_id saved');
344     push @check_names, $_->[1];
346 is_deeply($new_traits, [], 'check that duplicate traits are not added');
347 print STDERR Dumper \@check_names;
348 is_deeply(\@check_names, [
349           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000022',
350           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000023',
351           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000024',
352           'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000025',
353           'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000026',
354           'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000027',
355           'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000028',
356           'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000029'
357         ], 'check that duplicate traits are separated from new_traits');
360 ## Test adding observation variables, traits, methods, scales
362 my $response;
363 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ]);
364 $response = decode_json $mech->content;
365 print STDERR Dumper $response;
366 my $sgn_session_id = $response->{access_token};
367 print STDERR $sgn_session_id."\n";
369 my $q = "Select db_id FROM db where name = 'CO_334';";
370 my $sth = $schema->storage->dbh->prepare($q);
371 $sth->execute();
372 my ($db_id) = $sth->fetchrow_array();
374 $mech->post_ok('http://localhost:3010/ajax/onto/store_trait_method_scale_observation_variable',
375     [
376         "sgn_session_id"=>$sgn_session_id,
377         "selected_observation_variable_db_id"=> $db_id,
378         "new_observation_variable_name"=> "new observation variable name",
379         "new_observation_variable_definition"=> "new observation variable definition",
380         "selected_trait_db_id"=> $db_id,
381         "selected_trait_cvterm_id"=> undef,
382         "new_trait_name"=> "new trait name",
383         "new_trait_definition"=> "new trait definition",
384         "selected_method_db_id"=> $db_id,
385         "selected_method_cvterm_id"=> undef,
386         "new_method_name"=> "new method name",
387         "new_method_definition"=> "new method definition",
388         "selected_scale_db_id"=> $db_id,
389         "selected_scale_cvterm_id"=> undef,
390         "new_scale_name"=> "new scale name",
391         "new_scale_definition"=> "new scale definition",
392         "new_scale_format"=> "",
393         "new_scale_minimum"=> "",
394         "new_scale_maximum"=> "",
395         "new_scale_default"=> "",
396         "new_scale_categories"=> ""
397     ]
399 $response = decode_json $mech->content;
400 print STDERR Dumper $response;
401 ok($response->{success});
403 $q = "Select cvterm_id FROM cvterm where name = 'new trait name';";
404 $sth = $schema->storage->dbh->prepare($q);
405 $sth->execute();
406 my ($trait_cvterm_id) = $sth->fetchrow_array();
408 $mech->post_ok('http://localhost:3010/ajax/onto/store_trait_method_scale_observation_variable',
409     [
410         "selected_observation_variable_db_id"=> $db_id,
411         "new_observation_variable_name"=> "new observation variable name 2",
412         "new_observation_variable_definition"=> "new observation variable definition 2",
413         "selected_trait_db_id"=> $db_id,
414         "selected_trait_cvterm_id"=> $trait_cvterm_id,
415         "new_trait_name"=> "",
416         "new_trait_definition"=> "",
417         "selected_method_db_id"=> $db_id,
418         "selected_method_cvterm_id"=> "",
419         "new_method_name"=> "new method name 2",
420         "new_method_definition"=> "new method definition 2",
421         "selected_scale_db_id"=> $db_id,
422         "selected_scale_cvterm_id"=> "",
423         "new_scale_name"=> "new scale name 2",
424         "new_scale_definition"=> "new scale definition 2",
425         "new_scale_format"=> "",
426         "new_scale_minimum"=> "",
427         "new_scale_maximum"=> "",
428         "new_scale_default"=> "",
429         "new_scale_categories"=> ""
430     ]
432 $response = decode_json $mech->content;
433 print STDERR Dumper $response;
434 ok($response->{success});
436 $mech->post_ok('http://localhost:3010/ajax/onto/store_ontology_identifier',
437     [
438         "sgn_session_id"=>$sgn_session_id,
439         "ontology_name"=> "NewOntology1",
440         "ontology_description"=> "new ontology",
441         "ontology_identifier"=> "NOO1",
442         "ontology_type"=> "method_ontology",
443     ]
445 $response = decode_json $mech->content;
446 print STDERR Dumper $response;
447 ok($response->{success});
449 done_testing();