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:
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
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
36 This script check XX variables to test the right operation of the
37 CXGN::Metadata::DBipath module:
39 + test 1 and 3: Test for module use
40 + test from 4 to 8: Test to set data into the object without any storage
41 + test 9: Creation ob dbipath object using dbipath (schema, table, column)
42 + test 10 and 11: Test store function
43 + test 12 and 21: Test get_metadbdata function and all the metadbdata
44 associated to a new row
45 + test 22: Test is_obsolete function
46 + test 23 and 24: Test store function for a modification in the row
47 + test 25 to 34: Test get_metadbdata function and all the metadbdata
48 associated to a modification
49 + test 35: Obsolete function test
50 + test 36 to 45: Test get_metadbdata function and all the metadbdata
51 associated to obsolete
52 + test 46: Obsolete function test with revert tag
53 + test 57 to 56: Test get_metadbdata function and all the metadbdata
54 associated to obsolete with revert tag
60 Aureliano Bombarely Gomez
70 use Test
::More tests
=>56; # use qw | no_plan | while developing the tests
72 use CXGN
::DB
::Connection
;
73 use CXGN
::DB
::DBICFactory
;
76 use_ok
('CXGN::Metadata::Schema'); ## TEST1
77 use_ok
('CXGN::Metadata::Dbipath'); ## TEST2
78 use_ok
('CXGN::Metadata::Metadbdata'); ## TEST3
81 ## Check the environment variables
82 my @env_variables = ('GEMTEST_METALOADER', 'GEMTEST_DBUSER', 'GEMTEST_DBPASS', 'RESET_DBSEQ');
83 foreach my $env (@env_variables) {
84 unless ($ENV{$env} =~ m/^\w+/) {
85 print STDERR
"ENVIRONMENT VARIABLE WARNING: Environment variable $env was not set for this test. Use perldoc for more info.\n";
90 #if we cannot load the CXGN::Metadata::Schema module, no point in continuing
91 CXGN
::Metadata
::Schema
->can('connect')
92 or BAIL_OUT
('could not load the CXGN::Metadata::Schema module');
94 ## Prespecified variable
96 my $metadata_creation_user = $ENV{GEMTEST_METALOADER
};
99 ## The triggers need to set the search path to tsearch2 in the version of psql 8.1
100 my $psqlv = `psql --version`;
103 my @schema_list = ('metadata', 'public');
104 if ($psqlv =~ /8\.1/) {
105 push @schema_list, 'tsearch2';
108 my $schema = CXGN
::DB
::DBICFactory
->open_schema( 'CXGN::Metadata::Schema',
109 search_path
=> \
@schema_list,
112 dbuser
=> $ENV{GEMTEST_DBUSER
},
113 dbpass
=> $ENV{GEMTEST_DBPASS
},
117 $schema->txn_begin();
123 ## Get the last values
124 my $all_last_ids_href = $schema->get_all_last_ids($schema);
125 my %last_ids = %{$all_last_ids_href};
126 my $last_metadata_id = $last_ids{'metadata.md_metadata_metadata_id_seq'};
127 my $last_dbipath_id = $last_ids{'metadata.md_dbipath_dbipath_id_seq'};
129 ## Create a empty metadata object to use in the database store functions
130 my $metadbdata = CXGN
::Metadata
::Metadbdata
->new($schema, $metadata_creation_user);
131 my $creation_date = $metadbdata->get_object_creation_date();
132 my $creation_user_id = $metadbdata->get_object_creation_user_by_id();
134 ## FIRST TEST BLOCK (TEST FROM 4 TO 7)
135 ## This is the first group of tests, to check if an empty object can store and after can return the data
136 ## Create a new empty object;
138 my $dbipath = CXGN
::Metadata
::Dbipath
->new($schema, undef);
140 ## Load of the eight different parameters for an empty object using a hash with keys=root name for tha function and
141 ## values=value to test
143 my %test_values_for_empty_object=( dbipath_id
=> $last_dbipath_id+1,
144 column_name
=> 'dbipath_id',
145 table_name
=> 'md_dbipath',
146 schema_name
=> 'metadata',
149 ## Load the data in the empty object
150 my @function_keys = sort keys %test_values_for_empty_object;
151 foreach my $rootfunction (@function_keys) {
152 my $setfunction = 'set_' . $rootfunction;
153 if ($rootfunction eq 'dbipath_id') {
154 $setfunction = 'force_set_' . $rootfunction;
156 $dbipath->$setfunction($test_values_for_empty_object{$rootfunction});
158 ## Get the data from the object and store in two hashes. The first %getdata with keys=root_function_name and
159 ## value=value_get_from_object and the second, %testname with keys=root_function_name and values=name for the test.
161 my (%getdata, %testnames);
162 foreach my $rootfunction (@function_keys) {
163 my $getfunction = 'get_'.$rootfunction;
164 my $data = $dbipath->$getfunction();
165 $getdata{$rootfunction} = $data;
166 my $testname = 'BASIC SET/GET FUNCTION for ' . $rootfunction.' test';
167 $testnames{$rootfunction} = $testname;
170 ## And now run the test for each function and value
172 foreach my $rootfunction (@function_keys) {
173 is
($getdata{$rootfunction}, $test_values_for_empty_object{$rootfunction}, $testnames{$rootfunction})
174 or diag
"Looks like this failed.";
177 ## Additional function set/get dbipath (TEST 8)
178 $dbipath->set_dbipath('metadata', 'md_metadata', 'metadata_id');
179 my @bt_dbipath_elements = $dbipath->get_dbipath();
180 my $bt_dbipath_elements = join('.', @bt_dbipath_elements);
181 is
($bt_dbipath_elements, 'metadata.md_metadata.metadata_id', 'COMPLEX SET/GET FUNCTION for dbipath test')
182 or diag
"Looks like this failed";
185 ### SECOND TEST BLOCK
186 ### Use of store functions.
190 ### It will create a new object based in dbipath (TEST 9)
191 my $dbipath2 = CXGN
::Metadata
::Dbipath
->new_by_path($schema, ['metadata', 'md_dbipath', 'dbipath_id']);
192 my @bt_dbipath_elements2 = $dbipath2->get_dbipath();
193 my $bt_dbipath_elements2 = join('.', @bt_dbipath_elements2);
194 is
($bt_dbipath_elements2, 'metadata.md_dbipath.dbipath_id', 'CONSTRUCTOR FUNCTION new_by_path test')
195 or diag
"Looks like this failed";
197 ### It will store it (TEST 10 to 11)
198 my $dbipath2_stored = $dbipath2->store($metadbdata);
199 my $dbipath2_id = $dbipath2_stored->get_dbipath_id();
200 is
($dbipath2_id, $last_dbipath_id+1, 'STORE FUNCTION with a new dbipath row, checking dbipath_id')
201 or diag
"Looks like this failed";
202 my @st_dbipath_elements = $dbipath2_stored->get_dbipath();
203 my $st_dbipath_elements = join('.', @st_dbipath_elements);
204 is
($st_dbipath_elements, 'metadata.md_dbipath.dbipath_id', 'STORE FUNCTION with a new dbipath row, checking dbipath elements')
205 or diag
"Looks like this failed";
207 ## Checking the metadbdata associated to this new creation (TEST 12 to 21)
208 my $st_metadbdata = $dbipath2_stored->get_metadbdata();
209 my %metadbdata = $st_metadbdata->get_metadata_by_rows();
210 my %expected_metadata = ( metadata_id
=> $last_metadata_id+1,
211 create_date
=> $creation_date,
212 create_person_id
=> $creation_user_id,
215 foreach my $st_metadata_type (keys %metadbdata) {
216 my $message = "STORE FUNCTION METADBDATA INTERACTION, get_metadbdata test, checking $st_metadata_type";
217 is
($metadbdata{$st_metadata_type}, $expected_metadata{$st_metadata_type}, $message) or diag
"Looks like this failed";
219 ## Checking the is_obsolete function (TEST 22)
220 my $obsolete = $dbipath2_stored->is_obsolete();
221 is
($obsolete, 0,"IS OBSOLETE FUNCTION TEST") or diag
"Looks like this failed";
223 ## Testing a modification in the row data (TEST 23 and 24)
224 $dbipath2_stored->set_column_name('column_name');
225 my $dbipath2_modified = $dbipath2_stored->store($metadbdata);
226 my $dbipath2_id2 = $dbipath2_modified->get_dbipath_id();
227 is
($dbipath2_id2, $last_dbipath_id+1, 'STORE FUNCTION with a modified dbipath row, checking dbipath_id')
228 or diag
"Looks like this failed";
229 my @st_dbipath_elements2 = $dbipath2_modified->get_dbipath();
230 my $st_dbipath_elements2 = join('.', @st_dbipath_elements2);
231 is
($st_dbipath_elements2, 'metadata.md_dbipath.column_name', 'STORE FUNCTION with a modified dbipath row, checking dbipath elements')
232 or diag
"Looks like this failed";
234 ## Checking the metadbdata associated to this modification (TEST 25 to 34)
235 my $st_metadbdata2 = $dbipath2_modified->get_metadbdata();
236 my %metadbdata2 = $st_metadbdata2->get_metadata_by_rows();
237 my %expected_metadata2 = ( metadata_id
=> $last_metadata_id+2,
238 create_date
=> $creation_date,
239 create_person_id
=> $creation_user_id,
240 modified_date
=> $creation_date,
241 modified_person_id
=> $creation_user_id,
242 modification_note
=> 'set value in column_name column',
243 previous_metadata_id
=> $last_metadata_id+1,
246 foreach my $st_metadata_type2 (keys %metadbdata2) {
247 my $message2 = "STORE FUNCTION METADBDATA INTERACTION FOR MODIFICATIONS, get_metadbdata test, checking $st_metadata_type2";
248 is
($metadbdata2{$st_metadata_type2}, $expected_metadata2{$st_metadata_type2}, $message2) or diag
"Looks like this failed";
251 ## Test the obsolete function (TEST 35)
252 my $dbipath2_obsolete = $dbipath2_modified->obsolete($metadbdata, 'change to obsolete test');
253 my $obsolete2 = $dbipath2_stored->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 36 to 45)
257 my $st_metadbdata3 = $dbipath2_obsolete->get_metadbdata();
258 my %metadbdata3 = $st_metadbdata3->get_metadata_by_rows();
259 my %expected_metadata3 = ( 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,
267 obsolete_note
=> 'change to obsolete test'
269 foreach my $st_metadata_type3 (keys %metadbdata3) {
270 my $message3 = "STORE FUNCTION METADBDATA INTERACTION FOR OBSOLETE, get_metadbdata test, checking $st_metadata_type3";
271 is
($metadbdata3{$st_metadata_type3}, $expected_metadata3{$st_metadata_type3}, $message3) or diag
"Looks like this failed";
274 ## Test the REVERT tag for the obsolete function (TEST 46)
275 my $dbipath2_revert = $dbipath2_obsolete->obsolete($metadbdata, 'revert obsolete test', 'REVERT');
276 my $obsolete3 = $dbipath2_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 47 to 56)
280 my $st_metadbdata4 = $dbipath2_revert->get_metadbdata();
281 my %metadbdata4 = $st_metadbdata4->get_metadata_by_rows();
282 my %expected_metadata4 = ( 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,
290 obsolete_note
=> 'revert obsolete test'
292 foreach my $st_metadata_type4 (keys %metadbdata4) {
293 my $message4 = "STORE FUNCTION METADBDATA INTERACTION FOR REVERT OBSOLETE, get_metadbdata test, checking $st_metadata_type4";
294 is
($metadbdata4{$st_metadata_type4}, $expected_metadata4{$st_metadata_type4}, $message4) or diag
"Looks like this failed";
298 }; ## End of the eval function
301 print "\nEVAL ERROR:\n\n$@\n";
305 ## RESTORING THE ORIGINAL STATE IN THE DATABASE
306 ## To restore the original state in the database, rollback (it is in a transaction) and set the table_sequence values.
308 $schema->txn_rollback();
310 ## The transaction change the values in the sequence, so if we want set the original value, before the changes
311 ## we have two options:
312 ## 1) SELECT setval (<sequence_name>, $last_value_before_change, true); that said, ok your last true value was...
313 ## 2) SELECT setval (<sequence_name>, $last_value_before_change+1, false); It is false that your last value was ... so the
314 ## next time take the value before this.
316 ## The option 1 leave the seq information in a original state except if there aren't any value in the seq, that it is
317 ## more as the option 2
319 if ($ENV{RESET_DBSEQ
}) {
320 $schema->set_sqlseq_values_to_original_state(\
%last_ids);