From d31d8e7c4795d6a172ce421b6f0ab6f4f4d53dcc Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 4 Nov 2024 11:57:24 +0000 Subject: [PATCH] Bug 30657: Further unit tests Test the repeatable handling and error rethrow. There's also an attempt at counting calls to the database for subsequent attribute accessors, but it fails in an odd way I'm struggling to understand, so that test is commented for now --- t/db_dependent/Koha/Patron.t | 55 ++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 8ac6b343ded..085f39fcac5 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -1194,30 +1194,75 @@ subtest 'extended_attributes' => sub { }; subtest 'attribute accessor tests' => sub { - plan tests => 2; + plan tests => 4; $schema->storage->txn_begin; + # Cleanup previous attribute types Koha::Patron::Attribute::Types->search->delete; + # Create a non-repeatable attribute type and a repeatable attribute type my $attribute_type_1 = $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'smartnumber', repeatable => 0 } } ); + my $attribute_type_2 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { code => 'favbooks', repeatable => 1 } + } + ); - my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + # Create a test patron + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); - is( $patron->smartnumber, undef, 'smartnumber accessor returns undef when not set'); + # Test non-existing attribute (should return undef and not throw an error) + is( $patron->smartnumber, undef, 'smartnumber accessor returns undef when not set' ); + # Test setting and retrieving a non-repeatable attribute + $patron->extended_attributes( [ { code => 'smartnumber', attribute => 'SM1234' } ] ); + is( $patron->smartnumber, 'SM1234', 'smartnumber accessor returns value when set' ); + + # Test setting and retrieving a repeatable attribute $patron->extended_attributes( [ - { code => 'smartnumber', attribute => 'SM1234' } + { code => 'favbooks', attribute => 'Book1' }, + { code => 'favbooks', attribute => 'Book2' } ] ); + is_deeply( + $patron->favbooks, [ 'Book1', 'Book2' ], + 'favbooks accessor returns an array of values for repeatable attribute' + ); - is( $patron->smartnumber, 'SM1234', 'smartnumber accessor returns value when set'); + # Test for a non-existing method to confirm it throws an error + throws_ok { $patron->nonexistentattribute } 'Koha::Exceptions::Object::MethodNotCoveredByTests', + 'Calling a non-existent attribute method throws an error'; + +# # Count DBI calls to confirm once one attribute accessor is called, we trigger AUTOLOAD again +# # for subsequent attribute accessor calls for any valid attribute types. +# # Enable DBI tracing and count search queries for attribute types +# my $trace_output = ''; +# open my $fh, '>', \$trace_output or die "Can't open scalar for writing: $!"; +# +# # Enable tracing with a callback to capture the output +# my $dbh = $schema->storage->dbh; +# $dbh->trace(0, $fh); +# +# # Call each accessor method and confirm the search call count +# my $refetched_patron = Koha::Patron->find( $patron->id ); +# $refetched_patron->smartnumber; # First access +# $refetched_patron->favbooks; # Access a different attribute type +# +# # Disable tracing +# $dbh->trace(0); +# +# # Count occurrences of the attribute types query in trace output +# my $search_query_count = () = $trace_output =~ /SELECT .* FROM patron_attribute_types/i; +# +# is( $search_query_count, 1, 'attributes->search is only called once per attribute type' ); $schema->storage->txn_rollback; }; -- 2.47.0