Bugzilla – Attachment 168937 Details for
Bug 36641
Add an endpoint to list circulation rules
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 36641: Unit tests for effective=0
Bug-36641-Unit-tests-for-effective0.patch (text/plain), 7.59 KB, created by
Martin Renvoize (ashimema)
on 2024-07-13 12:14:49 UTC
(
hide
)
Description:
Bug 36641: Unit tests for effective=0
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2024-07-13 12:14:49 UTC
Size:
7.59 KB
patch
obsolete
>From 361e0f7d1c97d55f9d161667913f0e1d289f2fc3 Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Fri, 12 Jul 2024 10:16:08 +0100 >Subject: [PATCH] Bug 36641: Unit tests for effective=0 > >--- > t/db_dependent/api/v1/circulation_rules.t | 123 +++++++++++++++++----- > 1 file changed, 98 insertions(+), 25 deletions(-) > >diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t >index f1ec45a56ec..da9913bb6cb 100755 >--- a/t/db_dependent/api/v1/circulation_rules.t >+++ b/t/db_dependent/api/v1/circulation_rules.t >@@ -33,7 +33,10 @@ my $t = Test::Mojo->new('Koha::REST::V1'); > t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); > > subtest 'list_rules() tests' => sub { >- plan tests => 32; >+ >+ my $expected_rules = [ keys %{ Koha::CirculationRules->rule_kinds } ]; >+ >+ plan tests => ( scalar( @{$expected_rules} ) * 2 ) + 36; > > $schema->storage->txn_begin; > >@@ -62,9 +65,18 @@ subtest 'list_rules() tests' => sub { > $patron->set_password( { password => $password, skip_validation => 1 } ); > my $unauth_userid = $patron->userid; > >+ note("Effective rules by default"); > ## Authorized user tests >- # No circulation_rules, so empty hash should be returned >- $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( '/0' => {} ); >+ # No circulation_rules, so all keys in the returned hash should be undefined >+ $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200); >+ >+ # Extract and decode the JSON response >+ my $json = $t->tx->res->json; >+ note("No rules defined"); >+ foreach my $key ( @{$expected_rules} ) { >+ ok( exists $json->[0]->{$key}, "Key '$key' exists in the JSON response" ); >+ is( $json->[0]->{$key}, undef, "'$key' is undefined" ); >+ } > > # One rule created, should get returned > ok( >@@ -80,8 +92,10 @@ subtest 'list_rules() tests' => sub { > 'Given I added an issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,' > ); > >+ note("One default rule defined"); > $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200) >- ->json_is( '/0' => { 'fine' => 2 }, "Our single rule is returned" ); >+ ->json_is( '/0/fine' => 2, "Default fine rule is returned as expected" ) >+ ->json_is( '/0/finedays' => undef, "Rule finedays is undefined as expected" ); > > # Two circulation_rules created, they should both be returned > ok( >@@ -97,13 +111,10 @@ subtest 'list_rules() tests' => sub { > 'Given I added another issuing rule branchcode => undef,' . ' categorycode => undef, itemtype => undef,' > ); > >- $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( >- '/0' => { >- fine => 2, >- finedays => 5, >- }, >- "Two default rules are returned" >- ); >+ note("Two default rules defined"); >+ $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200) >+ ->json_is( '/0/fine' => 2, "Default fine rule is returned as expected" ) >+ ->json_is( '/0/finedays' => 5, "Default finedays rule is returned as expected" ); > > # Specificity works, three circulation_rules stored, one branchcode specific > ok( >@@ -119,21 +130,17 @@ subtest 'list_rules() tests' => sub { > "Given I added an issuing rule branchcode => $branchcode," . ' categorycode => undef, itemtype => undef,' > ); > >- $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$branchcode")->status_is(200)->json_is( >- '/0' => { >- fine => 4, >- finedays => 5, >- }, >- "Branch specific rule is returned when library is added to request query" >- ); >+ note("Two default rules and one branch rule defined"); >+ $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$branchcode")->status_is(200) >+ ->json_is( '/0/fine' => 4, "Branch specific fine rule is returned when library is added to request query" ) >+ ->json_is( >+ '/0/finedays' => 5, >+ "Default finedays rule is returned when library is added to request query but no branch specific rule is defined" >+ ); > >- $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( >- '/0' => { >- fine => 2, >- finedays => 5, >- }, >- "Default rules are returned when no library is added to request query" >- ); >+ $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200) >+ ->json_is( '/0/fine' => 2, "Defaul fine rule returned when no library is added to request query" ) >+ ->json_is( '/0/finedays' => 5, "Default finedays rule returned when no library is added to request query" ); > > # Warn on unsupported query parameter > $t->get_ok("//$userid:$password@/api/v1/circulation_rules?rules_blah=blah")->status_is(400) >@@ -202,5 +209,71 @@ subtest 'list_rules() tests' => sub { > # Unauthorized access > $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403); > >+ subtest 'effective=false tests' => sub { >+ >+ my $count = scalar( @{$expected_rules} ); >+ >+ plan tests => ( $count * 2 ) + $count + 10; >+ >+ # All rules >+ $t->get_ok("//$userid:$password@/api/v1/circulation_rules?effective=0")->status_is(200); >+ >+ # Extract and decode the JSON response >+ my $json = $t->tx->res->json; >+ >+ # Check if the response is an array >+ is( ref $json, 'ARRAY', 'Response is an array' ); >+ is( scalar( @{$json} ), 2, 'Response contains 2 rule sets' ); >+ >+ # Iterate over each hash in the array >+ my $index = 0; >+ foreach my $hash ( @{$json} ) { >+ my $pointer = Mojo::JSON::Pointer->new($hash); >+ >+ # First rule set should march default, default, default >+ if ( $index == 0 ) { >+ ok( $pointer->get('/branchcode') eq "*" >+ && $pointer->get('/itemtype') eq '*' >+ && $pointer->get('/categorycode') eq '*', "Default rules returned first" ); >+ } >+ >+ # Iterate over the list of expected keys for each hash >+ foreach my $key ( @{$expected_rules} ) { >+ ok( $pointer->contains( '/' . $key ), "Hash contains key '$key'" ); >+ } >+ >+ $index++; >+ } >+ >+ # Filter on library >+ $t->get_ok("//$userid:$password@/api/v1/circulation_rules?effective=0&library_id=$branchcode")->status_is(200); >+ >+ # Extract and decode the JSON response >+ $json = $t->tx->res->json; >+ >+ # Check if the response is an array >+ is( ref $json, 'ARRAY', 'Response is an array' ); >+ is( scalar( @{$json} ), 1, 'Filtered response contains 1 rule set' ); >+ >+ $index = 0; >+ foreach my $hash ( @{$json} ) { >+ my $pointer = Mojo::JSON::Pointer->new($hash); >+ >+ # First (and only) rule set should match branchcode, default, default. >+ if ( $index == 0 ) { >+ ok( $pointer->get('/branchcode') eq $branchcode >+ && $pointer->get('/itemtype') eq '*' >+ && $pointer->get('/categorycode') eq '*', "Branchcode rule set returned when filtered" ); >+ } >+ >+ # Iterate over the list of expected keys for each hash >+ foreach my $key ( @{$expected_rules} ) { >+ ok( $pointer->contains( '/' . $key ), "Hash contains key '$key'" ); >+ } >+ >+ $index++; >+ } >+ >+ }; > $schema->storage->txn_rollback; > }; >-- >2.45.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 36641
:
165086
|
167937
|
167938
|
167946
|
167947
|
168018
|
168019
|
168020
|
168023
|
168024
|
168025
|
168365
|
168366
|
168367
|
168368
|
168369
|
168370
|
168378
|
168379
|
168395
|
168396
|
168397
|
168398
|
168399
|
168400
|
168401
|
168841
|
168874
|
168875
|
168876
|
168877
|
168878
|
168879
|
168880
|
168881
|
168882
|
168883
|
168884
|
168905
|
168906
|
168907
|
168908
|
168909
|
168910
|
168911
|
168912
|
168913
|
168914
|
168915
|
168916
|
168926
|
168927
|
168928
|
168929
|
168930
|
168931
|
168932
|
168933
|
168934
|
168935
|
168936
|
168937
|
168938
|
168940
|
169013
|
169014
|
169015
|
169016
|
169017
|
169018
|
169019
|
169020
|
169021
|
169022
|
169023
|
169024
|
169025
|
169026