From 00134474e8773c6085cde656f04e52b1ec372f55 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. It also tests for repeatable attribute type's property. Test plan on the implementing patch. --- t/db_dependent/Koha/Patron/Attributes.t | 130 +++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patron/Attributes.t b/t/db_dependent/Koha/Patron/Attributes.t index 22e4d509a9..36b04df39d 100644 --- a/t/db_dependent/Koha/Patron/Attributes.t +++ b/t/db_dependent/Koha/Patron/Attributes.t @@ -19,16 +19,144 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 4; 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() repeatable attributes 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 => { repeatable => 1 } + } + ); + 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 repeatable attributes stored and retrieved correcctly' ); + + my $attribute_type_2 = $builder->build( + { source => 'BorrowerAttributeType', + value => { repeatable => 0 } + } + ); + + 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::NonRepeatable', + 'Exception thrown trying to store more than one non-repeatable attribute'; + my $attributes = Koha::Patron::Attributes->search( + { borrowernumber => $patron, code => $attribute_type_2->{code} } ); + is( $attributes->count, 1, '1 non-repeatable attribute stored' ); + is( $attributes->next->attribute, + 'Foo', 'Non-repeatable attribute remains unchanged' ); + + $schema->storage->txn_rollback; +}; + +subtest 'store() unique_id attributes tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; + + my $attribute_type_1 = $builder->build( + { source => 'BorrowerAttributeType', + value => { unique_id => 0 } + } + ); + Koha::Patron::Attribute->new( + { borrowernumber => $patron_1, + code => $attribute_type_1->{code}, + attribute => 'Foo' + } + )->store; + Koha::Patron::Attribute->new( + { borrowernumber => $patron_2, + code => $attribute_type_1->{code}, + attribute => 'Bar' + } + )->store; + my $attr_count + = Koha::Patron::Attributes->search( + { 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_1, + code => $attribute_type_2->{code}, + attribute => 'Foo' + } + )->store; + throws_ok { + Koha::Patron::Attribute->new( + { borrowernumber => $patron_2, + code => $attribute_type_2->{code}, + attribute => 'Foo' + } + )->store; + } + 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint', + 'Exception thrown trying to store more than one unique attribute'; + my $attributes = Koha::Patron::Attributes->search( + { borrowernumber => $patron_1, 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