[Rt-devel] [RFC][PATCH] DBIx-SB tests on other DBs

Ruslan U. Zakirov Ruslan.Zakirov at miet.ru
Tue May 17 09:42:20 EDT 2005


Jesse Vincent wrote:
> 
>>I expect it would be merged before 1.30 because:
>>1) it has no functionality changes;
>>2) mysql tests are disabled by default;
>>3) I'm waiting for some feedback.
> 
> 
> Looks quite neat. How are you generating this patch? I can't apply it
> cleanly here.
> 
Sorry, didn't test how it applies to trunk :( changes were partly 
applied to local branch so patch was not consistent.

--
Regards, Ruslan.

P.S. There is also change in SearchBuilder/Handle.pm in the patch that 
is look like cosmetic, but I was forced by "strange" warning. You can 
see it if don't apply this particular change.
-------------- next part --------------
=== t/01records.t
==================================================================
--- t/01records.t   (/mirrors/DBIx-SearchBuilder/trunk)   (revision 1629)
+++ t/01records.t   (/DBIx-SearchBuilder/local)   (revision 1629)
@@ -6,105 +6,116 @@
 use File::Spec;
 use Test::More;
 BEGIN { require "t/utils.pl" }
+our (@AvailableDrivers);
 
-eval "use DBD::SQLite";
-if ($@) { 
-plan skip_all => "DBD::SQLite required for testing database interaction" 
-} else{
-plan tests => 30;
-}
+use constant TESTS_PER_DRIVER => 30;
 
-my $handle = get_handle('SQLite');
-$handle->Connect( Driver => 'SQLite', Database => File::Spec->catfile(File::Spec->tmpdir(), "sb-test.$$"));
-isa_ok($handle->dbh, 'DBI::db');
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
+plan tests => $total;
 
-my $ret = $handle->SimpleQuery(TestApp::Address->schema);
-isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back");
+foreach my $d ( @AvailableDrivers ) {
+SKIP: {
+	unless( has_schema( 'TestApp::Address', $d ) ) {
+		skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+	}
+	unless( should_test( $d ) ) {
+		skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+	}
 
+	my $handle = get_handle( $d );
+	connect_handle( $handle );
+	isa_ok($handle->dbh, 'DBI::db');
 
-my $rec = TestApp::Address->new($handle);
-isa_ok($rec, 'DBIx::SearchBuilder::Record');
+	my $ret = init_schema( 'TestApp::Address', $handle );
+	isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back");
 
-# _Accessible testings
-is( $rec->_Accessible('id' => 'read'), 1, 'id is accessible for read' );
-is( $rec->_Accessible('id' => 'write'), undef, 'id is not accessible for write' );
-is( $rec->_Accessible('unexpected_field' => 'read'), undef, "field doesn't exist and can't be accessible for read" );
+	my $rec = TestApp::Address->new($handle);
+	isa_ok($rec, 'DBIx::SearchBuilder::Record');
 
-can_ok($rec,'Create');
+	# _Accessible testings
+	is( $rec->_Accessible('id' => 'read'), 1, 'id is accessible for read' );
+	is( $rec->_Accessible('id' => 'write'), undef, 'id is not accessible for write' );
+	is( $rec->_Accessible('unexpected_field' => 'read'), undef, "field doesn't exist and can't be accessible for read" );
 
-my ($id) = $rec->Create( Name => 'Jesse', Phone => '617 124 567');
-ok($id,"Created record ". $id);
-ok($rec->Load($id), "Loaded the record");
+	can_ok($rec,'Create');
 
+	my ($id) = $rec->Create( Name => 'Jesse', Phone => '617 124 567');
+	ok($id,"Created record ". $id);
+	ok($rec->Load($id), "Loaded the record");
 
-is($rec->id, $id, "The record has its id");
-is ($rec->Name, 'Jesse', "The record's name is Jesse");
 
-my ($val, $msg) = $rec->SetName('Obra');
-ok($val, $msg) ;
-is($rec->Name, 'Obra', "We did actually change the name");
+	is($rec->id, $id, "The record has its id");
+	is ($rec->Name, 'Jesse', "The record's name is Jesse");
 
-# Validate immutability of the field id
-($val, $msg) = $rec->Setid( $rec->id + 1 );
-ok(!$val, $msg);
-is($msg, 'Immutable field', 'id is immutable field');
-is($rec->id, $id, "The record still has its id");
+	my ($val, $msg) = $rec->SetName('Obra');
+	ok($val, $msg) ;
+	is($rec->Name, 'Obra', "We did actually change the name");
 
-# Check some non existant field
-ok( !eval{ $rec->SomeUnexpectedField }, "The record has no 'SomeUnexpectedField'");
-{
-	# test produce DBI warning
-	local $SIG{__WARN__} = sub {return};
-	is( $rec->_Value( 'SomeUnexpectedField' ), undef, "The record has no 'SomeUnexpectedField'");
-}
-($val, $msg) = $rec->SetSomeUnexpectedField( 'foo' );
-ok(!$val, $msg);
-is($msg, 'Nonexistant field?', "Field doesn't exist");
-($val, $msg) = $rec->_Set('SomeUnexpectedField', 'foo');
-ok(!$val, "$msg");
+	# Validate immutability of the field id
+	($val, $msg) = $rec->Setid( $rec->id + 1 );
+	ok(!$val, $msg);
+	is($msg, 'Immutable field', 'id is immutable field');
+	is($rec->id, $id, "The record still has its id");
 
+	# Check some non existant field
+	ok( !eval{ $rec->SomeUnexpectedField }, "The record has no 'SomeUnexpectedField'");
+	{
+		# test produce DBI warning
+		local $SIG{__WARN__} = sub {return};
+		is( $rec->_Value( 'SomeUnexpectedField' ), undef, "The record has no 'SomeUnexpectedField'");
+	}
+	($val, $msg) = $rec->SetSomeUnexpectedField( 'foo' );
+	ok(!$val, $msg);
+	is($msg, 'Nonexistant field?', "Field doesn't exist");
+	($val, $msg) = $rec->_Set('SomeUnexpectedField', 'foo');
+	ok(!$val, "$msg");
 
-# Validate truncation on update
 
-($val,$msg) = $rec->SetName('1234567890123456789012345678901234567890');
+	# Validate truncation on update
 
-ok($val, $msg) ;
+	($val,$msg) = $rec->SetName('1234567890123456789012345678901234567890');
 
-is($rec->Name, '12345678901234', "Truncated on update");
+	ok($val, $msg) ;
 
+	is($rec->Name, '12345678901234', "Truncated on update");
 
 
-# Test unicode truncation:
-my $univalue = "這是個測試";
 
-($val,$msg) = $rec->SetName($univalue.$univalue);
+	# Test unicode truncation:
+	my $univalue = "這是個測試";
 
-ok($val, $msg) ;
+	($val,$msg) = $rec->SetName($univalue.$univalue);
 
-is($rec->Name, '這是個測');
+	ok($val, $msg) ;
 
+	is($rec->Name, '這是個測');
 
 
-# make sure we do _not_ truncate things which should not be truncated
-($val,$msg) = $rec->SetEmployeeId('1234567890');
 
-ok($val, $msg) ;
+	# make sure we do _not_ truncate things which should not be truncated
+	($val,$msg) = $rec->SetEmployeeId('1234567890');
 
-is($rec->EmployeeId, '1234567890', "Did not truncate id on create");
+	ok($val, $msg) ;
 
-# make sure we do truncation on create
-my $newrec = TestApp::Address->new($handle);
-my $newid = $newrec->Create( Name => '1234567890123456789012345678901234567890',
-                             EmployeeId => '1234567890' );
+	is($rec->EmployeeId, '1234567890', "Did not truncate id on create");
 
-$newrec->Load($newid);
+	# make sure we do truncation on create
+	my $newrec = TestApp::Address->new($handle);
+	my $newid = $newrec->Create( Name => '1234567890123456789012345678901234567890',
+	                             EmployeeId => '1234567890' );
 
-ok ($newid, "Created a new record");
-is($newrec->Name, '12345678901234', "Truncated on create");
-is($newrec->EmployeeId, '1234567890', "Did not truncate id on create");
+	$newrec->Load($newid);
 
+	ok ($newid, "Created a new record");
+	is($newrec->Name, '12345678901234', "Truncated on create");
+	is($newrec->EmployeeId, '1234567890', "Did not truncate id on create");
 
+}} # SKIP, foreach blocks
 
+1;
+
+
+
 package TestApp::Address;
 
 use base qw/DBIx::SearchBuilder::Record/;
@@ -133,9 +144,20 @@
 
 }
 
+sub schema_mysql {
+<<EOF;
+CREATE TEMPORARY TABLE Address (
+        id integer AUTO_INCREMENT,
+        Name varchar(36),
+        Phone varchar(18),
+        EmployeeId int(8),
+  	PRIMARY KEY (id))
+EOF
 
-sub schema {
+}
 
+sub schema_sqlite {
+
 <<EOF;
 CREATE TABLE Address (
         id  integer primary key,
=== t/01basics.t
==================================================================
--- t/01basics.t   (/mirrors/DBIx-SearchBuilder/trunk)   (revision 1629)
+++ t/01basics.t   (/DBIx-SearchBuilder/local)   (revision 1629)
@@ -4,17 +4,15 @@
 
 use Test::More;
 BEGIN { require "t/utils.pl" }
-our (@SupportedDrivers);
+our (@AvailableDrivers);
 
-my $total = scalar(@SupportedDrivers) * 4;
+use constant TESTS_PER_DRIVER => 4;
+
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
 
-foreach my $d ( @SupportedDrivers ) {
+foreach my $d ( @AvailableDrivers ) {
 SKIP: {
-	eval "require DBD::$d";
-	if( $@ ) {
-		skip "DBD::$d is not installed", 4;
-	}
 	use_ok('DBIx::SearchBuilder::Handle::'. $d);
 	my $handle = get_handle( $d );
 	isa_ok($handle, 'DBIx::SearchBuilder::Handle');
=== t/utils.pl
==================================================================
--- t/utils.pl   (/mirrors/DBIx-SearchBuilder/trunk)   (revision 1629)
+++ t/utils.pl   (/DBIx-SearchBuilder/local)   (revision 1629)
@@ -2,6 +2,14 @@
 
 use strict;
 
+=head1 VARIABLES
+
+=head2 @SupportedDrivers
+
+Array of all supported DBD drivers.
+
+=cut
+
 our @SupportedDrivers = qw(
 	Informix
 	mysql
@@ -13,7 +21,24 @@
 	Sybase
 );
 
+=head2 @AvailableDrivers
 
+Array that lists only drivers from supported list
+that user has installed.
+
+=cut
+
+our @AvailableDrivers = grep { eval "require DBD::". $_ } @SupportedDrivers;
+
+=head1 FUNCTIONS
+
+=head2 get_handle
+
+Returns new DB specific handle. Takes one argument DB C<$type>.
+Other arguments uses to construct handle.
+
+=cut
+
 sub get_handle
 {
 	my $type = shift;
@@ -21,11 +46,107 @@
 	eval "require $class";
 	die $@ if $@;
 	my $handle;
-	{
-#		no strict 'refs';
-		$handle = $class->new( @_ );
-	}
+	$handle = $class->new( @_ );
 	return $handle;
 }
 
+=head2 handle_to_driver
+
+Returns driver name which gets from C<$handle> object argument.
+
+=cut
+
+sub handle_to_driver
+{
+	my $driver = ref($_[0]);
+	$driver =~ s/^.*:://;
+	return $driver;
+}
+
+=head2 connect_handle
+
+Connects C<$handle> object to DB.
+
+=cut
+
+sub connect_handle
+{
+	my $call = "connect_". lc handle_to_driver( $_[0] );
+	return unless defined &$call;
+	goto &$call;
+}
+
+sub connect_sqlite
+{
+	my $handle = shift;
+	return $handle->Connect(
+		Driver => 'SQLite',
+		Database => File::Spec->catfile(File::Spec->tmpdir(), "sb-test.$$")
+	);
+}
+
+sub connect_mysql
+{
+	my $handle = shift;
+	return $handle->Connect(
+		Driver => 'mysql',
+		Database => $ENV{'SB_TEST_MYSQL'},
+		User => $ENV{'SB_TEST_MYSQL_USER'} || 'root',
+		Pass => $ENV{'SB_TEST_MYSQL_PASS'} || '',
+	);
+}
+
+=head2 should_test
+
+Checks environment for C<SB_TEST_*> variables.
+Returns true if specified DB back-end should be tested.
+Takes one argument C<$driver> name.
+
+=cut
+
+sub should_test
+{
+	my $driver = shift;
+	return 1 if lc $driver eq 'sqlite';
+	my $env = 'SB_TEST_'. uc $driver;
+	return $ENV{$env};
+}
+
+=head2 had_schema
+
+Returns true if C<$class> has schema for C<$driver>.
+
+=cut
+
+sub has_schema
+{
+	my ($class, $driver) = @_;
+	my $call = $class ."::schema_". lc $driver;
+	return 1 if defined &$call;
+	return 0;
+}
+
+=head2 init_schema
+
+Takes C<$class> and C<$handle> and inits schema by calling
+C<schema_$driver> method of the C<$class>.
+Returns last C<DBI::st> on success or last return value of the
+SimpleQuery method on error.
+
+=cut
+
+sub init_schema
+{
+	my ($class, $handle) = @_;
+	my $call = "schema_". lc handle_to_driver( $handle );
+	my $schema = $class->$call();
+	$schema = ref( $schema )? $schema : [$schema];
+	my $ret;
+	foreach my $query( @$schema ) {
+		$ret = $handle->SimpleQuery( $query );
+		return $ret unless UNIVERSAL::isa( $ret, 'DBI::st' );
+	}
+	return $ret;
+}
+
 1;
=== t/02records_object.t
==================================================================
--- t/02records_object.t   (/mirrors/DBIx-SearchBuilder/trunk)   (revision 1629)
+++ t/02records_object.t   (/DBIx-SearchBuilder/local)   (revision 1629)
@@ -4,44 +4,54 @@
 use strict;
 use warnings;
 use File::Spec;
+use Test::More;
 
 BEGIN { require "t/utils.pl" }
+our (@AvailableDrivers);
 
-use Test::More;
-eval "use DBD::SQLite";
-if ($@) { 
-plan skip_all => "DBD::SQLite required for testing database interaction" 
-} else{
-plan tests => 9;
-}
-my $handle = get_handle('SQLite');
-$handle->Connect( Driver => 'SQLite', Database => File::Spec->catfile(File::Spec->tmpdir(), "sb-test.$$"));
-isa_ok($handle->dbh, 'DBI::db');
+use constant TESTS_PER_DRIVER => 8;
 
-foreach( @{ TestApp->schema } ) {
-	my $ret = $handle->SimpleQuery($_);
+my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+foreach my $d ( @AvailableDrivers ) {
+SKIP: {
+	unless( has_schema( 'TestApp', $d ) ) {
+		skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+	}
+	unless( should_test( $d ) ) {
+		skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+	}
+
+	my $handle = get_handle( $d );
+	connect_handle( $handle );
+	isa_ok($handle->dbh, 'DBI::db');
+
+	my $ret = init_schema( 'TestApp', $handle );
 	isa_ok($ret,'DBI::st', "Inserted the schema. got a statement handle back");
-}
 
+	my $emp = TestApp::Employee->new($handle);
+	my $e_id = $emp->Create( Name => 'RUZ' );
+	ok($e_id, "Got an ide for the new emplyee");
+	my $phone = TestApp::Phone->new($handle);
+	isa_ok( $phone, 'TestApp::Phone', "it's atestapp::phone");
+	my $p_id = $phone->Create( Employee => $e_id, Phone => '+7(903)264-03-51');
+	# XXX: test fails if next string is commented
+	is($p_id, 1, "Loaded record $p_id");
+	$phone->Load( $p_id );
 
-my $emp = TestApp::Employee->new($handle);
-my $e_id = $emp->Create( Name => 'RUZ' );
-ok($e_id, "Got an ide for the new emplyee");
-my $phone = TestApp::Phone->new($handle);
-isa_ok( $phone, 'TestApp::Phone', "it's atestapp::phone");
-my $p_id = $phone->Create( Employee => $e_id, Phone => '+7(903)264-03-51');
-# XXX: test fails if next string is commented
-is($p_id, 1, "Loaded record $p_id");
-$phone->Load( $p_id );
+	my $obj = $phone->EmployeeObj($handle);
+	ok($obj, "Employee #$e_id has phone #$p_id");
+	is($obj->id, $e_id);
+	is($obj->Name, 'RUZ');
 
-my $obj = $phone->EmployeeObj($handle);
-ok($obj, "Employee #$e_id has phone #$p_id");
-is($obj->id, $e_id);
-is($obj->Name, 'RUZ');
+}} # SKIP, foreach blocks
 
+1;
 
+
 package TestApp;
-sub schema {
+sub schema_sqlite {
 [
 q{
 CREATE TABLE Employees (
@@ -55,7 +65,21 @@
 	Phone varchar(18)
 ) }
 ]
+}
 
+sub schema_mysql {
+[ q{
+CREATE TEMPORARY TABLE Employees (
+	id integer AUTO_INCREMENT primary key,
+	Name varchar(36)
+)
+}, q{
+CREATE TEMPORARY TABLE Phones (
+	id integer AUTO_INCREMENT primary key,
+	Employee integer NOT NULL,
+	Phone varchar(18)
+)
+} ]
 }
 
 package TestApp::Employee;
=== SearchBuilder/Handle.pm
==================================================================
--- SearchBuilder/Handle.pm   (/mirrors/DBIx-SearchBuilder/trunk)   (revision 1629)
+++ SearchBuilder/Handle.pm   (/DBIx-SearchBuilder/local)   (revision 1629)
@@ -85,7 +85,7 @@
            DisconnectHandleOnDestroy => undef,
 	       @_);
 
-    my $dsn = $self->DSN;
+    my $dsn = $self->DSN || '';
 
     # Setting this actually breaks old RT versions in subtle ways. So we need to explicitly call it
 

Property changes on: 
___________________________________________________________________
Name: svk:merge
  4c7c5507-10e6-0310-a638-9a99c59afa75:/local/sb:9860
  a51291e0-c2ea-0310-847b-fbb8d8170edb:/local/sb:16880
  d53a6f7f-efd6-0310-930d-faa3dfc79e01:/local/SearchBuilder:1719
 +e417ac7c-1bcc-0310-8ffa-8f5827389a85:/DBIx-SearchBuilder/trunk:2885


More information about the Rt-devel mailing list