Archive Ensembl HomeArchive Ensembl Home
TestUtils.pm
Go to the documentation of this file.
00001 use strict;
00002 
00003 package TestUtils;
00004 
00005 require Exporter;
00006 use vars qw( @ISA @EXPORT_OK );
00007 @ISA=('Exporter');
00008 @EXPORT_OK=qw(&debug &test_getter_setter &count_rows);
00009 
00010 =head2 test_getter_setter
00011 
00012   Arg [1]    : Object $object
00013                The object to test the getter setter on
00014   Arg [2]    : string $method
00015                The name of the getter setter method to test
00016   Arg [3]    : $test_val
00017                The value to use to test the set behavior of the method.
00018   Example    : ok(&TestUtils::test_getter_setter($object, 'type', 'value'));
00019   Description: Tests a getter setter method by attempting to set a value
00020                and verifying that the newly set value can be retrieved.  The
00021                old value of the the attribute is restored after the test 
00022                (providing the method functions correctly).
00023   Returntype : boolean - true value on success, false on failure
00024   Exceptions : none
00025   Caller     : test scripts
00026 
00027 =cut
00028 
00029 sub test_getter_setter {
00030     my ($object, $method, $test_val) = @_;
00031     
00032     my $ret_val = 0;
00033     
00034     #save the old value
00035     my $old_val = $object->$method;
00036     
00037     $object->$method($test_val);
00038     
00039     #verify value was set
00040     $ret_val = (!defined($test_val) && !defined($object->$method)) || 
00041            ($object->$method eq $test_val);
00042 
00043     #restore the old value
00044     $object->$method($old_val);
00045     
00046     return $ret_val;
00047 }
00048 
00049 sub debug {
00050   if( $::verbose ) {
00051     print STDERR @_,"\n";
00052   }
00053 }
00054 
00055 sub count_rows {
00056   my $db = shift;
00057   my $tablename = shift;
00058 
00059   my $sth = $db->prepare( "select count(*) from $tablename" );
00060   $sth->execute();
00061   my ( $count ) = $sth->fetchrow_array();
00062   return $count;
00063 }
00064 
00065 
00066 
00067 1;
00068 
00069