Some stylistic test changes in preparation for next commits
Peter Rabbitson [Tue, 17 Mar 2015 15:09:44 +0000 (16:09 +0100)]
There are zero functional changes - everything has to do with making sure
we are not blindly poking into storage objects which may be changed to
delegates AND to make sure we do not leave any open cursors, which would show
up as deadlocks on multiple connections to the same SQLite file

Read under -w

15 files changed:
t/52leaks.t
t/67pager.t
t/76select.t
t/88result_set_column.t
t/icdt/engine_specific/sqlite.t
t/inflate/serialize.t
t/lib/DBICTest.pm
t/lib/DBICTest/BaseSchema.pm
t/multi_create/multilev_single_PKeqFK.t
t/prefetch/standard.t
t/resultset/update_delete.t
t/storage/dbh_do.t
t/storage/reconnect.t
t/storage/savepoints.t
t/storage/txn.t

index dad4f36..a4673b1 100644 (file)
@@ -116,8 +116,6 @@ unless (DBICTest::RunMode->is_plain) {
   my $rs = $schema->resultset ('Artist');
   my $storage = $schema->storage;
 
-  ok ($storage->connected, 'we are connected');
-
   my $row_obj = $rs->search({}, { rows => 1})->next;  # so that commits/rollbacks work
   ok ($row_obj, 'row from db');
 
index 2463369..fa7c93a 100644 (file)
@@ -69,12 +69,14 @@ $it = $rs->search(
   { order_by => 'title',
     rows => 3 }
 );
-my $page = $it->page(2);
 
-is( $page->count, 2, "standard resultset paged rs count ok" );
+{
+  my $page = $it->page(2);
 
-is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
+  is( $page->count, 2, "standard resultset paged rs count ok" );
 
+  is( $page->next->title, "Generic Manufactured Singles", "second page of standard resultset ok" );
+}
 
 # test software-based limit paging
 $it = $rs->search(
index 81ecf0d..9d09380 100644 (file)
@@ -60,6 +60,7 @@ my $subsel = $cds->search ({}, {
 is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
 is ($subsel->next->title, $cds->next->title, 'First CD title match');
 is ($subsel->next->title, $cds->next->title, 'Second CD title match');
+$cds->reset;
 
 is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');
 
index 21e1c9c..e1b73a3 100644 (file)
@@ -46,14 +46,16 @@ is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
 
 cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
 
-my $rso_year = $rs->search({}, { order_by => 'cdid' })->get_column('year');
-is($rso_year->next, 1999, "reset okay");
+{
+  my $rso_year = $rs->search({}, { order_by => 'cdid' })->get_column('year');
+  is($rso_year->next, 1999, "reset okay");
 
-is($rso_year->first, 1999, "first okay");
+  is($rso_year->first, 1999, "first okay");
 
-warnings_exist (sub {
-  is($rso_year->single, 1999, "single okay");
-}, qr/Query returned more than one row/, 'single warned');
+  warnings_exist (sub {
+    is($rso_year->single, 1999, "single okay");
+  }, qr/Query returned more than one row/, 'single warned');
+}
 
 
 # test distinct propagation
index 29a00c0..8a152ab 100644 (file)
@@ -15,19 +15,21 @@ use DBICTest;
     no_deploy => 1, # Deploying would cause an early rebless
   );
 
+  my $storage = $schema->storage;
+
   is(
-    ref $schema->storage, 'DBIx::Class::Storage::DBI',
+    ref $storage, 'DBIx::Class::Storage::DBI',
     'Starting with generic storage'
   );
 
   # Calling date_time_parser should cause the storage to be reblessed,
   # so that we can pick up datetime_parser_type from subclasses
-  my $parser = $schema->storage->datetime_parser();
+  my $parser = $storage->datetime_parser();
 
   is($parser, 'DateTime::Format::SQLite', 'Got expected storage-set datetime_parser');
-  isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::SQLite', 'storage');
+  isa_ok($storage, 'DBIx::Class::Storage::DBI::SQLite', 'storage');
 
-  ok(! $schema->storage->connected, 'Not yet connected');
+  ok(! $storage->connected, 'Not yet connected');
 }
 
 # so user's env doesn't screw us
index a9bf5d7..63c31aa 100644 (file)
@@ -95,7 +95,7 @@ is_deeply (
 );
 
 #===== make sure make_column_dirty interacts reasonably with inflation
-$object = $rs->first;
+$object = $rs->search({}, { rows => 1 })->next;
 $object->update ({serialized => { x => 'y'}});
 
 $object->serialized->{x} = 'z'; # change state without notifying $object
index fa92a95..f2b2773 100644 (file)
@@ -320,10 +320,12 @@ sub init_schema {
 
     my $schema;
 
+    my @dsn = $self->_database(%args);
+
     if ($args{compose_connection}) {
       $need_global_cleanup = 1;
       $schema = DBICTest::Schema->compose_connection(
-                  'DBICTest', $self->_database(%args)
+                  'DBICTest', @dsn
                 );
     } else {
       $schema = DBICTest::Schema->compose_namespace('DBICTest');
@@ -334,7 +336,7 @@ sub init_schema {
     }
 
     if ( !$args{no_connect} ) {
-      $schema = $schema->connect($self->_database(%args));
+      $schema->connection(@dsn);
     }
 
     if ( !$args{no_deploy} ) {
index f51328c..12cb5b8 100644 (file)
@@ -7,6 +7,7 @@ use base qw(DBICTest::Base DBIx::Class::Schema);
 
 use Fcntl qw(:DEFAULT :seek :flock);
 use Time::HiRes 'sleep';
+use Scope::Guard ();
 use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
 use DBICTest::Util 'local_umask';
 use namespace::clean;
@@ -23,10 +24,20 @@ sub capture_executed_sql_bind {
   local *DBIx::Class::Storage::DBI::_format_for_trace = sub { $_[1] };
   Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
 
+  # can not use local() due to an unknown number of storages
+  # (think replicated)
+  my $orig_states = { map
+    { $_ => $self->storage->$_ }
+    qw(debugcb debugobj debug)
+  };
+
+  my $sg = Scope::Guard->new(sub {
+    $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
+  });
 
-  local $self->storage->{debugcb};
-  local $self->storage->{debugobj} = my $tracer_obj = DBICTest::SQLTracerObj->new;
-  local $self->storage->{debug} = 1;
+  $self->storage->debugcb(undef);
+  $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
+  $self->storage->debug(1);
 
   local $Test::Builder::Level = $Test::Builder::Level + 2;
   $cref->();
index ef5c9a8..9a5adbe 100644 (file)
@@ -59,7 +59,7 @@ for my $type (qw/has_one might_have/) {
   my $cd_title = "Test $type cd";
   my $artist_names = [ map { "Artist via $type $_" } (1, 2) ];
 
-  my $someartist = $artist_rs->next;
+  my $someartist = $artist_rs->search({}, { rows => 1 })->next;
 
   lives_ok (sub {
     my $cd = $schema->resultset('CD')->create ({
index 69574ab..75107c7 100644 (file)
@@ -144,7 +144,7 @@ cmp_ok($rs->count, '==', 1, "single artist returned from multi-join");
 
 is($rs->next->name, 'Caterwauler McCrae', "Correct artist returned");
 
-$cd = $schema->resultset('Artist')->first->create_related('cds',
+$cd = $schema->resultset('Artist')->search({ artistid => 1 })->first->create_related('cds',
     {
     title   => 'Unproduced Single',
     year    => 2007
index 7d9efa9..d13848c 100644 (file)
@@ -140,7 +140,13 @@ $schema->is_executed_sql_bind( sub {
 
 # try the same sql with forced multicolumn in
 $schema->is_executed_sql_bind( sub {
-  local $schema->storage->{_use_multicolumn_in} = 1;
+
+  my $orig_umi = $schema->storage->_use_multicolumn_in;
+  my $sg = Scope::Guard->new(sub {
+    $schema->storage->_use_multicolumn_in($orig_umi);
+  });
+
+  $schema->storage->_use_multicolumn_in(1);
 
   # this can't actually execute on sqlite
   eval { $fks_multi->update ({ read_count => \ 'read_count + 1' }) };
index 0beb858..952fa1c 100644 (file)
@@ -43,7 +43,7 @@ is_deeply (
 
 # test nested aliasing
 my $res = 'original';
-$storage->dbh_do (sub {
+$schema->storage->dbh_do (sub {
   shift->dbh_do(sub { $_[3] = 'changed' }, @_)
 }, $res);
 
index 557bff8..75681d3 100644 (file)
@@ -49,7 +49,7 @@ close $db_file;
   # Catch the DBI connection error
   local $SIG{__WARN__} = sub {};
   throws_ok {
-    my @art_three = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
+    $schema->resultset("Artist")->create({ name => 'not gonna happen' });
   }  qr/not a database/, 'The operation failed';
 }
 
index 0c56afc..5866e6d 100644 (file)
@@ -69,8 +69,17 @@ for ('', keys %$env2optdep) { SKIP: {
 
   note "Testing $prefix";
 
-  local $schema->storage->{debugobj} = my $stats = DBICTest::SVPTracerObj->new;
-  local $schema->storage->{debug} = 1;
+  # can not use local() due to an unknown number of storages
+  # (think replicated)
+  my $orig_states = { map
+    { $_ => $schema->storage->$_ }
+    qw(debugcb debugobj debug)
+  };
+  my $sg = Scope::Guard->new(sub {
+    $schema->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
+  });
+  $schema->storage->debugobj (my $stats = DBICTest::SVPTracerObj->new);
+  $schema->storage->debug (1);
 
   $schema->resultset('Artist')->create({ name => 'foo' });
 
@@ -229,12 +238,12 @@ for ('', keys %$env2optdep) { SKIP: {
     'rollback from inner transaction';
 
 ### cleanupz
-  $schema->storage->dbh->do ("DROP TABLE artist");
+  $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE artist") });
 }}
 
 done_testing;
 
 END {
-  eval { $schema->storage->dbh->do ("DROP TABLE artist") } if defined $schema;
+  eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE artist") }) } if defined $schema;
   undef $schema;
 }
index 06af849..4c6d505 100644 (file)
@@ -36,7 +36,7 @@ my $code = sub {
 for my $want (0,1) {
   my $schema = DBICTest->init_schema;
 
-  is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
+  is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
 
   my @titles = map {'txn_do test CD ' . $_} (1..5);
   my $artist = $schema->resultset('Artist')->find(1);
@@ -56,7 +56,7 @@ for my $want (0,1) {
     title => "txn_do test CD $_",
   })->first->year, 2006, "new CD $_ year correct") for (1..5);
 
-  is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
+  is( $schema->storage->transaction_depth, 0, 'txn depth has been reset');
 }
 
 # Test txn_do() @_ aliasing support
@@ -72,7 +72,7 @@ for my $want (0,1) {
 {
   my $schema = DBICTest->init_schema;
 
-  is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
+  is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
 
   my $nested_code = sub {
     my ($schema, $artist, $code) = @_;
@@ -96,7 +96,7 @@ for my $want (0,1) {
   })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
   is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
 
-  is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
+  is( $schema->storage->transaction_depth, 0, 'txn depth has been reset');
 }
 
 # test nested txn_begin on fresh connection
@@ -260,7 +260,7 @@ my $fail_code = sub {
   # Test failed txn_do()
   for my $pass (1,2) {
 
-    is( $schema->storage->{transaction_depth}, 0, "txn depth starts at 0 (pass $pass)");
+    is( $schema->storage->transaction_depth, 0, "txn depth starts at 0 (pass $pass)");
 
     my $artist = $schema->resultset('Artist')->find(3);
 
@@ -274,13 +274,13 @@ my $fail_code = sub {
     })->first;
     ok(!defined($cd), qq{failed txn_do didn't change the cds table (pass $pass)});
 
-    is( $schema->storage->{transaction_depth}, 0, "txn depth has been reset (pass $pass)");
+    is( $schema->storage->transaction_depth, 0, "txn depth has been reset (pass $pass)");
   }
 
 
   # Test failed txn_do() with failed rollback
   {
-    is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
+    is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
 
     my $artist = $schema->resultset('Artist')->find(3);
 
@@ -322,7 +322,7 @@ my $fail_code = sub {
 {
   my $schema = DBICTest->init_schema();
 
-  is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
+  is( $schema->storage->transaction_depth, 0, 'txn depth starts at 0');
 
   my $nested_fail_code = sub {
     my ($schema, $artist, $code1, $code2) = @_;