new config option to DBICTest to let you set an alternative storage type, start on...
[dbsrgits/DBIx-Class.git] / t / 93storage_replication.t
index afe7b76..43d3e77 100644 (file)
@@ -2,12 +2,13 @@ use strict;
 use warnings;
 use lib qw(t/lib);
 use Test::More;
+use Data::Dump qw/dump/;
 
 BEGIN {
-    eval "use DBD::Multi";
+    eval "use Moose";
     plan $@
-        ? ( skip_all => 'needs DBD::Multi for testing' )
-        : ( tests => 18 );
+        ? ( skip_all => 'needs Moose for testing' )
+        : ( tests => 2 );
 }      
 
 ## ----------------------------------------------------------------------------
@@ -15,8 +16,70 @@ BEGIN {
 ## ----------------------------------------------------------------------------
 
 TESTSCHEMACLASS: {
+
+    package DBIx::Class::DBI::Replicated::TestReplication;
+   
+    use DBICTest;
+    use base qw/Class::Accessor::Fast/;
+    
+    __PACKAGE__->mk_accessors( qw/schema/ );
+
+    ## Initialize the object
+    
+       sub new {
+           my $proto = shift;
+           my $class = ref( $proto ) || $proto;
+           my $self = {};
+       
+           bless( $self, $class );
+       
+           $self->schema( $self->init_schema );
+       
+           return $self;
+       }
+    
+    ## get the Schema and set the replication storage type
+    
+    sub init_schema {
+        my $class = shift @_;
+        my $schema = DBICTest->init_schema(storage_type=>'::DBI::Replicated');
+        return $schema;
+    }
+}
+
+## ----------------------------------------------------------------------------
+## Create an object and run some tests
+## ----------------------------------------------------------------------------
+
+my %params = (
+    db_paths => [
+        "t/var/DBIxClass.db",
+        "t/var/DBIxClass_slave1.db",
+        "t/var/DBIxClass_slave2.db",
+    ],
+);
+
+ok my $replicate = DBIx::Class::DBI::Replicated::TestReplication->new()
+    => 'Created a replication object';
+    
+isa_ok $replicate->schema
+    => 'DBIx::Class::Schema';
+    
+    
+    warn dump $replicate->schema->storage->meta;
+    
+    warn dump $replicate->schema->storage->master;
+
+
+__END__
+
+## ----------------------------------------------------------------------------
+## Build a class to hold all our required testing data and methods.
+## ----------------------------------------------------------------------------
+
+TESTSCHEMACLASS: {
        
-       package DBIx::Class::DBI::Replication::TestReplication;
+       package DBIx::Class::DBI::Replicated::TestReplication;
 
        use DBI;        
        use DBICTest;
@@ -57,7 +120,7 @@ TESTSCHEMACLASS: {
        sub init_schema {
                my $class = shift @_;
                my $schema = DBICTest->init_schema();
-               $schema->storage_type( '::DBI::Replication' );
+               $schema->storage_type( '::DBI::Replicated' );
                
                return $schema;
        }
@@ -67,15 +130,15 @@ TESTSCHEMACLASS: {
        sub connect {
                my $self = shift @_;
                my ($master, @slaves) = @{$self->{dsns}};
-               my @connections = ([$master, '','', {AutoCommit=>1, PrintError=>0}]);
-               my @slavesob;
+               my $master_connect_info = [$master, '','', {AutoCommit=>1, PrintError=>0}];
                
+               my @slavesob;
                foreach my $slave (@slaves)
                {
                        my $dbh = shift @{$self->{slaves}}
                         || DBI->connect($slave,"","",{PrintError=>0, PrintWarn=>0});
                        
-                       push @connections,
+                       push @{$master_connect_info->[-1]->{slaves_connect_info}},
                         [$dbh, '','',{priority=>10}];
                         
                        push @slavesob,
@@ -87,10 +150,7 @@ TESTSCHEMACLASS: {
                
                $self
                        ->{schema}
-                       ->connect([
-                               @connections,
-                               {limit_dialect => 'LimitXY'}
-                       ]);
+                       ->connect(@$master_connect_info);
        }
        
        ## replication
@@ -137,7 +197,7 @@ my %params = (
        ],
 );
 
-ok my $replicate = DBIx::Class::DBI::Replication::TestReplication->new(%params)
+ok my $replicate = DBIx::Class::DBI::Replicated::TestReplication->new(%params)
        => 'Created a replication object';
        
 isa_ok $replicate->{schema}
@@ -171,13 +231,6 @@ is $artist1->name, 'Ozric Tentacles'
 ## we overload any type of write operation so that is must hit the master
 ## database.
 
-use Fcntl qw (:flock);
-
-my $master_path = $replicate->{db_paths}->[0];
-open LOCKFILE, ">>$master_path"
- or die "Cannot open $master_path";
-flock(LOCKFILE, LOCK_EX);
-
 $replicate
        ->{schema}
        ->populate('Artist', [
@@ -255,6 +308,20 @@ eval {
 
 ok $@ => 'Got read errors after everything failed';
 
+## make sure ->connect_info returns something sane
+
+ok $replicate->{schema}->storage->connect_info
+    => 'got something out of ->connect_info';
+
+## Force a connection to the write source for testing.
+
+$replicate->{schema}->storage($replicate->{schema}->storage->write_source);
+
+## What happens when we do a find for something that doesn't exist?
+
+ok ! $replicate->{schema}->resultset('Artist')->find(666)
+    => 'Correctly did not find a bad artist id';
+
 ## Delete the old database files
 $replicate->cleanup;