From 950b068a533cc24848ee4e5a9f43e26956d6a5f5 Mon Sep 17 00:00:00 2001 From: Yohann Dufour Date: Thu, 7 Aug 2014 16:39:50 +0200 Subject: [PATCH] Bug 10363: Adds unit tests for Koha::AuthorisedValue package --- t/db_dependent/AuthorisedValues.t | 147 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 t/db_dependent/AuthorisedValues.t diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t new file mode 100644 index 0000000..76dab8b --- /dev/null +++ b/t/db_dependent/AuthorisedValues.t @@ -0,0 +1,147 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 29; + +use C4::Context; +use Koha::AuthorisedValue; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$dbh->do("DELETE FROM authorised_values"); + +# Tests for Koha::AuthorisedValue + +# insert +my $av1 = Koha::AuthorisedValue->new({ + category => 'av_for_testing', + authorised_value => 'value 1', + lib => 'display value 1', + lib_opac => 'opac display value 1', + imageurl => 'image1.png', +}); + +eval {$av1->insert}; +ok( ! $@, 'AV 1 is inserted without error'); + +my $av1_bis = Koha::AuthorisedValue->new({ + category => 'av_for_testing', + authorised_value => 'value 1', + lib => 'display value 2', + lib_opac => 'opac display value 2', + imageurl => 'image2.png', +}); + +eval {$av1_bis->insert}; +ok( $@, 'AV 2 is not inserted (error raised) : Unique key category-authorised_value'); + +my $id = $av1->{id}; +ok( $av1->{id}, 'AV 1 is inserted' ); + +my $new_av1 = Koha::AuthorisedValue->new( { id => $id } )->fetch; + +is( $new_av1->{id}, $id, "insert: id has been correctly get back" ); +is( $new_av1->{authorised_value}, $av1->{authorised_value}, "insert: authorised_value has been correctly get back" ); +is( $new_av1->{lib}, $av1->{lib}, "insert: lib has been correctly get back" ); +is( $new_av1->{lib_opac}, $av1->{lib_opac}, "insert: lib_opac has been correctly get back" ); +is( $new_av1->{imageurl}, $av1->{imageurl}, "insert: imageurl has been correctly get back" ); +is( $new_av1->{branches_limitations}, undef, "insert: branches_limitations is not get back with fetch" ); +is_deeply( $new_av1->branches_limitations, [], "insert: The branches_limitations method returns the branches limitations" ); +is_deeply( $new_av1->{branches_limitations}, [], "insert: The branches_limitations method set the branches_limitations key" ); + +# update +$av1->{authorised_value} = 'new value 1'; +$av1->{lib} = 'new lib 1'; +$av1->{lib_opac} = 'new lib opac 1'; +$av1->{imageurl} = 'new_image2.png'; +my $branches_limitations = [ + {branchcode => 'CPL', branchname => 'Centerville'}, + {branchcode => 'MPL', branchname => 'Midway'}, +]; +$av1->{branches_limitations} = $branches_limitations; +eval{$av1->update}; +ok( ! $@, 'AV 1 is updated without error'); + +$new_av1 = Koha::AuthorisedValue->new( { id => $id } )->fetch; +is( $new_av1->{id}, $id, "update: id has been correctly get back" ); +is( $new_av1->{authorised_value}, 'new value 1', "update: authorised_value has been correctly get back" ); +is( $new_av1->{lib}, 'new lib 1', "update: lib has been correctly get back" ); +is( $new_av1->{lib_opac}, 'new lib opac 1', "update: lib_opac has been correctly get back" ); +is( $new_av1->{imageurl}, 'new_image2.png', "update: imageurl has been correctly get back" ); +is( $new_av1->{branches_limitations}, undef, "update: branches_limitations is not get back with fetch" ); +is_deeply( $new_av1->branches_limitations, $branches_limitations, "update: The branches_limitations method returns the branches limitations" ); +is_deeply( $new_av1->{branches_limitations}, $branches_limitations, "update: The branches_limitations method set the branches_limitations key" ); + +# delete +eval{$av1->delete}; +ok( ! $@, 'AV 1 is deleted without error' ); + +eval{ + $new_av1 = Koha::AuthorisedValue->new( { id => $id } )->fetch; +}; +ok( $@, 'AV 1 is removed from the database' ); + + +# Tests for Koha::AuthorisedValues +$av1 = Koha::AuthorisedValue->new({ + category => 'av_for_testing_1', + authorised_value => 'value 1', + lib => 'display value 1', + lib_opac => 'opac display value 1', + imageurl => 'image1.png', +}); +my $av2 = Koha::AuthorisedValue->new({ + category => 'av_for_testing_1', + authorised_value => 'value 2', + lib => 'display value 2', + lib_opac => 'opac display value 2', + imageurl => 'image2.png', +}); +my $av3 = Koha::AuthorisedValue->new({ + category => 'av_for_testing_1', + authorised_value => 'value 3', + lib => 'display value 3', + lib_opac => 'opac display value 3', + imageurl => 'image3.png', +}); +my $av4 = Koha::AuthorisedValue->new({ + category => 'av_for_testing_2', + authorised_value => 'value 4', + lib => 'display value 4', + lib_opac => 'opac display value 4', + imageurl => 'image4.png', +}); +my $av5 = Koha::AuthorisedValue->new({ + category => 'av_for_testing_2', + authorised_value => 'value 5', + lib => 'display value 5', + lib_opac => 'opac display value 5', + imageurl => 'image5.png', +}); +eval { + $av1->insert; + $av2->insert; + $av3->insert; + $av4->insert; + $av5->insert; +}; +ok( ! $@, "5 AV inserted with success" ); +my $avs = Koha::AuthorisedValue->new; + +# get categories +my $categories = $avs->categories; +is( grep ({/av_for_testing_1/} @$categories), 1, "av_for_testing_1 is a category"); +is( grep ({/av_for_testing_2/} @$categories), 1, "av_for_testing_2 is a category"); +is( grep ({/av_for_testing_3/} @$categories), 0, "av_for_testing_3 is not a category"); + +# filter +my $avs1 = $avs->search({category => 'av_for_testing_1'} ); +my $avs2 = $avs->search({category => 'av_for_testing_2'} ); +is( scalar(@$avs1), 3, 'There are 3 AVs for category 1' ); +is( scalar(@$avs2), 2, 'There are 2 AVs for category 2' ); + +is( $av1->delete, 1, "Delete 1 AV" ); + +$dbh->rollback; -- 1.9.1