Bug 17932: (followup) Tidy tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 24 Jan 2017 13:48:31 +0000 (10:48 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 17 Feb 2017 15:33:01 +0000 (15:33 +0000)
This patch makes the tests create its own data instead of searching the
DB for a branchcode and a categorycode. It does so on each subtest,
because there shouldn't be side effects between subtests.

I also wrapped each subtest inside a transaction, for the same reason.

To test:
- Apply this patch
- Run:
  $ prove t/db_dependent/Koha/Object.t
=> SUCCESS: Tests return green with this patch
- Sign off :-D

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
t/db_dependent/Koha/Object.t

index fd5d89b..d812d37 100755 (executable)
@@ -33,16 +33,17 @@ BEGIN {
     use_ok('Koha::Patron');
 }
 
-my $schema = Koha::Database->new->schema;
-$schema->storage->txn_begin;
-
+my $schema  = Koha::Database->new->schema;
 my $builder = t::lib::TestBuilder->new();
 
-my $categorycode = $schema->resultset('Category')->first()->categorycode();
-my $branchcode = $schema->resultset('Branch')->first()->branchcode();
-
 subtest 'is_changed' => sub {
     plan tests => 6;
+
+    $schema->storage->txn_begin;
+
+    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
+
     my $object = Koha::Patron->new();
     $object->categorycode( $categorycode );
     $object->branchcode( $branchcode );
@@ -61,10 +62,18 @@ subtest 'is_changed' => sub {
     is( $object->is_changed(), 1, "Object is changed after Set" );
     $object->store();
     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'in_storage' => sub {
     plan tests => 6;
+
+    $schema->storage->txn_begin;
+
+    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
+
     my $object = Koha::Patron->new();
     is( $object->in_storage, 0, "Object is not in storage" );
     $object->categorycode( $categorycode );
@@ -83,23 +92,43 @@ subtest 'in_storage' => sub {
     $patron = $schema->resultset('Borrower')->find( $borrowernumber );
     ok( ! $patron, "Object no longer found in database" );
     is( $object->in_storage, 0, "Object is not in storage" );
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'id' => sub {
     plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
+
     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
     is( $patron->id, $patron->borrowernumber );
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'get_column' => sub {
     plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
+
     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
     is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'discard_changes' => sub {
     plan tests => 1;
-    my $builder = t::lib::TestBuilder->new;
+
+    $schema->storage->txn_begin;
+
     my $patron = $builder->build( { source => 'Borrower' } );
     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
     $patron->dateexpiry(dt_from_string);
@@ -109,12 +138,16 @@ subtest 'discard_changes' => sub {
         dt_from_string->truncate( to => 'day' ),
         'discard_changes should refresh the object'
     );
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'TO_JSON tests' => sub {
 
     plan tests => 5;
 
+    $schema->storage->txn_begin;
+
     my $borrowernumber = $builder->build(
         { source => 'Borrower',
           value => { lost => 1,
@@ -131,6 +164,8 @@ subtest 'TO_JSON tests' => sub {
     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
 
     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
+
+    $schema->storage->txn_rollback;
 };