fixed the pod for add_child. This function does not take any arguments. it generates...
[cxgn-corelibs.git] / t / CXGN / Metadata / dbiref.t
blob26f0a831e15877cc4b6cb0c061f1f018df2aa648
1 #!/usr/bin/perl
3 =head1 NAME
5 dbiref.t
6 A piece of code to test the CXGN::Metadata::Dbiref module
8 =cut
10 =head1 SYNOPSIS
12 perl Dbiref.t
14 Note: To run the complete test the database connection should be done as
15 postgres user
16 (web_usr have not privileges to insert new data into the sed tables)
18 prove Dbiref.t
20 this test need some environment variables:
21 export GEMTEST_METALOADER= 'metaloader user'
22 export GEMTEST_DBUSER= 'database user with insert permissions'
23 export GEMTEST_DBPASS= 'database password'
25 also is recommendable set the reset dbseq after run the script
26 export RESET_DBSEQ=1
28 if it is not set, after one run all the test that depends of a primary id
29 (as metadata_id) will fail because it is calculated based in the last
30 primary id and not in the current sequence for this primary id
33 =head1 DESCRIPTION
35 This script check XX variables to test the right operation of the
36 CXGN::Metadata::DBipath module:
38 + test 1 and 3: Test for module use
39 + test from 4 to 7: Test to set data into the object without any storage
40 + test 8 and 9: Test store function for a new row
41 + test 10 and 11: Test store related with metadata for dbipath and dbiref
42 + test 12: Test get_dbipath_obj function
43 + test 13: Test set_dbipath_id_by_dbipath_elements
44 + test 14 to 23: Test the store function for a modification case.
45 Checking the metadbdata.
46 + test 24: Testing constructor new with a id
47 + test 25: Testing is_obsolete function
48 + test 26: Testing obsolete function
49 + test from 27 to 36: Testing the metadbdata associated to obsolete change
50 + test 37: Testing the revert tag for the obsolete function
51 + test 38 to 47: Testing the metadbdata associated with the revert change
52 + test 48: Testing new_by_accession constructor
54 =cut
56 =head1 AUTHORS
58 Aureliano Bombarely Gomez
59 (ab782@cornell.edu)
61 =cut
64 use strict;
65 use warnings;
67 use Data::Dumper;
68 use Test::More tests=>48; # use qw | no_plan | while developing the tests
70 use CXGN::DB::Connection;
71 use CXGN::DB::DBICFactory;
73 BEGIN {
74 use_ok('CXGN::Metadata::Schema'); ## TEST1
75 use_ok('CXGN::Metadata::Dbiref'); ## TEST2
76 use_ok('CXGN::Metadata::Metadbdata'); ## TEST3
79 ## Check the environment variables
80 my @env_variables = ('GEMTEST_METALOADER', 'GEMTEST_DBUSER', 'GEMTEST_DBPASS', 'RESET_DBSEQ');
81 foreach my $env (@env_variables) {
82 unless ($ENV{$env} =~ m/^\w+/) {
83 print STDERR "ENVIRONMENT VARIABLE WARNING: Environment variable $env was not set for this test. Use perldoc for more info.\n";
87 #if we cannot load the CXGN::Metadata::Schema module, no point in continuing
88 CXGN::Metadata::Schema->can('connect')
89 or BAIL_OUT('could not load the CXGN::Metadata::Schema module');
91 ## Prespecified variable
93 my $metadata_creation_user = $ENV{GEMTEST_METALOADER};
95 ## The triggers need to set the search path to tsearch2 in the version of psql 8.1
96 my $psqlv = `psql --version`;
97 chomp($psqlv);
99 my @schema_list = ('metadata', 'public');
100 if ($psqlv =~ /8\.1/) {
101 push @schema_list, 'tsearch2';
104 my $schema = CXGN::DB::DBICFactory->open_schema( 'CXGN::Metadata::Schema',
105 search_path => \@schema_list,
106 dbconn_args =>
108 dbuser => $ENV{GEMTEST_DBUSER},
109 dbpass => $ENV{GEMTEST_DBPASS},
113 $schema->txn_begin();
116 ## Get the last values
117 my $all_last_ids_href = $schema->get_all_last_ids($schema);
118 my %last_ids = %{$all_last_ids_href};
119 my $last_metadata_id = $last_ids{'metadata.md_metadata_metadata_id_seq'};
120 my $last_dbipath_id = $last_ids{'metadata.md_dbipath_dbipath_id_seq'};
121 my $last_dbiref_id = $last_ids{'metadata.md_dbiref_dbiref_id_seq'};
123 ## Create a empty metadata object to use in the database store functions
124 my $metadbdata = CXGN::Metadata::Metadbdata->new($schema, $metadata_creation_user);
125 my $creation_date = $metadbdata->get_object_creation_date();
126 my $creation_user_id = $metadbdata->get_object_creation_user_by_id();
129 ## FIRST TEST BLOCK (TEST FROM 4 TO 7)
130 ## This is the first group of tests, to check if an empty object can store and after can return the data
131 ## Create a new empty object (It is not necessary add any dbipath_id becuase it don't interact with the db;
133 my $dbiref = CXGN::Metadata::Dbiref->new($schema, undef);
135 ## Load of the eight different parameters for an empty object using a hash with keys=root name for tha function and
136 ## values=value to test
138 my %test_values_for_empty_object=( dbiref_id => $last_dbiref_id+1,
139 iref_accession => $last_dbipath_id+1,
140 accession => $last_dbipath_id+1, ## A synonym for iref_accession
141 dbipath_id => $last_dbipath_id+1,
144 ## Load the data in the empty object
145 my @function_keys = sort keys %test_values_for_empty_object;
146 foreach my $rootfunction (@function_keys) {
147 my $setfunction = 'set_' . $rootfunction;
148 if ($rootfunction eq 'dbiref_id') {
149 $setfunction = 'force_set_' . $rootfunction;
151 $dbiref->$setfunction($test_values_for_empty_object{$rootfunction});
153 ## Get the data from the object and store in two hashes. The first %getdata with keys=root_function_name and
154 ## value=value_get_from_object and the second, %testname with keys=root_function_name and values=name for the test.
156 my (%getdata, %testnames);
157 foreach my $rootfunction (@function_keys) {
158 my $getfunction = 'get_'.$rootfunction;
159 my $data = $dbiref->$getfunction();
160 $getdata{$rootfunction} = $data;
161 my $testname = 'BASIC SET/GET FUNCTION for ' . $rootfunction.' test';
162 $testnames{$rootfunction} = $testname;
165 ## And now run the test for each function and value
167 foreach my $rootfunction (@function_keys) {
168 is($getdata{$rootfunction}, $test_values_for_empty_object{$rootfunction}, $testnames{$rootfunction})
169 or diag "Looks like this failed.";
172 ## SECOND BLOCK, Interactions with the DB for metadata object
174 eval {
176 ## First, the store functions check if exists a dbipath_id into the database, so it will create a dbipath id
177 ## for the path 'metadata.md_dbipath.dbipath_id' (with itself to be sure that it don't exists into the database)
179 ### It will create a new object based in dbipath
180 my $dbipath = CXGN::Metadata::Dbipath->new_by_path($schema, ['metadata', 'md_dbipath', 'dbipath_id']);
181 my $dbipath_stored = $dbipath->store($metadbdata);
182 my $dbipath_id = $dbipath_stored->get_dbipath_id();
184 ## New, it will create the dbiref object (TEST 8 and 9)
185 my $dbiref2 = CXGN::Metadata::Dbiref->new($schema, undef);
186 $dbiref2->set_accession($dbipath_id);
187 $dbiref2->set_dbipath_id($dbipath_id);
188 my $dbiref2_stored = $dbiref2->store($metadbdata);
189 is($dbiref2_stored->get_accession(), $last_dbipath_id+1, 'STORE FUNCTION for a new row, checking iref_accession')
190 or diag "Looks like this failed";
191 is($dbiref2_stored->get_dbipath_id(), $last_dbipath_id+1, 'STORE FUNCTION for a new row, checking dbipath_id')
192 or diag "Looks like this failed";
194 my $test5 = $dbiref2_stored->get_dbipath_id();
195 print "test path: $test5\n";
197 ## Check the metadata for dbipath and dbiref (TEST 10 and 11)
198 ## The metadata should be the same because both were created at the same time (with the same metadbdata)
199 my $dbipath_metadata_id = $dbipath_stored->get_metadbdata()->get_metadata_id();
200 my $dbiref_metadata_id = $dbiref2_stored->get_metadbdata()->get_metadata_id();
201 is($dbipath_metadata_id, $last_metadata_id+1, 'STORE FUNCTION METADATA RELATED, checking metadata_id for a new dbipath')
202 or diag "Looks like this failed";
203 is($dbiref_metadata_id, $last_metadata_id+1, 'STORE FUNCTION METADATA RELATED, checking metadata_id for a new dbiref')
204 or diag "Looks like this failed";
206 ## Testing get_dbipath_obj function (TEST 12)
207 my $dbipath2 = $dbiref2_stored->get_dbipath_obj();
208 my @dbipath_elements = $dbipath2->get_dbipath();
209 my $dbipath_elements = join('.', @dbipath_elements);
210 is($dbipath_elements, 'metadata.md_dbipath.dbipath_id', 'GET DBIPATH_OBJ FUNCTION, checking the dbipath elements')
211 or diag "Looks like this failed";
213 ## Testing set_dbipath_id_by_dbipath_elements (TEST 13)
214 ## Before it need a different dbipath row to set
215 my $dbipath3 = CXGN::Metadata::Dbipath->new_by_path($schema, ['metadata', 'md_dbiref', 'dbiref_id']);
216 my $dbipath3_stored = $dbipath3->store($metadbdata);
217 my $dbipath3_id = $dbipath3_stored->get_dbipath_id();
219 $dbiref2_stored->set_dbipath_id_by_dbipath_elements(['metadata', 'md_dbiref', 'dbiref_id']);
220 is($dbiref2_stored->get_dbipath_id(), $dbipath3_id, 'SET DBIPATH ID BY DBIPATH ELEMENTS, checking the new dbipath_id')
221 or diag "Looks like this failed";
223 ## Now test store for a modification checking the metadbdata (TEST 14 to 23)
224 my $dbiref3_stored = $dbiref2_stored->store($metadbdata);
225 my $dbiref3_metadbdata = $dbiref3_stored->get_metadbdata();
226 my %metadbdata3 = $dbiref3_metadbdata->get_metadata_by_rows();
227 my %expected_metadata3 = ( metadata_id => $last_metadata_id+2,
228 create_date => $creation_date,
229 create_person_id => $creation_user_id,
230 modified_date => $creation_date,
231 modified_person_id => $creation_user_id,
232 modification_note => 'set value in dbipath_id column',
233 previous_metadata_id => $last_metadata_id+1,
234 obsolete => 0
236 foreach my $st_metadata_type3 (keys %metadbdata3) {
237 my $message3 = "STORE FUNCTION METADBDATA INTERACTION FOR MODIFICATIONS, get_metadbdata test, checking $st_metadata_type3";
238 is($metadbdata3{$st_metadata_type3}, $expected_metadata3{$st_metadata_type3}, $message3) or diag "Looks like this failed";
241 ## Get a new dbiref using id (TEST 24)
242 my $dbiref4 = CXGN::Metadata::Dbiref->new($schema, $dbiref2_stored->get_dbiref_id());
243 is($dbiref4->get_accession(), $dbiref2_stored->get_accession(), 'CONSTRUCTOR NEW using a dbxref_id, checking iref_accession')
244 or diag "Looks like this failed";
246 ## Testing the obsolete features
247 ## Checking the is_obsolete function (TEST 25)
248 my $obsolete = $dbiref4->is_obsolete();
249 is($obsolete, 0,"IS OBSOLETE FUNCTION TEST") or diag "Looks like this failed";
251 ## Test the obsolete function (TEST 26)
252 my $dbiref5_obsolete = $dbiref4->obsolete($metadbdata, 'change to obsolete test');
253 my $obsolete2 = $dbiref5_obsolete->is_obsolete();
254 is($obsolete2, 1,"OBSOLETE FUNCTION TEST") or diag "Looks like this failed";
256 ## Checking the metadata associated to this obsolete change (TEST 27 to 36)
257 my $metadbdata5 = $dbiref5_obsolete->get_metadbdata();
258 my %metadbdata5 = $metadbdata5->get_metadata_by_rows();
259 my %expected_metadata5 = ( metadata_id => $last_metadata_id+3,
260 create_date => $creation_date,
261 create_person_id => $creation_user_id,
262 modified_date => $creation_date,
263 modified_person_id => $creation_user_id,
264 modification_note => 'change to obsolete',
265 previous_metadata_id => $last_metadata_id+2,
266 obsolete => 1,
267 obsolete_note => 'change to obsolete test'
269 foreach my $metadata_type5 (keys %metadbdata5) {
270 my $message5 = "STORE FUNCTION METADBDATA INTERACTION FOR OBSOLETE, get_metadbdata test, checking $metadata_type5";
271 is($metadbdata5{$metadata_type5}, $expected_metadata5{$metadata_type5}, $message5) or diag "Looks like this failed";
274 ## Test the REVERT tag for the obsolete function (TEST 37)
275 my $dbiref5_revert = $dbiref5_obsolete->obsolete($metadbdata, 'revert obsolete test', 'REVERT');
276 my $obsolete3 = $dbiref5_revert->is_obsolete();
277 is($obsolete3, 0,"OBSOLETE FUNCTION TEST") or diag "Looks like this failed";
279 ## Checking the metadata associated to this obsolete change (TEST 38 to 47)
280 my $metadbdata6 = $dbiref5_revert->get_metadbdata();
281 my %metadbdata6 = $metadbdata6->get_metadata_by_rows();
282 my %expected_metadata6 = ( metadata_id => $last_metadata_id+4,
283 create_date => $creation_date,
284 create_person_id => $creation_user_id,
285 modified_date => $creation_date,
286 modified_person_id => $creation_user_id,
287 modification_note => 'revert obsolete',
288 previous_metadata_id => $last_metadata_id+3,
289 obsolete => 0,
290 obsolete_note => 'revert obsolete test'
292 foreach my $metadata_type6 (keys %metadbdata6) {
293 my $message6 = "STORE FUNCTION METADBDATA INTERACTION FOR REVERT OBSOLETE, get_metadbdata test, checking $metadata_type6";
294 is($metadbdata6{$metadata_type6}, $expected_metadata6{$metadata_type6}, $message6) or diag "Looks like this failed";
297 ## Testing new_by_accession constructor (TEST 48).
298 ## It will use the first dbiref object stored with accession=$dbipath_id and path = ['metadata', 'md_dbipath', dbipath_id]
299 ## It should return an object with dbiref_id= $last_dbiref_id+1
301 my $dbiref6 = CXGN::Metadata::Dbiref->new_by_accession( $schema,
302 $dbiref2_stored->get_accession(),
303 ['metadata', 'md_dbiref', 'dbiref_id'] ); ## It was modificated
305 is($dbiref6->get_dbiref_id(), $last_dbiref_id+1, "NEW_BY_ACCESSION constructor, checking dbiref_id")
306 or diag "Looks like this failed";
309 }; ## End of the eval function
312 if ($@) {
313 print "\nEVAL ERROR:\n\n$@\n";
317 ## RESTORING THE ORIGINAL STATE IN THE DATABASE
318 ## To restore the original state in the database, rollback (it is in a transaction) and set the table_sequence values.
320 $schema->txn_rollback();
322 ## The transaction change the values in the sequence, so if we want set the original value, before the changes
323 ## we have two options:
324 ## 1) SELECT setval (<sequence_name>, $last_value_before_change, true); that said, ok your last true value was...
325 ## 2) SELECT setval (<sequence_name>, $last_value_before_change+1, false); It is false that your last value was ... so the
326 ## next time take the value before this.
328 ## The option 1 leave the seq information in a original state except if there aren't any value in the seq, that it is
329 ## more as the option 2
331 if ($ENV{RESET_DBSEQ}) {
332 $schema->set_sqlseq_values_to_original_state(\%last_ids);