From db4f434a3f04820546d0fd72df6c5370c07569c3 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 30 Dec 2016 11:10:33 -0300 Subject: [PATCH] Bug 17828: Unit tests for Koha::Patron::Attribute->store Unit tests for an overloaded Koha::Patron::Attribute->store that checks attribute type's uniqueness property and raises an exception conveniently. Test plan on the implementing patch. --- t/db_dependent/Koha/Patron/Attributes.t | 67 ++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patron/Attributes.t b/t/db_dependent/Koha/Patron/Attributes.t index 22e4d509a9..8e90637dc5 100644 --- a/t/db_dependent/Koha/Patron/Attributes.t +++ b/t/db_dependent/Koha/Patron/Attributes.t @@ -19,16 +19,81 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use t::lib::TestBuilder; +use Test::Exception; use Koha::Database; use Koha::Patron::Attribute; +use Koha::Patron::Attributes; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; +subtest 'store() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $patron + = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + my $attribute_type_1 = $builder->build( + { source => 'BorrowerAttributeType', + value => { unique_id => 0 } + } + ); + Koha::Patron::Attribute->new( + { borrowernumber => $patron, + code => $attribute_type_1->{code}, + attribute => 'Foo' + } + )->store; + Koha::Patron::Attribute->new( + { borrowernumber => $patron, + code => $attribute_type_1->{code}, + attribute => 'Bar' + } + )->store; + my $attr_count + = Koha::Patron::Attributes->search( + { borrowernumber => $patron, code => $attribute_type_1->{code} } ) + ->count; + is( $attr_count, 2, + '2 non-unique attributes stored and retrieved correcctly' ); + + my $attribute_type_2 = $builder->build( + { source => 'BorrowerAttributeType', + value => { unique_id => 1 } + } + ); + + Koha::Patron::Attribute->new( + { borrowernumber => $patron, + code => $attribute_type_2->{code}, + attribute => 'Foo' + } + )->store; + throws_ok { + Koha::Patron::Attribute->new( + { borrowernumber => $patron, + code => $attribute_type_2->{code}, + attribute => 'Bar' + } + )->store; + } + 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint', + 'Exception thrown trying to store more than one unique attribute'; + my $attributes = Koha::Patron::Attributes->search( + { borrowernumber => $patron, code => $attribute_type_2->{code} } ); + is( $attributes->count, 1, '1 Unique attribute stored' ); + is( $attributes->next->attribute, + 'Foo', 'Unique attribute remains unchanged' ); + + $schema->storage->txn_rollback; +}; + subtest 'opac_display() tests' => sub { plan tests => 2; -- 2.11.0