Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / t / CXGN / Metadata / dbversion.t
blob2bcd2497cfce3b856009c090d1f72843190a4152
1 #!/usr/bin/perl
3 =head1 NAME
5 dbversion.t
6 A piece of code to test the CXGN::Metadata::Dbversion module
8 =cut
10 =head1 SYNOPSIS
12 perl Dbversion.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 Dbversion.t
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'
26 example:
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'
32 =head1 DESCRIPTION
34 This script check 33 variables to test the right operation of the
35 CXGN::Metadata::DBipath module:
37 + Test from 1 to 3 - use modules.
38 + Test from 4 to 6 - BASIC SET/GET FUNCTIONS.
39 + Test from 7 to 14 - STORE FUNCTION.
40 + Test from 15 to 18 - OBSOLETE FUNCTION.
41 + Test 19 and 20 - GET PATCH NUMBER METHOD.
42 + Test 21 and 22 - EXISTS DBPATCH METHOD.
43 + Test from 23 to 29 - CHECKING PREVIOUS DBPATCHES METHOD.
45 =cut
47 =head1 AUTHORS
49 Aureliano Bombarely Gomez
50 (ab782@cornell.edu)
52 =cut
55 use strict;
56 use warnings;
58 use Data::Dumper;
59 use Test::More; #qw | no_plan |; # while developing the tests
60 use Test::Exception;
62 use CXGN::DB::Connection;
64 ## The tests still need search_path
66 my @schema_list = ('metadata', 'public');
67 my $schema_list = join(',', @schema_list);
68 my $set_path = "SET search_path TO $schema_list";
70 ## First check env. variables and connection
72 BEGIN {
74 ## Env. variables have been changed to use biosource specific ones
76 my @env_variables = qw/METADATA_TEST_METALOADER METADATA_TEST_DBDSN METADATA_TEST_DBUSER METADATA_TEST_DBPASS/;
78 ## RESET_DBSEQ is an optional env. variable, it doesn't need to check it
80 for my $env (@env_variables) {
81 unless (defined $ENV{$env}) {
82 plan skip_all => "Environment variable $env not set, aborting";
86 eval {
87 CXGN::DB::Connection->new(
88 $ENV{METADATA_TEST_DBDSN},
89 $ENV{METADATA_TEST_DBUSER},
90 $ENV{METADATA_TEST_DBPASS},
91 {on_connect_do => $set_path}
92 );
95 if ($@ =~ m/DBI connect/) {
97 plan skip_all => "Could not connect to database";
100 plan tests => 23;
103 BEGIN {
104 use_ok('CXGN::Metadata::Schema'); ## TEST1
105 use_ok('CXGN::Metadata::Dbversion'); ## TEST2
106 use_ok('CXGN::Metadata::Metadbdata'); ## TEST3
110 #if we cannot load the CXGN::Metadata::Schema module, no point in continuing
111 CXGN::Metadata::Schema->can('connect')
112 or BAIL_OUT('could not load the CXGN::Metadata::Schema module');
114 ## Prespecified variable
116 my $metadata_creation_user = $ENV{METADATA_TEST_METALOADER};
118 ## The biosource schema contain all the metadata classes so don't need to create another Metadata schema
119 ## CXGN::DB::DBICFactory is obsolete, it has been replaced by CXGN::Metadata::Schema
121 my $schema = CXGN::Metadata::Schema->connect( $ENV{METADATA_TEST_DBDSN},
122 $ENV{METADATA_TEST_DBUSER},
123 $ENV{METADATA_TEST_DBPASS},
124 {on_connect_do => $set_path});
127 $schema->txn_begin();
130 ## Get the last values
131 my %nextvals = $schema->get_nextval();
132 my $last_metadata_id = $nextvals{'md_metadata'};
133 my $last_dbversion_id = $nextvals{'md_dbversion'};
135 ## Create a empty metadata object to use in the database store functions
136 my $metadbdata = CXGN::Metadata::Metadbdata->new($schema, $metadata_creation_user);
137 my $creation_date = $metadbdata->get_object_creation_date();
138 my $creation_user_id = $metadbdata->get_object_creation_user_by_id();
140 ## FIRST TEST BLOCK (TEST FROM 4 TO 6)
141 ## This is the first group of tests, to check if an empty object can store and after can return the data
142 ## Create a new empty object;
144 my $dbversion = CXGN::Metadata::Dbversion->new($schema, undef);
146 ## Load of the eight different parameters for an empty object using a hash with keys=root name for tha function and
147 ## values=value to test
149 my %test_values_for_empty_object=( dbversion_id => $last_dbversion_id+1,
150 patch_name => '9999_patch_test',
151 patch_description => 'this is a test for description',
154 ## Load the data in the empty object
155 my @function_keys = sort keys %test_values_for_empty_object;
156 foreach my $rootfunction (@function_keys) {
157 my $setfunction = 'set_' . $rootfunction;
158 if ($rootfunction eq 'dbversion_id') {
159 $setfunction = 'force_set_' . $rootfunction;
161 $dbversion->$setfunction($test_values_for_empty_object{$rootfunction});
163 ## Get the data from the object and store in two hashes. The first %getdata with keys=root_function_name and
164 ## value=value_get_from_object and the second, %testname with keys=root_function_name and values=name for the test.
166 my (%getdata, %testnames);
167 foreach my $rootfunction (@function_keys) {
168 my $getfunction = 'get_'.$rootfunction;
169 my $data = $dbversion->$getfunction();
170 $getdata{$rootfunction} = $data;
171 my $testname = 'BASIC SET/GET FUNCTION for ' . $rootfunction.' test';
172 $testnames{$rootfunction} = $testname;
175 ## And now run the test for each function and value
177 foreach my $rootfunction (@function_keys) {
178 is($getdata{$rootfunction}, $test_values_for_empty_object{$rootfunction}, $testnames{$rootfunction})
179 or diag "Looks like this failed.";
182 ### SECOND TEST BLOCK
183 ### Use of store functions.
185 eval {
187 ### It will create a new object based in dbipath and it will store (TEST 7 to 9)
188 my $dbversion2 = CXGN::Metadata::Dbversion->new($schema);
189 $dbversion2->set_patch_name('9998_patch_test');
190 $dbversion2->set_patch_description('This is a description test');
191 $dbversion2->store($metadbdata);
193 is($dbversion2->get_dbversion_id(), $last_dbversion_id+1, 'STORE FUNCTION test, checking dbversion_id')
194 or diag "Looks like this failed";
195 is($dbversion2->get_patch_name(), '9998_patch_test', 'STORE FUNCTION test, checking patch_name')
196 or diag "Looks like this failed";
197 is($dbversion2->get_patch_description(), 'This is a description test', 'STORE FUNCTION test, checking patch_description')
198 or diag "Looks like this failed";
200 ## Get the metadbdata and test obsolete (TEST 10 and 11)
202 my $dbv_md = $dbversion2->get_metadbdata();
203 is($dbv_md->get_metadata_id(), $last_metadata_id+1, 'STORE FUNCTION (as insert) and GET METADATA test, checking metadata_id')
204 or diag "Looks like this failed";
206 is($dbversion2->is_obsolete(), 0, 'IS OBSOLETE FUNCTION test, checking boolean false')
207 or diag "Looks like this failed";
209 ## Testing new object based in path_name (TEST 12)
210 my $dbversion3 = CXGN::Metadata::Dbversion->new($schema);
211 $dbversion3->set_patch_name('9999_patch_test');
212 $dbversion3->set_patch_description('This is a description test');
213 $dbversion3->store($metadbdata);
215 my $dbversion4 = CXGN::Metadata::Dbversion->new_by_patch_name($schema, '9999_patch_test');
216 is($dbversion4->get_dbversion_id(), $last_dbversion_id+2, 'CONTRUCTOR new_by_patch_name TEST, checking dbversion_id')
217 or diag "Looks like this failed";
219 ## Testing modification and storing (TEST 13 and 14)
220 $dbversion4->set_patch_description('This is another description test');
221 $dbversion4->store($metadbdata);
223 my $dbversion5 = CXGN::Metadata::Dbversion->new($schema, $dbversion4->get_dbversion_id() );
224 is($dbversion5->get_patch_description(), 'This is another description test', 'STORE FUNCTION (as update) test, checking patch_descr')
225 or diag "Looks like this failed";
227 my $dbv_md2 = $dbversion4->get_metadbdata();
228 is($dbv_md2->get_metadata_id(), $last_metadata_id+2, 'STORE FUNCTION (as update) and GET METADATA test, checking metadata_id')
229 or diag "Looks like this failed";
231 ## Testing obsolete and revert obsolete something (TEST 15 to 18)
232 $dbversion5->obsolete($metadbdata, 'Obsolete test');
233 is($dbversion5->is_obsolete(), 1, 'OBSOLETE FUNCTION test, checking boolean with is_obsolete() method')
234 or diag "Looks like this failed";
236 is($dbversion5->get_metadbdata()->get_metadata_id(), $last_metadata_id+3, 'OBSOLETE FUNTION test, checking new metadata_id')
237 or diag "Looks like this failed";
239 $dbversion5->obsolete($metadbdata, 'Obsolete test', 'REVERT');
240 is($dbversion5->is_obsolete(), 0, 'REVERT OBSOLETE FUNCTION test, checking boolean with is_obsolete() method')
241 or diag "Looks like this failed";
243 is($dbversion5->get_metadbdata()->get_metadata_id(), $last_metadata_id+4, 'REVERT OBSOLETE FUNTION test, checking new metadata_id')
244 or diag "Looks like this failed";
246 ##############################################################################
247 ## DEPRECATED METHODS (get_patch_number and check_previous_dbpatches) ###
248 ##############################################################################
250 #### Cheking a get_patch_number (TEST 19 and 20)
251 ## my $patch_number = $dbversion5->get_patch_number();
252 ## is ($patch_number, 9999, 'GET PATCH NUMBER METHOD, checking patch number')
253 ## or diag "Looks like this failed";
255 #### It will test too with a patch name with zeros
257 my $dbversion6 = CXGN::Metadata::Dbversion->new($schema);
258 $dbversion6->set_patch_name('00023_patch_test');
260 ## is($dbversion6->get_patch_number(), 23, 'GET PATCH NUMBER METHOD, cheking patch number with zeros')
261 ## or diag "Looks like this failed";
263 ## Using exists dbpatches (TEST 19 and 20)
265 is($dbversion6->exists_dbpatch('9999_patch_test'), 1, 'EXISTS DBPATCH METHOD, checking boolean for a true value')
266 or diag "Looks like this failed";
267 is($dbversion6->exists_dbpatch('0023_patch_test'), 0, 'EXISTS DBPATCH METHOD, cheking boolean for a false value')
268 or diag "Looks like this failed";
270 ## DEPRECATED METHOD
271 #### Using checking previous dbpatches. It will check this function in three different ways:
273 ## ## 1-With a specific value, 9999_patch_test. It should return 9998 keys into the hash, perhaps more if exists
274 ## ## patch names with the same patch number, for example if during this test exists an original 9998_something
276 ## ## (TEST 23 to 26)
278 ## my %check_previous1 = $dbversion6->check_previous_dbpatches('9999_patch_test');
279 ## my $key_n1 = scalar(keys %check_previous1);
281 ## It can have more than one patch with the same number, to fix that.
283 ## my %rep;
284 ## my $same_c = 0;
285 ## foreach my $p (keys %check_previous1) {
286 ## if ($p =~ m/^(\d+)/) {
287 ## my $number = $1;
289 ## if (exists $rep{$number}) {
290 ## $same_c++;
291 ## }
292 ## else {
293 ## $rep{$number} = 1;
294 ## }
295 ## }
296 ## }
298 ## my $expected_key_n = 9998;
299 ## if ($last_dbversion_id > 9998) {
300 ## $expected_key_n = 9999;
301 ## }
302 ## $expected_key_n += $same_c;
304 ## is($key_n1, $expected_key_n, 'CHECKING PREVIOUS DBPATCHES METHOD, checking number of previous patch numbers (for 9999_patch_test)')
305 ## or diag "Looks like this failed";
307 ## is($check_previous1{'9998_patch_test'}, 1, 'CHECKING PREVIOUS DBPATCHES METHOD, checking boolean for a known patch name (9998)')
308 ## or diag "Looks like this failed";
310 ## is($check_previous1{'0000_patch_test'}, undef, 'CHECKING PREVIOUS DBPATCHES METHOD, checking boolean for a unknown patch name (0000)')
311 ## or diag "Looks like this failed";
313 ## if ($last_dbversion_id < 9997) {
315 ## 9997 should be a unknown number only if last_dbversion_id < 9997
316 ## is($check_previous1{'9997'}, 0, 'CHEKING PREVIOUS DBPATCHES METHOD, checking boolean for a unknown patch number (9997)')
317 ## or diag "Looks like this failed";
319 ## }
320 ## else {
322 ## It still should do the same number of test.. so
323 ## is($check_previous1{'9997'}, undef, 'CHEKING PREVIOUS DBPATCHES METHOD, checking boolean for a unknown patch name (9997)')
324 ## or diag "Looks like this failed";
325 ## }
327 ## 2- Without any specific value, in this case should take the $patch_name for $dbversion6.
328 ## We don't know how many of the db_patches exists from 22 to 1 but we know that there are 22 patch numbers
330 ## TEST 27
332 ## my %check_previous2 = $dbversion6->check_previous_dbpatches();
333 ## is(scalar( keys %check_previous2), 22, 'CHECKING PREVIOUS DBPATCHES METHOD, checking previous patch n for patch 0023_patch_test')
334 ## or diag "Looks like this failed";
337 ## 3- In an object without any patch_name, it should take the last_dbversion_id (9999_test_patch for this case)
339 ## TEST 28 and 29
341 ## my $empty_dbversion = CXGN::Metadata::Dbversion->new($schema);
342 ## my %check_previous3 = $empty_dbversion->check_previous_dbpatches();
344 ## is($key_n1, $expected_key_n, 'CHECKING PREVIOUS DBPATCHES METHOD, checking number of previous patch numbers in EMPTY OBJ')
345 ## or diag "Looks like this failed";
347 ## is($check_previous1{'9998_patch_test'}, 1, 'CHECKING PREVIOUS DBPATCHES METHOD, checking boolean for a known patch name in EMPTY OBJ')
348 ## or diag "Looks like this failed";
350 ## Testing the complete checking
352 my $dbversion7 = CXGN::Metadata::Dbversion->new($schema);
354 ## Check that this patch has been executed before (test 30)
356 throws_ok { $dbversion7->complete_checking({ patch_name => '9998_patch_test'}) } qr/DBPATCH EXECUTION ERROR/,
357 "TESTING DIE for complete_checking when use patch_name";
359 ## Check that other previous patches by default (using patch number) has not been runned (TEST 31) ## DEPRECATED ##
361 ## throws_ok { $dbversion7->complete_checking({ patch_name => '9999_other_test'}) } qr/PREVIOUS DB_PATCH by default ERROR/,
362 ## "TESTING DIE for complete_checking when previous patches that don't exists";
364 ## Check a list of previous patches using a list (TEST 32)
366 throws_ok { $dbversion7->complete_checking({ patch_name => '9999_other_test', prepatch => ['9998_alt_test'] }) }
367 qr/PREVIOUS DB_PATCH ERROR/,
368 "TESTING DIE for complete_checking when use patch_name";
370 ## Test when pass the complete_cheking (TEST 33)
372 lives_ok { $dbversion7->complete_checking({ patch_name => '9999_other_test', prepatch => ['9998_patch_test'] }) }
373 "TESTING DON'T DIE for complete_checking when pass the check";
376 }; ## End of the eval function
378 if ($@) {
379 print "\nEVAL ERROR:\n\n$@\n";
382 ## RESTORING THE ORIGINAL STATE IN THE DATABASE
383 ## To restore the original state in the database, rollback (it is in a transaction) and set the table_sequence values.
385 $schema->txn_rollback();
387 ## The transaction change the values in the sequence, so if we want set the original value, before the changes
388 ## we have two options:
389 ## 1) SELECT setval (<sequence_name>, $last_value_before_change, true); that said, ok your last true value was...
390 ## 2) SELECT setval (<sequence_name>, $last_value_before_change+1, false); It is false that your last value was ... so the
391 ## next time take the value before this.
393 ## The option 1 leave the seq information in a original state except if there aren't any value in the seq, that it is
394 ## more as the option 2
396 ## The test does not reset the db sequences anymore.
398 ####
399 1; #
400 ####