Koha/t/db_dependent/XISBN.t
Tomas Cohen Arazi 8193c40b9d Bug 12782: (followup) skip tests that fail because of provider's error
It seems that XISBN is failing sometimes, if you run the tests a couple
of times in a row. An error 500 is raised by lwp.

This problem should be trapped and related tests skipped. Because too much
noise could make people pay no attention to this tests failing.

To reproduce:
- Run the tests several consecutive times:
  $ prove -v t/db_dependent/XISBN.t
=> FAIL: XISBN test will fail eventually, printing a networking-related warning.
- Apply the patch
- Repeat the test
=> SUCCESS: when XISBN the networking/connection refused problem arises, the test
  is skipped, and it still passes.

Regards
To+

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-08-20 12:29:27 -03:00

137 lines
3.7 KiB
Perl
Executable file

#!/usr/bin/perl
#
# This Koha test module is a stub!
# Add more tests here!!!
use Modern::Perl;
use Test::More tests => 5;
use MARC::Record;
use C4::Biblio;
use C4::XISBN;
use C4::Context;
use C4::Search;
use Test::MockModule;
BEGIN {
use_ok('C4::XISBN');
}
my $dbh = C4::Context->dbh;
$dbh->{RaiseError} = 1;
$dbh->{AutoCommit} = 0;
my $search_module = new Test::MockModule('C4::Search');
$search_module->mock('SimpleSearch', \&Mock_SimpleSearch );
my $context = C4::Context->new;
my ( $biblionumber_tag, $biblionumber_subfield ) =
GetMarcFromKohaField( 'biblio.biblionumber', '' );
my ( $isbn_tag, $isbn_subfield ) =
GetMarcFromKohaField( 'biblioitems.isbn', '' );
# Harry Potter and the Sorcerer's Stone, 1st American ed. 1997
my $isbn1 = '0590353403';
# ThingISBN match : Silent Wing, First Edition 1998
my $isbn2 = '0684843897';
# XISBN match : Harry Potter and the Sorcerer's Stone,
# 1. Scholastic mass market paperback printing1.
my $isbn3 = '043936213X';
my $biblionumber1 = _add_biblio_with_isbn($isbn1);
my $biblionumber2 = _add_biblio_with_isbn($isbn2);
my $biblionumber3 = _add_biblio_with_isbn($isbn3);
my $trial = C4::XISBN::get_biblionumber_from_isbn($isbn1);
is( $trial->[0]->{biblionumber},
$biblionumber1,
"It gets the correct biblionumber from the only isbn we have added." );
$trial = C4::XISBN::_get_biblio_from_xisbn($isbn1);
is( $trial->{biblionumber},
$biblionumber1, "Gets biblionumber like the previous test." );
## Test ThingISBN
$context->set_preference( 'ThingISBN', 1 );
$context->set_preference( 'XISBN', 0 );
my $results_thingisbn;
eval { $results_thingisbn = C4::XISBN::get_xisbns($isbn1); };
SKIP: {
skip "Problem retrieving ThingISBN", 1
unless $@ eq '';
is( $results_thingisbn->[0]->{biblionumber},
$biblionumber3,
"Gets correct biblionumber from a book with a similar isbn using ThingISBN." );
}
## Test XISBN
$context->set_preference( 'ThingISBN', 0 );
$context->set_preference( 'XISBN', 1 );
my $results_xisbn;
eval { $results_xisbn = C4::XISBN::get_xisbns($isbn1); };
SKIP: {
skip "Problem retrieving XISBN", 1
unless $@ eq '';
is( $results_xisbn->[0]->{biblionumber},
$biblionumber3,
"Gets correct biblionumber from a book with a similar isbn using XISBN." );
}
$dbh->rollback;
# Util subs
# Add new biblio with isbn and return biblionumber
sub _add_biblio_with_isbn {
my $isbn = shift;
my $marc_record = MARC::Record->new;
my $field = MARC::Field->new( $isbn_tag, '', '', $isbn_subfield => $isbn );
$marc_record->append_fields($field);
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marc_record, '' );
return $biblionumber;
}
# Mocked subs
# C4::Search::SimpleSearch
sub Mock_SimpleSearch {
my $query = shift;
my @results;
$query =~ s/-//g;
my $ret_biblionumber;
if ( $query =~ /$isbn1/ ) {
$ret_biblionumber = $biblionumber1;
}
elsif ( $query =~ /$isbn2/ ) {
$ret_biblionumber = $biblionumber2;
}
elsif ( $query =~ /$isbn3/ ) {
$ret_biblionumber = $biblionumber3;
}
my $record = MARC::Record->new;
$record->leader(' ngm a22 7a 4500');
my $biblionumber_field;
if ( $biblionumber_tag < 10 ) {
$biblionumber_field =
MARC::Field->new( $biblionumber_tag, $ret_biblionumber );
}
else {
$biblionumber_field = MARC::Field->new( $biblionumber_tag, '', '',
$biblionumber_subfield => $ret_biblionumber );
}
$record->append_fields($biblionumber_field);
my $indexing_mode = C4::Context->config('zebra_bib_index_mode') // 'dom';
push @results, ( $indexing_mode eq 'dom' )
? $record->as_xml()
: $record->as_usmarc() ;
return ( undef, \@results, 1 );
}