From 1df5474a900866cca7c98d63496d59ddfa137649 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 28 May 2013 16:10:18 +0200 Subject: [PATCH] [SIGNED-OFF] Bug 10363: Adds unit tests for Koha::AuthorisedValue[s] packages Signed-off-by: Kyle M Hall Signed-off-by: Bernardo Gonzalez Kriegel --- t/db_dependent/AuthorisedValues.t | 154 +++++++++++++++++++++++++++++++++++++ 1 file changed, 154 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..2b5cc96 --- /dev/null +++ b/t/db_dependent/AuthorisedValues.t @@ -0,0 +1,154 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::More tests => 31; + +use Koha::AuthorisedValue; +use Koha::AuthorisedValues; + +use Data::Dumper; # FIXME REMOVEME + +# 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::AuthorisedValues->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->filter({category => 'av_for_testing_1'} ); +my $avs2 = $avs->filter({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' ); + +# select +$avs->select('value 5'); +my $selected = $avs->filter( { selected => 1 } ); +is( scalar(@$selected), 1, 'There is just 1 AV selected' ); +$selected = shift @$selected; +is( $selected->{selected}, 1, 'av is selected' ); +is( $selected->{authorised_value}, 'value 5', 'av selected is "value 5"'); + + +$av1->delete; +$av2->delete; +$av3->delete; +$av4->delete; +$av5->delete; -- 1.7.9.5