[Rt-commit] r2903 - in DBIx-SearchBuilder/trunk: . SearchBuilder t
jesse at bestpractical.com
jesse at bestpractical.com
Sat May 21 02:00:14 EDT 2005
Author: jesse
Date: Sat May 21 02:00:13 2005
New Revision: 2903
Modified:
DBIx-SearchBuilder/trunk/ (props changed)
DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
DBIx-SearchBuilder/trunk/t/01basics.t
DBIx-SearchBuilder/trunk/t/01records.t
DBIx-SearchBuilder/trunk/t/02records_object.t
DBIx-SearchBuilder/trunk/t/utils.pl
Log:
r16970 at hualien: jesse | 2005-05-21 01:58:40 -0400
* Significant testing updates from ruslan to support multi-handle testing.
Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm (original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm Sat May 21 02:00:13 2005
@@ -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
Modified: DBIx-SearchBuilder/trunk/t/01basics.t
==============================================================================
--- DBIx-SearchBuilder/trunk/t/01basics.t (original)
+++ DBIx-SearchBuilder/trunk/t/01basics.t Sat May 21 02:00:13 2005
@@ -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');
Modified: DBIx-SearchBuilder/trunk/t/01records.t
==============================================================================
--- DBIx-SearchBuilder/trunk/t/01records.t (original)
+++ DBIx-SearchBuilder/trunk/t/01records.t Sat May 21 02:00:13 2005
@@ -6,102 +6,114 @@
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
+ # Validate truncation on update
-($val,$msg) = $rec->SetName('1234567890123456789012345678901234567890');
+ ($val,$msg) = $rec->SetName('1234567890123456789012345678901234567890');
-ok($val, $msg) ;
+ ok($val, $msg) ;
-is($rec->Name, '12345678901234', "Truncated on update");
+ is($rec->Name, '12345678901234', "Truncated on update");
-# Test unicode truncation:
-my $univalue = "這是個測試";
+ # Test unicode truncation:
+ my $univalue = "這是個測試";
-($val,$msg) = $rec->SetName($univalue.$univalue);
+ ($val,$msg) = $rec->SetName($univalue.$univalue);
-ok($val, $msg) ;
+ ok($val, $msg) ;
-is($rec->Name, '這是個測');
+ is($rec->Name, '這是個測');
-# make sure we do _not_ truncate things which should not be truncated
-($val,$msg) = $rec->SetEmployeeId('1234567890');
+ # make sure we do _not_ truncate things which should not be truncated
+ ($val,$msg) = $rec->SetEmployeeId('1234567890');
-ok($val, $msg) ;
+ ok($val, $msg) ;
-is($rec->EmployeeId, '1234567890', "Did not truncate id on create");
+ is($rec->EmployeeId, '1234567890', "Did not truncate id on create");
-# make sure we do truncation on create
-my $newrec = TestApp::Address->new($handle);
-my $newid = $newrec->Create( Name => '1234567890123456789012345678901234567890',
- EmployeeId => '1234567890' );
+ # make sure we do truncation on create
+ my $newrec = TestApp::Address->new($handle);
+ my $newid = $newrec->Create( Name => '1234567890123456789012345678901234567890',
+ EmployeeId => '1234567890' );
-$newrec->Load($newid);
+ $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");
+ ok ($newid, "Created a new record");
+ is($newrec->Name, '12345678901234', "Truncated on create");
+ is($newrec->EmployeeId, '1234567890', "Did not truncate id on create");
+
+ cleanup_schema( 'TestApp::Address', $handle );
+}} # SKIP, foreach blocks
+
+1;
@@ -133,8 +145,19 @@
}
+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 (
Modified: DBIx-SearchBuilder/trunk/t/02records_object.t
==============================================================================
--- DBIx-SearchBuilder/trunk/t/02records_object.t (original)
+++ DBIx-SearchBuilder/trunk/t/02records_object.t Sat May 21 02:00:13 2005
@@ -4,44 +4,55 @@
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;
+
+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');
-foreach( @{ TestApp->schema } ) {
- my $ret = $handle->SimpleQuery($_);
+ 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 $obj = $phone->EmployeeObj($handle);
+ ok($obj, "Employee #$e_id has phone #$p_id");
+ is($obj->id, $e_id);
+ is($obj->Name, 'RUZ');
+
+ cleanup_schema( 'TestApp', $handle );
+}} # SKIP, foreach blocks
-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');
+1;
package TestApp;
-sub schema {
+sub schema_sqlite {
[
q{
CREATE TABLE Employees (
@@ -55,7 +66,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;
Modified: DBIx-SearchBuilder/trunk/t/utils.pl
==============================================================================
--- DBIx-SearchBuilder/trunk/t/utils.pl (original)
+++ DBIx-SearchBuilder/trunk/t/utils.pl Sat May 21 02:00:13 2005
@@ -2,6 +2,14 @@
use strict;
+=head1 VARIABLES
+
+=head2 @SupportedDrivers
+
+Array of all supported DBD drivers.
+
+=cut
+
our @SupportedDrivers = qw(
Informix
mysql
@@ -13,6 +21,23 @@
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
{
@@ -21,11 +46,126 @@
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 $method = 'schema_'. lc $driver;
+ return UNIVERSAL::can( $class, $method );
+}
+
+=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;
+}
+
+=head2 cleanup_schema
+
+Takes C<$class> and C<$handle> and inits schema by calling
+C<cleanup_schema_$driver> method of the C<$class> if method exists.
+Always returns undef.
+
+=cut
+
+sub cleanup_schema
+{
+ my ($class, $handle) = @_;
+ my $call = "cleanup_schema_". lc handle_to_driver( $handle );
+ return unless UNIVERSAL::can( $class, $call );
+ my $schema = $class->$call();
+ $schema = ref( $schema )? $schema : [$schema];
+ foreach my $query( @$schema ) {
+ eval { $handle->SimpleQuery( $query ) };
+ }
+}
+
1;
More information about the Rt-commit
mailing list