From 481a671d6924393fff4b9a8bf234ad3461889d19 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 7 Sep 2017 11:50:42 -0400 Subject: [PATCH] Bug 13937: Add unit tests --- t/Koha/Z3950responder.t | 31 +++++++ t/db_dependent/Koha/Z3950Responder/Session.t | 70 +++++++++++++++ t/db_dependent/Koha/Z3950Responder/Session2.t | 86 +++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 t/Koha/Z3950responder.t create mode 100644 t/db_dependent/Koha/Z3950Responder/Session.t create mode 100644 t/db_dependent/Koha/Z3950Responder/Session2.t diff --git a/t/Koha/Z3950responder.t b/t/Koha/Z3950responder.t new file mode 100644 index 0000000000..3d74e5df8f --- /dev/null +++ b/t/Koha/Z3950responder.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 8; + +BEGIN { + use_ok('Koha::Z3950Responder'); +} + +my $zR = Koha::Z3950Responder->new({}); + +my $args={ PEER_NAME => 'PEER'}; +$zR->init_handler($args); +is ( $args->{IMP_NAME}, 'Koha',"Server returns basic info"); +$args->{DATABASES} = ['biblios']; +$args->{QUERY} = 'biblios'; +$args->{SETNAME} = 'biblios'; +$args->{START} = 0; +$args->{OFFSET} = 0; +$args->{NUMBER} = 42; +$zR->search_handler( $args ); +is ( $args->{ERR_CODE}, 2, "We didn't start server , should fail"); +is ( $args->{ERR_STR}, 'Cannot connect to upstream server', "We didn't start server, should fail because it cannot connect"); +$zR->present_handler( $args ); +is ( $args->{ERR_CODE}, 30, "There is no handler as we aren't connected"); +is ( $args->{ERR_STR}, 'No such resultset', "We don't have a handler, should fail because we don't"); +my $arg_check = ( $args ); +$zR->fetch_handler( $args ); +is_deeply( $args, $arg_check, "nothing should change"); +$zR->close_handler( $args ); +is_deeply( $args, $arg_check, "nothing should change"); diff --git a/t/db_dependent/Koha/Z3950Responder/Session.t b/t/db_dependent/Koha/Z3950Responder/Session.t new file mode 100644 index 0000000000..59930810e9 --- /dev/null +++ b/t/db_dependent/Koha/Z3950Responder/Session.t @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 3; +use t::lib::TestBuilder; +use C4::Items; + +BEGIN { + use_ok('Koha::Z3950Responder'); + use_ok('Koha::Z3950Responder::Session'); +} + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; + +$schema->storage->txn_begin; + +subtest 'add_item_status' => sub { + + plan tests => 2; + + # Make sure we are using default values set in code + $builder->schema->resultset( 'AuthorisedValue' )->delete_all; + + ## FIRST ITEM HAS ALL THE STATUSES ## + my $item_1 = $builder->build({ + source => 'Item', + value => { + onloan => '2017-07-07', + itemlost => 1, + notforloan => 1, + damaged => 1, + withdrawn => 1, + } + }); + my $item_marc_1 = C4::Items::GetMarcItem( $item_1->{biblionumber}, $item_1->{itemnumber} ); + my $item_field_1 = scalar $item_marc_1->field('952'); + $builder->build({ source => 'Reserve', value=> { itemnumber => $item_1->{itemnumber} } }); + $builder->build({ source => 'Branchtransfer', value=> { itemnumber => $item_1->{itemnumber}, datearrived => undef } }); + ## END FIRST ITEM ## + + ## SECOND ITEM HAS NO STATUSES ## + my $item_2 = $builder->build({ + source => 'Item', + value => { + onloan => undef, + itemlost => 0, + notforloan => 0, + damaged => 0, + withdrawn => 0, + } + }); + my $item_marc_2 = C4::Items::GetMarcItem( $item_2->{biblionumber}, $item_2->{itemnumber} ); + my $item_field_2 = scalar $item_marc_2->field('952'); + ## END SECOND ITEM ## + + # Create the responder + my $args={ PEER_NAME => 'PEER'}; + my $zR = Koha::Z3950Responder->new({add_item_status_subfield => 'k'}); + $zR->init_handler($args); + + $args->{HANDLE}->add_item_status($item_field_1); + is($item_field_1->subfield('k'),"Checked Out, Lost, Not for Loan, Damaged, Withdrawn, In Transit, On Hold","All statuses added in one field as expected"); + + $args->{HANDLE}->add_item_status($item_field_2); + is($item_field_2->subfield('k'),'Available',"Available status added as expected"); + +}; + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Z3950Responder/Session2.t b/t/db_dependent/Koha/Z3950Responder/Session2.t new file mode 100644 index 0000000000..073889b050 --- /dev/null +++ b/t/db_dependent/Koha/Z3950Responder/Session2.t @@ -0,0 +1,86 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 3; +use t::lib::TestBuilder; +use C4::Items; + +BEGIN { + use_ok('Koha::Z3950Responder'); + use_ok('Koha::Z3950Responder::Session'); +} + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; + +$schema->storage->txn_begin; + +subtest 'add_item_status' => sub { + + plan tests => 2; + + # This time we are sustituting some values + $builder->schema->resultset( 'AuthorisedValue' )->delete_all; + $builder->build({ + source => 'AuthorisedValue', + value => { + category => 'Z3950_STATUS', + authorised_value => 'AVAILABLE', + lib => "Free as a bird" + } + }); + $builder->build({ + source => 'AuthorisedValue', + value => { + category => 'Z3950_STATUS', + authorised_value => 'DAMAGED', + lib => "Borked completely" + } + }); + + ## FIRST ITEM HAS ALL THE STATUSES ## + my $item_1 = $builder->build({ + source => 'Item', + value => { + onloan => '2017-07-07', + itemlost => 1, + notforloan => 1, + damaged => 1, + withdrawn => 1, + } + }); + my $item_marc_1 = C4::Items::GetMarcItem( $item_1->{biblionumber}, $item_1->{itemnumber} ); + my $item_field_1 = scalar $item_marc_1->field('952'); + $builder->build({ source => 'Reserve', value=> { itemnumber => $item_1->{itemnumber} } }); + $builder->build({ source => 'Branchtransfer', value=> { itemnumber => $item_1->{itemnumber}, datearrived => undef } }); + ## END FIRST ITEM ## + + ## SECOND ITEM HAS NO STATUSES ## + my $item_2 = $builder->build({ + source => 'Item', + value => { + onloan => undef, + itemlost => 0, + notforloan => 0, + damaged => 0, + withdrawn => 0, + } + }); + my $item_marc_2 = C4::Items::GetMarcItem( $item_2->{biblionumber}, $item_2->{itemnumber} ); + my $item_field_2 = scalar $item_marc_2->field('952'); + ## END SECOND ITEM ## + + # Create the responder + my $args={ PEER_NAME => 'PEER'}; + my $zR = Koha::Z3950Responder->new({add_item_status_subfield => 'k'}); + $zR->init_handler($args); + + $args->{HANDLE}->add_item_status($item_field_1); + is($item_field_1->subfield('k'),"Checked Out, Lost, Not for Loan, Borked completely, Withdrawn, In Transit, On Hold","All statuses added in one field as expected"); + + $args->{HANDLE}->add_item_status($item_field_2); + is($item_field_2->subfield('k'),'Free as a bird',"Available status is 'Free as a bird' added as expected"); + +}; + +$schema->storage->txn_rollback; -- 2.17.1