[Bast-commits] r4315 - in DBIx-Class/0.08/branches/replication_dedux/t: . lib

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Thu May 1 16:56:10 BST 2008


Author: jnapiorkowski
Date: 2008-05-01 16:56:10 +0100 (Thu, 01 May 2008)
New Revision: 4315

Modified:
   DBIx-Class/0.08/branches/replication_dedux/t/93storage_replication.t
   DBIx-Class/0.08/branches/replication_dedux/t/lib/DBICTest.pm
Log:
changed replication test to support custom database connect info, added a little code to DBICTest to support this

Modified: DBIx-Class/0.08/branches/replication_dedux/t/93storage_replication.t
===================================================================
--- DBIx-Class/0.08/branches/replication_dedux/t/93storage_replication.t	2008-04-30 23:15:28 UTC (rev 4314)
+++ DBIx-Class/0.08/branches/replication_dedux/t/93storage_replication.t	2008-05-01 15:56:10 UTC (rev 4315)
@@ -2,7 +2,7 @@
 use warnings;
 use lib qw(t/lib);
 use Test::More;
-use Data::Dump qw/dump/;
+use DBICTest;
 
 BEGIN {
     eval "use Moose";
@@ -20,17 +20,18 @@
 ## Build a class to hold all our required testing data and methods.
 ## ----------------------------------------------------------------------------
 
-TESTSCHEMACLASS: {
+TESTSCHEMACLASSES: {
 
+    ## --------------------------------------------------------------------- ##
+    ## Create an object to contain your replicated stuff.
+    ## --------------------------------------------------------------------- ##
+    
     package DBIx::Class::DBI::Replicated::TestReplication;
    
     use DBICTest;
-    use File::Copy;
-    use Data::Dump qw/dump/;
-    
     use base qw/Class::Accessor::Fast/;
     
-    __PACKAGE__->mk_accessors( qw/schema master_path slave_paths/ );
+    __PACKAGE__->mk_accessors( qw/schema/ );
 
     ## Initialize the object
     
@@ -39,8 +40,6 @@
 	    my $self = $class->SUPER::new(@_);
 	
 	    $self->schema( $self->init_schema );
-	    $self->master_path("t/var/DBIxClass.db");
-	
 	    return $self;
 	}
     
@@ -52,16 +51,49 @@
         return $schema;
     }
     
+    sub generate_replicant_connect_info {}
+    sub replicate {}
+    sub cleanup {}
+
+  
+    ## --------------------------------------------------------------------- ##
+    ## Subclass for when you are using SQLite for testing, this provides a fake
+    ## replication support.
+    ## --------------------------------------------------------------------- ##
+        
+    package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
+
+    use DBICTest;
+    use File::Copy;    
+    use base 'DBIx::Class::DBI::Replicated::TestReplication';
+    
+    __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
+    
+    ## Set the mastep path from DBICTest
+    
+	sub new {
+	    my $class = shift @_;
+	    my $self = $class->SUPER::new(@_);
+	
+	    $self->master_path( DBICTest->_sqlite_dbfilename );
+	    $self->slave_paths([
+            "t/var/DBIxClass_slave1.db",
+            "t/var/DBIxClass_slave2.db",    
+        ]);
+        
+	    return $self;
+	}    
+	
     ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
     ## $storage->connect_info to be used for connecting replicants.
     
     sub generate_replicant_connect_info {
-    	my $self = shift @_;
+        my $self = shift @_;
         my @dsn = map {
             "dbi:SQLite:${_}";
         } @{$self->slave_paths};
         
-        return map { [$_,'','',{}] } @dsn;
+        return map { [$_,'','',{AutoCommit=>1}] } @dsn;
     }
     
     ## Do a 'good enough' replication by copying the master dbfile over each of
@@ -83,6 +115,33 @@
             unlink $slave;
         }     
     }
+    
+    ## --------------------------------------------------------------------- ##
+    ## Subclass for when you are setting the databases via custom export vars
+    ## This is for when you have a replicating database setup that you are
+    ## going to test against.  You'll need to define the correct $ENV and have
+    ## two slave databases to test against, as well as a replication system
+    ## that will replicate in less than 1 second.
+    ## --------------------------------------------------------------------- ##
+        
+    package DBIx::Class::DBI::Replicated::TestReplication::Custom; 
+    use base 'DBIx::Class::DBI::Replicated::TestReplication';
+    
+    ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
+    ## $storage->connect_info to be used for connecting replicants.
+    
+    sub generate_replicant_connect_info { 
+        return (
+            [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
+            [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],           
+        );
+    }
+    
+    ## pause a bit to let the replication catch up 
+    
+    sub replicate {
+    	sleep 1;
+    } 
 }
 
 ## ----------------------------------------------------------------------------
@@ -91,13 +150,12 @@
 
 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
 
-ok my $replicated = DBIx::Class::DBI::Replicated::TestReplication
-    ->new({
-        slave_paths=>[
-	        "t/var/DBIxClass_slave1.db",
-	        "t/var/DBIxClass_slave2.db",    
-        ],
-    }) => 'Created a replication object';
+my $replicated_class = DBICTest->has_custom_dsn ?
+    'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
+    'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
+
+ok my $replicated = $replicated_class->new
+    => 'Created a replication object';
     
 isa_ok $replicated->schema
     => 'DBIx::Class::Schema';

Modified: DBIx-Class/0.08/branches/replication_dedux/t/lib/DBICTest.pm
===================================================================
--- DBIx-Class/0.08/branches/replication_dedux/t/lib/DBICTest.pm	2008-04-30 23:15:28 UTC (rev 4314)
+++ DBIx-Class/0.08/branches/replication_dedux/t/lib/DBICTest.pm	2008-05-01 15:56:10 UTC (rev 4315)
@@ -43,9 +43,17 @@
 
 =cut
 
+sub has_custom_dsn {
+	return $ENV{"DBICTEST_DSN"} ? 1:0;
+}
+
+sub _sqlite_dbfilename {
+	return "t/var/DBIxClass.db";
+}
+
 sub _database {
     my $self = shift;
-    my $db_file = "t/var/DBIxClass.db";
+    my $db_file = $self->_sqlite_dbfilename;
 
     unlink($db_file) if -e $db_file;
     unlink($db_file . "-journal") if -e $db_file . "-journal";




More information about the Bast-commits mailing list