6 A piece of code to test the CXGN::Metadata::Groups module
14 Note: To run the complete test the database connection should be done as
16 (web_usr have not privileges to insert new data into the sed tables)
20 this test need some environment variables:
22 export METADATA_TEST_METALOADER='metaloader user'
23 export METADATA_TEST_DBDSN='database dsn as:
24 dbi:DriverName:database=database_name;host=hostname;port=port'
27 export METADATA_TEST_DBDSN='dbi:Pg:database=sandbox;host=localhost;'
29 export METADATA_TEST_DBUSER='database user with insert permissions'
30 export METADATA_TEST_DBPASS='database password'
34 This script check 103 variables to test the right operation of the
35 CXGN::Metadata::Groups module:
37 + Tests from 1 to 4 - use of modules;
38 + Tests from 5 to 8 - BASIC SET/GET FUNCTIONS
39 + Test 9 - STORE FUNCTION with a new group row
40 + Tests from 10 to 19 - STORE FUNCTION METADBDATA INTERACTION
41 + Test 20 - IS OBSOLETE FUNCTION TEST
42 + Test 21 and 22 - STORE FUNCTION with a modified group row
43 + Tests from 23 to 32 - STORE FUNCTION METADBDATA INTERACTION FOR MODIFICATIONS
44 + Test 33 - OBSOLETE FUNCTION TEST
45 + Tests from 34 to 43 - STORE FUNCTION METADBDATA INTERACTION FOR OBSOLETE
46 + Test 44 - REVERT OBSOLETE FUNCTION TEST
47 + Test from 45 to 54 - STORE FUNCTION METADBDATA INTERACTION FOR REVERT OBSOLETE
48 + Test from 55 to 61 - SET/GET MEMBER
49 + Test from 62 to 94 - STORE MEMBERS and STORE FUNCTION METADBDATA FOR MEMBER
50 + Test 95 and 96 - OBSOLETE MEMBER FUNCTION
51 + Test 97 - NEW_BY_GROUP_NAME CONSTRUCTOR
52 + Test 98 - ADD MEMBER METHOD,
53 + Test 99 - GENERAL STORE FUNCTION,
54 + Test 100 - GET MEMBERS FUNCTION with OBSOLETE TAG,
55 + Test 101 - GET MEMBERS FUNCTION with NON OBSOLETE TAG
56 + Test 102 - Warning for new_by_member
57 + Test 103 - New_by_member
63 Aureliano Bombarely Gomez
73 use Test
::More
;# qw | no_plan |; # while developing the test
77 use CXGN
::DB
::Connection
;
79 ## The tests still need search_path
81 my @schema_list = ('metadata', 'public');
82 my $schema_list = join(',', @schema_list);
83 my $set_path = "SET search_path TO $schema_list";
85 ## First check env. variables and connection
89 ## Env. variables have been changed to use biosource specific ones
91 my @env_variables = qw
/METADATA_TEST_METALOADER METADATA_TEST_DBDSN METADATA_TEST_DBUSER METADATA_TEST_DBPASS/;
93 ## RESET_DBSEQ is an optional env. variable, it doesn't need to check it
95 for my $env (@env_variables) {
96 unless (defined $ENV{$env}) {
97 plan skip_all
=> "Environment variable $env not set, aborting";
102 CXGN
::DB
::Connection
->new(
103 $ENV{METADATA_TEST_DBDSN
},
104 $ENV{METADATA_TEST_DBUSER
},
105 $ENV{METADATA_TEST_DBPASS
},
106 {on_connect_do
=> $set_path}
110 if ($@
=~ m/DBI connect/) {
112 plan skip_all
=> "Could not connect to database";
119 use_ok
('CXGN::Metadata::Schema'); ## TEST1
120 use_ok
('CXGN::Metadata::Groups'); ## TEST2
121 use_ok
('CXGN::Metadata::Metadbdata'); ## TEST3
122 use_ok
('CXGN::Metadata::Dbiref') ## TEST4
126 #if we cannot load the CXGN::Metadata::Schema module, no point in continuing
127 CXGN
::Metadata
::Schema
->can('connect')
128 or BAIL_OUT
('could not load the CXGN::Metadata::Schema module');
130 ## Prespecified variable
132 my $metadata_creation_user = $ENV{METADATA_TEST_METALOADER
};
134 ## The biosource schema contain all the metadata classes so don't need to create another Metadata schema
135 ## CXGN::DB::DBICFactory is obsolete, it has been replaced by CXGN::Metadata::Schema
137 my $schema = CXGN
::Metadata
::Schema
->connect( $ENV{METADATA_TEST_DBDSN
},
138 $ENV{METADATA_TEST_DBUSER
},
139 $ENV{METADATA_TEST_DBPASS
},
140 {on_connect_do
=> $set_path});
142 $schema->txn_begin();
145 ## Get the last values
146 my %nextval = $schema->get_nextval();
147 my $last_metadata_id = $nextval{'md_metadata'};
148 my $last_group_id = $nextval{'md_groups'};
149 my $last_dbiref_id = $nextval{'md_dbiref'};
151 ## Create a empty metadata object to use in the database store functions
152 my $metadbdata = CXGN
::Metadata
::Metadbdata
->new($schema, $metadata_creation_user);
153 my $creation_date = $metadbdata->get_object_creation_date();
154 my $creation_user_id = $metadbdata->get_object_creation_user_by_id();
156 ## FIRST TEST BLOCK (TEST FROM 5 TO 8)
157 ## This is the first group of tests, to check if an empty object can store and after can return the data
158 ## Create a new empty object;
160 my $group = CXGN
::Metadata
::Groups
->new($schema, undef);
162 ## Load of the eight different parameters for an empty object using a hash with keys=root name for tha function and
163 ## values=value to test
165 my %test_values_for_empty_object=( group_id
=> $last_group_id+1,
166 group_name
=> 'group test',
167 group_type
=> 'dbipath',
168 group_description
=> 'group testing using dbipaths',
171 ## Load the data in the empty object
172 my @function_keys = sort keys %test_values_for_empty_object;
173 foreach my $rootfunction (@function_keys) {
174 my $setfunction = 'set_' . $rootfunction;
175 if ($rootfunction eq 'group_id') {
176 $setfunction = 'force_set_' . $rootfunction;
178 $group->$setfunction($test_values_for_empty_object{$rootfunction});
180 ## Get the data from the object and store in two hashes. The first %getdata with keys=root_function_name and
181 ## value=value_get_from_object and the second, %testname with keys=root_function_name and values=name for the test.
183 my (%getdata, %testnames);
184 foreach my $rootfunction (@function_keys) {
185 my $getfunction = 'get_'.$rootfunction;
186 my $data = $group->$getfunction();
187 $getdata{$rootfunction} = $data;
188 my $testname = 'BASIC SET/GET FUNCTION for ' . $rootfunction.' test';
189 $testnames{$rootfunction} = $testname;
192 ## And now run the test for each function and value
194 foreach my $rootfunction (@function_keys) {
195 is
($getdata{$rootfunction}, $test_values_for_empty_object{$rootfunction}, $testnames{$rootfunction})
196 or diag
"Looks like this failed.";
199 ### SECOND TEST BLOCK
200 ### Use of store functions.
204 ### It will create a new object based in dbipath (TEST 9)
205 my $group2 = CXGN
::Metadata
::Groups
->new($schema);
206 $group2->set_group_name('group test2');
207 $group2->set_group_type('dbipath');
208 $group2->set_group_description('group testing using dbipaths');
212 my $group3_stored = $group2->store_group($metadbdata);
213 my $group3_id = $group3_stored->get_group_id();
215 is
($group3_id, $last_group_id+1, 'STORE FUNCTION with a new group row, checking group_id')
216 or diag
"Looks like this failed";
218 ## Checking the metadbdata associated to this new creation (TEST 10 to 19)
220 my $metadbdata3 = $group3_stored->get_metadbdata();
221 my %metadbdata3 = $metadbdata3->get_metadata_by_rows();
223 my %expected_metadata3 = ( metadata_id
=> $last_metadata_id+1,
224 create_date
=> $creation_date,
225 create_person_id
=> $creation_user_id,
229 foreach my $metadata_type3 (keys %metadbdata3) {
230 my $message3 = "STORE FUNCTION METADBDATA INTERACTION, get_metadbdata test, checking $metadata_type3";
232 is
($metadbdata3{$metadata_type3}, $expected_metadata3{$metadata_type3}, $message3)
233 or diag
"Looks like this failed";
236 ## Checking the is_obsolete function (TEST 20)
238 my $obsolete = $group3_stored->is_obsolete();
239 is
($obsolete, 0,"IS OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
241 ## Testing a modification in the row data (TEST 21 and 22)
243 $group3_stored->set_group_description('testing modifications in the description');
244 my $group4_modified = $group3_stored->store_group($metadbdata);
245 my $group4_id = $group4_modified->get_group_id();
247 is
($group4_id, $last_group_id+1, 'STORE FUNCTION with a modified group row, checking group_id')
248 or diag
"Looks like this failed";
250 my $group4_desc = $group4_modified->get_group_description();
251 is
($group4_desc, 'testing modifications in the description', 'STORE FUNCTION with a modified group row, checking group_description')
252 or diag
"Looks like this failed";
254 ## Checking the metadbdata associated to this modification (TEST 23 to 32)
256 my $metadbdata4 = $group4_modified->get_metadbdata();
257 my %metadbdata4 = $metadbdata4->get_metadata_by_rows();
258 my %expected_metadata4 = ( metadata_id
=> $last_metadata_id+2,
259 create_date
=> $creation_date,
260 create_person_id
=> $creation_user_id,
261 modified_date
=> $creation_date,
262 modified_person_id
=> $creation_user_id,
263 modification_note
=> 'set value in group_description column',
264 previous_metadata_id
=> $last_metadata_id+1,
268 foreach my $metadata_type4 (keys %metadbdata4) {
269 my $message4 = "STORE FUNCTION METADBDATA INTERACTION FOR MODIFICATIONS, get_metadbdata test, checking $metadata_type4";
270 is
($metadbdata4{$metadata_type4}, $expected_metadata4{$metadata_type4}, $message4) or diag
"Looks like this failed";
273 ## Test the obsolete function (TEST 33)
275 my $group5_obsolete = $group4_modified->obsolete_group($metadbdata, 'change to obsolete test');
276 my $obsolete5 = $group5_obsolete->is_obsolete();
277 is
($obsolete5, 1,"OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
279 ## Checking the metadata associated to this obsolete change (TEST 34 to 43)
281 my $metadbdata5 = $group5_obsolete->get_metadbdata();
282 my %metadbdata5 = $metadbdata5->get_metadata_by_rows();
283 my %expected_metadata5 = ( metadata_id
=> $last_metadata_id+3,
284 create_date
=> $creation_date,
285 create_person_id
=> $creation_user_id,
286 modified_date
=> $creation_date,
287 modified_person_id
=> $creation_user_id,
288 modification_note
=> 'change to obsolete',
289 previous_metadata_id
=> $last_metadata_id+2,
291 obsolete_note
=> 'change to obsolete test'
294 foreach my $metadata_type5 (keys %metadbdata5) {
296 my $message5 = "STORE FUNCTION METADBDATA INTERACTION FOR OBSOLETE, get_metadbdata test, checking $metadata_type5";
297 is
($metadbdata5{$metadata_type5}, $expected_metadata5{$metadata_type5}, $message5) or diag
"Looks like this failed";
300 ## Test the REVERT tag for the obsolete function (TEST 44)
302 my $group6_revert = $group5_obsolete->obsolete_group($metadbdata, 'revert obsolete test', 'REVERT');
303 my $obsolete6 = $group6_revert->is_obsolete();
304 is
($obsolete6, 0,"REVERT OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
306 ## Checking the metadata associated to this obsolete change (TEST 45 to 54)
308 my $metadbdata6 = $group6_revert->get_metadbdata();
309 my %metadbdata6 = $metadbdata6->get_metadata_by_rows();
310 my %expected_metadata6 = ( metadata_id
=> $last_metadata_id+4,
311 create_date
=> $creation_date,
312 create_person_id
=> $creation_user_id,
313 modified_date
=> $creation_date,
314 modified_person_id
=> $creation_user_id,
315 modification_note
=> 'revert obsolete',
316 previous_metadata_id
=> $last_metadata_id+3,
318 obsolete_note
=> 'revert obsolete test'
321 foreach my $metadata_type6 (keys %metadbdata6) {
322 my $message6 = "STORE FUNCTION METADBDATA INTERACTION FOR REVERT OBSOLETE, get_metadbdata test, checking $metadata_type6";
324 is
($metadbdata6{$metadata_type6}, $expected_metadata6{$metadata_type6}, $message6) or diag
"Looks like this failed";
327 ### THIRD BLOCK, test the member code
329 ## First, we need to add some dbiref's. It will create three based in md_metadata table with the metadata_id
330 ## created before. (TEST 55 to 61)
333 my @metadata_ids = ($last_metadata_id+2, $last_metadata_id+3, $last_metadata_id+4);
335 my $dbipath_id = CXGN
::Metadata
::Dbipath
->new_by_path( $schema,
336 ['metadata', 'md_metadata', 'metadata_id'] )
340 foreach my $met_id (@metadata_ids) {
341 my $dbiref = CXGN
::Metadata
::Dbiref
->new($schema, undef);
342 $dbiref->set_accession($met_id);
343 $dbiref->set_dbipath_id($dbipath_id);
345 my $dbiref_id = $dbiref->store($metadbdata)
348 push @dbiref_list, $dbiref_id;
351 my $group7 = CXGN
::Metadata
::Groups
->new($schema, $last_group_id+1);
352 $group7->set_member_ids(\
@dbiref_list);
354 my @member_ids = $group7->get_member_ids();
355 my $obj_member_ids = join(',', sort @member_ids);
356 my $exp_member_ids = join(',', sort @dbiref_list);
358 is
($obj_member_ids, $exp_member_ids, 'SET/GET MEMBER IDS over the group object, checking dbiref_id list')
359 or diag
"Looks like this failed";
361 my @members = $group7->get_members();
364 foreach my $member (@members) {
365 my @dbipath_elements = $member->get_dbipath_obj()
368 my $g_dbipath_name = join('.', @dbipath_elements);
369 my $g_accession = $member->get_accession();
372 is
($g_accession, $metadata_ids[$n], "SET/GET MEMBER $c the group object, checking dbiref object (dbiref_id)")
373 or diag
"Looks like this failed";
377 is
($g_dbipath_name, 'metadata.md_metadata.metadata_id', "SET/GET MEMBER $c over group object, checking dbiref object (dbipath)")
378 or diag
"Looks like this failed";
381 ## Test store member functions (TEST 62 to 94)
383 my $group8_stored = $group7->store_members($metadbdata);
384 my @stored_members = $group8_stored->get_members();
385 my %member_metadata = $group8_stored->get_metadbdata_for_members($metadbdata);
387 my $test1 = join(',', keys %member_metadata);
388 my $test2 = join(',', values %member_metadata);
393 foreach my $stored_member (@stored_members) {
394 my $s_accession = $stored_member->get_accession();
395 my $s_dbiref_id = $stored_member->get_dbiref_id();
398 is
($s_accession, $metadata_ids[$m], "STORE MEMBERS ($d) the group object, checking dbiref object (dbiref_id)")
399 or diag
"Looks like this failed";
403 my %member_metadbdata8 = $member_metadata{$s_dbiref_id}->get_metadata_by_rows();
404 my %expected_metadata8 = ( metadata_id
=> $last_metadata_id+1,
405 create_date
=> $creation_date,
406 create_person_id
=> $creation_user_id,
409 foreach my $metadata_type8 (keys %member_metadbdata8) {
410 my $message8 = "STORE FUNCTION METADBDATA FOR MEMBER ($d), get_metadbdata test, checking $metadata_type8";
412 is
($member_metadbdata8{$metadata_type8}, $expected_metadata8{$metadata_type8}, $message8) or diag
"Looks like this failed";
416 ### Test obsolete member... and imagine that i don't remember the dbiref_id for the accession=$last_metadata_id+2 (TEST 95, 96)
418 my $obsolete_dbiref_id = CXGN
::Metadata
::Dbiref
->new_by_accession( $schema,
424 my $non_obsolete_dbiref_id = CXGN
::Metadata
::Dbiref
->new_by_accession( $schema,
431 my $group9 = $group8_stored->obsolete_member( $metadbdata,
433 'obsolete a member test'
435 my $obsolete_member1 = $group9->is_obsolete_member( $obsolete_dbiref_id );
436 is
($obsolete_member1, 1, "OBSOLETE MEMBER FUNCTION, is_obsolete_member test, ckecking boolean, true")
437 or diag
"Looks like this failed";
438 my $obsolete_member2 = $group9->is_obsolete_member( $non_obsolete_dbiref_id );
439 is
($obsolete_member2, 0, "OBSOLETE MEMBER FUNCTION, is_obsolete_member test, ckecking boolean, false")
440 or diag
"Looks like this failed";
442 ### Test new_by_group_name (TEST 97)
444 my $group_name = $group7->get_group_name();
446 my $group10 = CXGN
::Metadata
::Groups
->new_by_group_name($schema, $group_name);
448 is
($group10->get_group_id(), $last_group_id+1, "NEW_BY_GROUP_NAME CONSTRUCTOR, checking group_id")
449 or diag
"Looks like this failed";
453 ## It will create an empty object and set the values instead to create the object with these values
454 ## because when it is created a new object with an accession that do not exists into the database return
457 my $new_dbiref = CXGN
::Metadata
::Dbiref
->new( $schema );
458 $new_dbiref->set_accession($last_metadata_id+1);
459 $new_dbiref->set_dbipath_id_by_dbipath_elements( ['metadata', 'md_metadata', 'metadata_id'] );
461 my $new_dbiref_id = $new_dbiref->store($metadbdata)
464 my $group11 = $group10->add_member($new_dbiref_id)
465 ->store($metadbdata);
469 my @m_members = $group11->get_members();
470 foreach my $m_member (@m_members) {
471 my $accession = $m_member->get_accession();
472 push @accessions, $accession;
475 ## The members should be the @metadata_ids + this $last_metadata_id+4 (TEST 98)
477 push @metadata_ids, $last_metadata_id+1;
478 my $expected_members = join(',', sort {$a <=> $b} @metadata_ids);
479 my $obtained_members = join(',', sort {$a <=> $b} @accessions);
480 is
($obtained_members, $expected_members, "ADD MEMBER METHOD, cheking a list of member (dbiref_ids)")
481 or diag
"Looks like this failed";
483 ## Group11 also should have a group_id = $last_group_id+1 (TEST 99)
485 is
($group11->get_group_id(), $last_group_id+1, "GENERAL STORE FUNCTION, checking group_id")
486 or diag
"Looks like this failed";
488 ## Check the obsolete tag to get members, it will take $last_metadata_id+2, (TEST 100)
489 my @obsolete_members = $group11->get_members('OBSOLETE');
490 my $obs_accession = $obsolete_members[0]->get_accession();
492 is
($obs_accession, $last_metadata_id+2, "GET MEMBERS FUNCTION with OBSOLETE TAG, checking accessions for members")
493 or diag
"Looks like this failed";
495 ## Test the non obsolete too (TEST 101)
496 my @non_obsolete_members = $group11->get_members('NON_OBSOLETE');
498 my @non_obsolete_acc = ();
499 foreach my $non_obsolete_member (@non_obsolete_members) {
500 my $non_obs_acc = $non_obsolete_member->get_accession();
501 push @non_obsolete_acc, $non_obs_acc;
504 my $obtained_non_obs = join(',', sort {$a <=> $b} @non_obsolete_acc);
505 my $expected_non_obs = join(',', sort {$a <=> $b} ($last_metadata_id+3, $last_metadata_id+4, $last_metadata_id+1 ) );
507 is
($obtained_non_obs, $expected_non_obs,
508 "GET MEMBERS FUNCTION with NON OBSOLETE TAG, checking accessions for members")
509 or diag
"Looks like this failed";
512 ## Testing new by members.
515 foreach my $m_metadata_id (@metadata_ids) {
516 my $m_dbiref_id = CXGN
::Metadata
::Dbiref
->new_by_accession( $schema,
522 push @members12, $m_dbiref_id;
525 ## Now it will add a new member ($last_metadata_id+2)
527 my $other_dbiref = CXGN
::Metadata
::Dbiref
->new( $schema );
528 $other_dbiref->set_accession($last_metadata_id+2);
529 $other_dbiref->set_dbipath_id_by_dbipath_elements( ['metadata', 'md_metadata', 'metadata_id'] );
531 my $other_dbiref_id = $other_dbiref->store($metadbdata)
534 push @members12, $other_dbiref_id;
537 warning_like
{ $group12 = CXGN
::Metadata
::Groups
->new_by_members($schema, \
@members12); } qr/DATABASE COHERENCE/,
538 'TESTING WARNING ERROR when does not exist group with the specified elements into the database';
540 my $group12_id = $group12->get_group_id();
542 ## This is to test if fail to find the group to store the new group and check the new group_id
543 unless (defined $group12_id) {
544 $group12_id = $group12->store($metadbdata)
548 is
($group12_id, $last_group_id+2, "NEW_BY_MEMBERS CONSTRUCTOR, checking the group_id") or diag
"Looks like this failed";
553 }; ## End of the eval function
556 print "\nEVAL ERROR:\n\n$@\n";
561 ## RESTORING THE ORIGINAL STATE IN THE DATABASE
562 ## To restore the original state in the database, rollback (it is in a transaction) and set the table_sequence values.
564 $schema->txn_rollback();
566 ## The transaction change the values in the sequence, so if we want set the original value, before the changes
567 ## we have two options:
568 ## 1) SELECT setval (<sequence_name>, $last_value_before_change, true); that said, ok your last true value was...
569 ## 2) SELECT setval (<sequence_name>, $last_value_before_change+1, false); It is false that your last value was ... so the
570 ## next time take the value before this.
572 ## The option 1 leave the seq information in a original state except if there aren't any value in the seq, that it is
573 ## more as the option 2
575 ## It do not reset the seq anymore for tests