Bug 17722: Make PatronLists.t run inside a transaction
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 5 Dec 2016 14:19:12 +0000 (11:19 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 9 Dec 2016 17:50:14 +0000 (17:50 +0000)
This patch makes t/db_dependent/PatronLists.t run inside a transaction.
It also makes it generate its own data using t::lib::TestBuilder instead
of relying on sample patrons on the DB.

To test:
- Run:
  $ prove t/db_dependent/PatronLists.t
=> SUCCESS: Tests pass
- Apply the patch
- Run:
  $ prove t/db_dependent/PatronLists.t
=> SUCCESS: Tests pass
- Sign off :-D

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
t/db_dependent/PatronLists.t

index 7b8fcf2..a5bb41a 100755 (executable)
@@ -1,21 +1,42 @@
 #!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
 use Modern::Perl;
 
-use Test::More tests => 9;
+use Test::More tests => 7;
+use t::lib::TestBuilder;
 
-BEGIN {
-    use_ok('C4::Context');
-    use_ok('Koha::List::Patron');
-}
+use Koha::Database;
+use Koha::List::Patron
+    qw( AddPatronList AddPatronsToList DelPatronList DelPatronsFromList GetPatronLists ModPatronList );
+
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
+
+my $builder = t::lib::TestBuilder->new;
 
 C4::Context->_new_userenv('DUMMY SESSION');
 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, ', ');
 
-my $dbh = C4::Context->dbh;
-my $sth = $dbh->prepare("SELECT * FROM borrowers ORDER BY RAND() LIMIT 10");
-$sth->execute();
-my @borrowers = @{ $sth->fetchall_arrayref( {} ) };
+# Create 10 sample borrowers
+my @borrowers = ();
+foreach (1..10) {
+    push @borrowers, $builder->build({ source => 'Borrower' });
+}
 
 my $owner = $borrowers[0]->{borrowernumber};
 
@@ -78,3 +99,7 @@ DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } )
 @lists =
   GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } );
 ok( !@lists, 'DelPatronList works' );
+
+$schema->storage->txn_rollback;
+
+1;