Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions mdp-lib/Access/Holdings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ sub _query_item_access_api {
$holdings_data->{n_enum},
@{$holdings_data->{ocns}}
);
$held = $holdings_data->{copy_count};
$held = $holdings_data->{$field};
$held = _extract_held_by_field($holdings_data, $field);
};
if (my $err = $@) {
log_error($err);
Expand Down Expand Up @@ -156,6 +155,31 @@ sub _query_item_held_by_api {

# ---------------------------------------------------------------------

=item _extract_held_by_field

Uses the named field to distill Holdings API return structure into a
`held` value. Plain vanilla `copy_count` and `brlm_count` just extract
the field of the same name. `currently_held_count` consults the
`currently_held_count` and the `deposited` value.

=cut

# ---------------------------------------------------------------------
sub _extract_held_by_field {
my $holdings_data = shift;
my $field = shift;

if ($field eq 'currently_held_count') {
if ($holdings_data->{currently_held_count} > 0) {
return $holdings_data->{currently_held_count};
}
return $holdings_data->{deposited} || 0;
}
return $holdings_data->{$field};
}

# ---------------------------------------------------------------------

=item _id_is_held_core

Common code for id_is_held, id_is_held_and_BRLM, and id_is_currently_held.
Expand Down
32 changes: 30 additions & 2 deletions mdp-lib/t/access/holdings.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ my $held_response = {
'copy_count' => 555,
'brlm_count' => 222,
'currently_held_count' => 111,
'deposited' => 1,
'format' => 'spm',
'n_enum' => 'v.1-5 (1901-1905)',
'ocns' => ['001', '002', '003']
Expand All @@ -78,7 +79,6 @@ sub get_ua_for_error {
return $ua;
}

my $held_response_json = $jsonxs->encode($held_response);
my $institutions_response_json = $jsonxs->encode($institutions_response);
my $item_access_endpoint = qr{$Access::Holdings::ITEM_ACCESS_ENDPOINT};
my $item_held_by_endpoint = qr{$Access::Holdings::ITEM_HELD_BY_ENDPOINT};
Expand All @@ -94,8 +94,13 @@ sub get_ua {
return $ua;
}

# Can use predefined $held_response above, or pass a custom structure.
sub get_ua_for_held {
return get_ua($item_access_endpoint, $held_response_json);
my $response = shift || $held_response;
return get_ua(
$item_access_endpoint,
$jsonxs->encode($response)
);
}

sub get_ua_for_institutions {
Expand Down Expand Up @@ -289,6 +294,16 @@ subtest "id_is_currently_held" => sub {
expect_params($ua);
$ENV{DEBUG} = $save_debug;
};

subtest "currently_held_count and deposited both 0" => sub {
# Copy the default response so we can fiddle with it
my $response = { %$held_response };
$response->{currently_held_count} = 0;
$response->{deposited} = 0;
my $ua = get_ua_for_held($response);
my ($lock_id, $held) = Access::Holdings::id_is_currently_held($C, $htid, 'umich', $ua);
is($held, 0);
};
};

subtest "currently held according to API" => sub {
Expand All @@ -307,6 +322,19 @@ subtest "id_is_currently_held" => sub {
expect_params($ua);
$ENV{DEBUG} = $save_debug;
};

subtest "currently held if currently_held_count or deposited" => sub {
my $currently_held_deposited_combinations = [[0, 1], [1, 0], [1, 1]];
foreach my $combo (@$currently_held_deposited_combinations) {
# Copy the default response so we can fiddle with it
my $response = { %$held_response };
$response->{currently_held_count} = $combo->[0];
$response->{deposited} = $combo->[1];
my $ua = get_ua_for_held($response);
my ($lock_id, $held) = Access::Holdings::id_is_currently_held($C, $htid, 'umich', $ua);
is($held, 1, "currently_held=$combo->[0], deposited=$combo->[1]");
};
};
};

subtest "return error message and held = 0 if API fails, and log error message" => sub {
Expand Down
3 changes: 3 additions & 0 deletions mdp-lib/t/auth/auth.t → mdp-lib/t/auth/auth.t.BRITTLE
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/perl

# THIS TEST IS DISABLED BECAUSE IT IS SUBJECT TO RANDOM SEGFAULTS
# SEE ETT-743

use strict;
use warnings;

Expand Down
2 changes: 1 addition & 1 deletion mock-holdings-api/web/v1/item_access.currently_held
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"brlm_count":0,"copy_count":1,"currently_held_count":1,"format":"spm","n_enum":"","ocns":[123456]}
{"brlm_count":0,"copy_count":1,"currently_held_count":1,"deposited":1,"format":"spm","n_enum":"","ocns":[123456]}
2 changes: 1 addition & 1 deletion mock-holdings-api/web/v1/item_access.not_current
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"brlm_count":0,"copy_count":1,"currently_held_count":0,"format":"spm","n_enum":"","ocns":[123456]}
{"brlm_count":0,"copy_count":1,"currently_held_count":0,"deposited":0,"format":"spm","n_enum":"","ocns":[123456]}
2 changes: 1 addition & 1 deletion mock-holdings-api/web/v1/item_access.not_held
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"brlm_count":0,"copy_count":0,"currently_held_count":0,"format":"spm","n_enum":"","ocns":[123456]}
{"brlm_count":0,"copy_count":0,"currently_held_count":0,"deposited":0,"format":"spm","n_enum":"","ocns":[123456]}