6 A piece of code to test the CXGN::Metadata::Dbipath 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'
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 8: Test to set data into the object without any storage
40 + test 9: Creation ob dbipath object using dbipath (schema, table, column)
41 + test 10 and 11: Test store function
42 + test 12 and 21: Test get_metadbdata function and all the metadbdata
43 associated to a new row
44 + test 22: Test is_obsolete function
45 + test 23 and 24: Test store function for a modification in the row
46 + test 25 to 34: Test get_metadbdata function and all the metadbdata
47 associated to a modification
48 + test 35: Obsolete function test
49 + test 36 to 45: Test get_metadbdata function and all the metadbdata
50 associated to obsolete
51 + test 46: Obsolete function test with revert tag
52 + test 57 to 56: Test get_metadbdata function and all the metadbdata
53 associated to obsolete with revert tag
59 Aureliano Bombarely Gomez
69 use Test
::More
; # use qw | no_plan | while developing the tests
71 use CXGN
::DB
::Connection
;
74 ## The tests still need search_path
76 my @schema_list = ('metadata', 'public');
77 my $schema_list = join(',', @schema_list);
78 my $set_path = "SET search_path TO $schema_list";
80 ## First check env. variables and connection
84 ## Env. variables have been changed to use biosource specific ones
86 my @env_variables = qw
/METADATA_TEST_METALOADER METADATA_TEST_DBDSN METADATA_TEST_DBUSER METADATA_TEST_DBPASS/;
88 for my $env (@env_variables) {
89 unless (defined $ENV{$env}) {
90 plan skip_all
=> "Environment variable $env not set, aborting";
95 CXGN
::DB
::Connection
->new(
96 $ENV{METADATA_TEST_DBDSN
},
97 $ENV{METADATA_TEST_DBUSER
},
98 $ENV{METADATA_TEST_DBPASS
},
99 {on_connect_do
=> $set_path}
103 if ($@
=~ m/DBI connect/) {
105 plan skip_all
=> "Could not connect to database";
112 use_ok
('CXGN::Metadata::Schema'); ## TEST1
113 use_ok
('CXGN::Metadata::Dbipath'); ## TEST2
114 use_ok
('CXGN::Metadata::Metadbdata'); ## TEST3
118 #if we cannot load the CXGN::Metadata::Schema module, no point in continuing
119 CXGN
::Metadata
::Schema
->can('connect')
120 or BAIL_OUT
('could not load the CXGN::Metadata::Schema module');
122 ## Prespecified variable
124 my $metadata_creation_user = $ENV{METADATA_TEST_METALOADER
};
126 ## The biosource schema contain all the metadata classes so don't need to create another Metadata schema
127 ## CXGN::DB::DBICFactory is obsolete, it has been replaced by CXGN::Metadata::Schema
129 my $schema = CXGN
::Metadata
::Schema
->connect( $ENV{METADATA_TEST_DBDSN
},
130 $ENV{METADATA_TEST_DBUSER
},
131 $ENV{METADATA_TEST_DBPASS
},
132 {on_connect_do
=> $set_path});
135 $schema->txn_begin();
138 ## Get the last values
139 my %nextvals = $schema->get_nextval();
140 my $last_metadata_id = $nextvals{'md_metadata'};
141 my $last_dbipath_id = $nextvals{'md_dbipath'};
143 ## Create a empty metadata object to use in the database store functions
144 my $metadbdata = CXGN
::Metadata
::Metadbdata
->new($schema, $metadata_creation_user);
145 my $creation_date = $metadbdata->get_object_creation_date();
146 my $creation_user_id = $metadbdata->get_object_creation_user_by_id();
148 ## FIRST TEST BLOCK (TEST FROM 4 TO 7)
149 ## This is the first group of tests, to check if an empty object can store and after can return the data
150 ## Create a new empty object;
152 my $dbipath = CXGN
::Metadata
::Dbipath
->new($schema, undef);
154 ## Load of the eight different parameters for an empty object using a hash with keys=root name for tha function and
155 ## values=value to test
157 my %test_values_for_empty_object=( dbipath_id
=> $last_dbipath_id+1,
158 column_name
=> 'dbipath_id',
159 table_name
=> 'md_dbipath',
160 schema_name
=> 'metadata',
163 ## Load the data in the empty object
164 my @function_keys = sort keys %test_values_for_empty_object;
165 foreach my $rootfunction (@function_keys) {
166 my $setfunction = 'set_' . $rootfunction;
167 if ($rootfunction eq 'dbipath_id') {
168 $setfunction = 'force_set_' . $rootfunction;
170 $dbipath->$setfunction($test_values_for_empty_object{$rootfunction});
172 ## Get the data from the object and store in two hashes. The first %getdata with keys=root_function_name and
173 ## value=value_get_from_object and the second, %testname with keys=root_function_name and values=name for the test.
175 my (%getdata, %testnames);
176 foreach my $rootfunction (@function_keys) {
177 my $getfunction = 'get_'.$rootfunction;
178 my $data = $dbipath->$getfunction();
179 $getdata{$rootfunction} = $data;
180 my $testname = 'BASIC SET/GET FUNCTION for ' . $rootfunction.' test';
181 $testnames{$rootfunction} = $testname;
184 ## And now run the test for each function and value
186 foreach my $rootfunction (@function_keys) {
187 is
($getdata{$rootfunction}, $test_values_for_empty_object{$rootfunction}, $testnames{$rootfunction})
188 or diag
"Looks like this failed.";
191 ## Additional function set/get dbipath (TEST 8)
192 $dbipath->set_dbipath('metadata', 'md_metadata', 'metadata_id');
193 my @bt_dbipath_elements = $dbipath->get_dbipath();
194 my $bt_dbipath_elements = join('.', @bt_dbipath_elements);
195 is
($bt_dbipath_elements, 'metadata.md_metadata.metadata_id', 'COMPLEX SET/GET FUNCTION for dbipath test')
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 $dbipath2 = CXGN
::Metadata
::Dbipath
->new_by_path($schema, ['metadata', 'md_dbipath', 'dbipath_id']);
206 my @bt_dbipath_elements2 = $dbipath2->get_dbipath();
207 my $bt_dbipath_elements2 = join('.', @bt_dbipath_elements2);
208 is
($bt_dbipath_elements2, 'metadata.md_dbipath.dbipath_id', 'CONSTRUCTOR FUNCTION new_by_path test')
209 or diag
"Looks like this failed";
211 ### It will store it (TEST 10 to 11)
212 my $dbipath2_stored = $dbipath2->store($metadbdata);
213 my $dbipath2_id = $dbipath2_stored->get_dbipath_id();
214 is
($dbipath2_id, $last_dbipath_id+1, 'STORE FUNCTION with a new dbipath row, checking dbipath_id')
215 or diag
"Looks like this failed";
216 my @st_dbipath_elements = $dbipath2_stored->get_dbipath();
217 my $st_dbipath_elements = join('.', @st_dbipath_elements);
218 is
($st_dbipath_elements, 'metadata.md_dbipath.dbipath_id', 'STORE FUNCTION with a new dbipath row, checking dbipath elements')
219 or diag
"Looks like this failed";
221 ## Checking the metadbdata associated to this new creation (TEST 12 to 21)
222 my $st_metadbdata = $dbipath2_stored->get_metadbdata();
223 my %metadbdata = $st_metadbdata->get_metadata_by_rows();
224 my %expected_metadata = ( metadata_id
=> $last_metadata_id+1,
225 create_date
=> $creation_date,
226 create_person_id
=> $creation_user_id,
229 foreach my $st_metadata_type (keys %metadbdata) {
230 my $message = "STORE FUNCTION METADBDATA INTERACTION, get_metadbdata test, checking $st_metadata_type";
231 is
($metadbdata{$st_metadata_type}, $expected_metadata{$st_metadata_type}, $message) or diag
"Looks like this failed";
233 ## Checking the is_obsolete function (TEST 22)
234 my $obsolete = $dbipath2_stored->is_obsolete();
235 is
($obsolete, 0,"IS OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
237 ## Testing a modification in the row data (TEST 23 and 24)
238 $dbipath2_stored->set_column_name('column_name');
239 my $dbipath2_modified = $dbipath2_stored->store($metadbdata);
240 my $dbipath2_id2 = $dbipath2_modified->get_dbipath_id();
241 is
($dbipath2_id2, $last_dbipath_id+1, 'STORE FUNCTION with a modified dbipath row, checking dbipath_id')
242 or diag
"Looks like this failed";
243 my @st_dbipath_elements2 = $dbipath2_modified->get_dbipath();
244 my $st_dbipath_elements2 = join('.', @st_dbipath_elements2);
245 is
($st_dbipath_elements2, 'metadata.md_dbipath.column_name', 'STORE FUNCTION with a modified dbipath row, checking dbipath elements')
246 or diag
"Looks like this failed";
248 ## Checking the metadbdata associated to this modification (TEST 25 to 34)
249 my $st_metadbdata2 = $dbipath2_modified->get_metadbdata();
250 my %metadbdata2 = $st_metadbdata2->get_metadata_by_rows();
251 my %expected_metadata2 = ( metadata_id
=> $last_metadata_id+2,
252 create_date
=> $creation_date,
253 create_person_id
=> $creation_user_id,
254 modified_date
=> $creation_date,
255 modified_person_id
=> $creation_user_id,
256 modification_note
=> 'set value in column_name column',
257 previous_metadata_id
=> $last_metadata_id+1,
260 foreach my $st_metadata_type2 (keys %metadbdata2) {
261 my $message2 = "STORE FUNCTION METADBDATA INTERACTION FOR MODIFICATIONS, get_metadbdata test, checking $st_metadata_type2";
262 is
($metadbdata2{$st_metadata_type2}, $expected_metadata2{$st_metadata_type2}, $message2) or diag
"Looks like this failed";
265 ## Test the obsolete function (TEST 35)
266 my $dbipath2_obsolete = $dbipath2_modified->obsolete($metadbdata, 'change to obsolete test');
267 my $obsolete2 = $dbipath2_stored->is_obsolete();
268 is
($obsolete2, 1,"OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
270 ## Checking the metadata associated to this obsolete change (TEST 36 to 45)
271 my $st_metadbdata3 = $dbipath2_obsolete->get_metadbdata();
272 my %metadbdata3 = $st_metadbdata3->get_metadata_by_rows();
273 my %expected_metadata3 = ( metadata_id
=> $last_metadata_id+3,
274 create_date
=> $creation_date,
275 create_person_id
=> $creation_user_id,
276 modified_date
=> $creation_date,
277 modified_person_id
=> $creation_user_id,
278 modification_note
=> 'change to obsolete',
279 previous_metadata_id
=> $last_metadata_id+2,
281 obsolete_note
=> 'change to obsolete test'
283 foreach my $st_metadata_type3 (keys %metadbdata3) {
284 my $message3 = "STORE FUNCTION METADBDATA INTERACTION FOR OBSOLETE, get_metadbdata test, checking $st_metadata_type3";
285 is
($metadbdata3{$st_metadata_type3}, $expected_metadata3{$st_metadata_type3}, $message3) or diag
"Looks like this failed";
288 ## Test the REVERT tag for the obsolete function (TEST 46)
289 my $dbipath2_revert = $dbipath2_obsolete->obsolete($metadbdata, 'revert obsolete test', 'REVERT');
290 my $obsolete3 = $dbipath2_revert->is_obsolete();
291 is
($obsolete3, 0,"OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
293 ## Checking the metadata associated to this obsolete change (TEST 47 to 56)
294 my $st_metadbdata4 = $dbipath2_revert->get_metadbdata();
295 my %metadbdata4 = $st_metadbdata4->get_metadata_by_rows();
296 my %expected_metadata4 = ( metadata_id
=> $last_metadata_id+4,
297 create_date
=> $creation_date,
298 create_person_id
=> $creation_user_id,
299 modified_date
=> $creation_date,
300 modified_person_id
=> $creation_user_id,
301 modification_note
=> 'revert obsolete',
302 previous_metadata_id
=> $last_metadata_id+3,
304 obsolete_note
=> 'revert obsolete test'
306 foreach my $st_metadata_type4 (keys %metadbdata4) {
307 my $message4 = "STORE FUNCTION METADBDATA INTERACTION FOR REVERT OBSOLETE, get_metadbdata test, checking $st_metadata_type4";
308 is
($metadbdata4{$st_metadata_type4}, $expected_metadata4{$st_metadata_type4}, $message4) or diag
"Looks like this failed";
312 }; ## End of the eval function
315 print "\nEVAL ERROR:\n\n$@\n";
319 ## RESTORING THE ORIGINAL STATE IN THE DATABASE
320 ## To restore the original state in the database, rollback (it is in a transaction) and set the table_sequence values.
322 $schema->txn_rollback();
324 ## The transaction change the values in the sequence, so if we want set the original value, before the changes
325 ## we have two options:
326 ## 1) SELECT setval (<sequence_name>, $last_value_before_change, true); that said, ok your last true value was...
327 ## 2) SELECT setval (<sequence_name>, $last_value_before_change+1, false); It is false that your last value was ... so the
328 ## next time take the value before this.
330 ## The option 1 leave the seq information in a original state except if there aren't any value in the seq, that it is
331 ## more as the option 2
333 ## The test does not reset the sequences anymore.