From e62468de2ed75fa33b2428d31c2a883c9176431e Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 30 Jan 2024 13:53:05 +0000 Subject: [PATCH] Bug 28869: Unit tests Test plan: Run t/db_dependent/AuthorisedValues.t Signed-off-by: Marcel de Rooy Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- t/db_dependent/AuthorisedValues.t | 50 ++++++++++++++++++++++++++++++- t/lib/TestBuilder.pm | 3 ++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index 368e9e9186..02fcbc5ac8 100755 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -1,7 +1,8 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 16; +use Test::More tests => 17; +use Test::Exception; use Try::Tiny; use t::lib::TestBuilder; @@ -11,6 +12,7 @@ use C4::Context; use Koha::AuthorisedValue; use Koha::AuthorisedValues; use Koha::AuthorisedValueCategories; +use Koha::Exceptions; use Koha::MarcSubfieldStructures; my $schema = Koha::Database->new->schema; @@ -332,4 +334,50 @@ subtest 'search_by_*_field + find_by_koha_field + get_description + authorised_v }; }; +subtest 'is_integer_only' => sub { + plan tests => 13; + $schema->storage->txn_begin; + + my $avcat1 = $builder->build_object( { class => 'Koha::AuthorisedValueCategories' } ); + my $avcat2 = $builder->build_object( { class => 'Koha::AuthorisedValueCategories' } ); + $avcat2->is_integer_only(1)->store; + is( $avcat1->is_integer_only, 0, 'No numeric requirement expected' ); + is( $avcat2->is_integer_only, 1, 'Numeric requirement expected' ); + + my $avval1 = $builder->build_object( + { + class => 'Koha::AuthorisedValues', + value => { category => $avcat1->category_name, authorised_value => 'abc' } + } + ); + my $avval2 = $builder->build_object( + { + class => 'Koha::AuthorisedValues', + value => { category => $avcat2->category_name, authorised_value => '123' } + } + ); + + # Test helper method on child (authval) + is( $avval1->is_integer_only, 0, 'No numeric requirement expected' ); + is( $avval2->is_integer_only, 1, 'Numeric requirement expected' ); + + lives_ok { $avval2->authorised_value(-1)->store } 'No exception expected'; + lives_ok { $avval2->authorised_value(0)->store } 'No exception expected'; + lives_ok { $avval2->authorised_value(22)->store } 'No exception expected'; + + # Test ->store with bad data + throws_ok { $avval2->authorised_value(undef)->store } 'Koha::Exceptions::NoInteger', + 'Exception expected (undefined)'; + throws_ok { $avval2->authorised_value('')->store } 'Koha::Exceptions::NoInteger', 'Exception expected (empty)'; + throws_ok { $avval2->authorised_value('abc')->store } 'Koha::Exceptions::NoInteger', 'Exception expected for abc'; + throws_ok { $avval2->authorised_value('+12')->store } 'Koha::Exceptions::NoInteger', + 'Exception expected for + sign'; + throws_ok { $avval2->authorised_value(' 12')->store } 'Koha::Exceptions::NoInteger', + 'Exception expected for preceding space'; + throws_ok { $avval2->authorised_value('12 ')->store } 'Koha::Exceptions::NoInteger', + 'Exception expected for trailing space'; + + $schema->storage->txn_rollback; +}; + $schema->storage->txn_rollback; diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 97d34c8d96..630fab0cfb 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -575,6 +575,9 @@ sub _gen_blob { sub _gen_default_values { my ($self) = @_; return { + AuthorisedValueCategory => { + is_integer_only => 0, + }, BackgroundJob => { context => '{}' }, -- 2.39.5