5 use Test::More tests => 15;
6 diag("General hold targeter tests");
8 use OpenILS::Const qw/:const/;
9 use OpenILS::Utils::TestUtils;
10 use OpenILS::Utils::HoldTargeter;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 my $script = OpenILS::Utils::TestUtils->new();
14 my $targeter = OpenILS::Utils::HoldTargeter->new;
20 # == Targeting Concerto hold 1. Title hold.
23 my $result = $targeter->target(hold => $hold_id)->[0];
25 ok($result->{success}, "Targeting hold $hold_id returned success");
27 # Concerto hold 1 targets record 2 with a pickup_lib of 5.
28 # There are several viable copies with circ lib 5.
29 my $current_copy = $e->retrieve_asset_copy($result->{target});
30 is($current_copy->circ_lib.'', '5', 'Targeted copy lives at pickup lib');
32 my $maps = $e->search_action_hold_copy_map([
36 flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
40 is(scalar(@$maps), 25, "Hold $hold_id has 25 mapped potential copies");
42 is(scalar(grep {$_->target_copy->call_number->record != 2} @$maps), 0,
43 'All targeted copies belong to the targeted bib record');
45 # Retarget to confirm a new copy is selected and that the previously
46 # targeted item has a new entry in action.unfulfilled_hold_list.
48 $result = $targeter->target(hold => $hold_id)->[0];
50 isnt($result->{target}, $current_copy->id,
51 'Second targeter run on hold 1 selected targeted a different copy');
53 my $unfulfilled = $e->search_action_unfulfilled_hold_list(
54 {hold => $hold_id, current_copy => $current_copy->id})->[0];
56 isnt($unfulfilled, undef, 'Previous copy has unfulfilled hold entry');
58 my $prev_target = $result->{target};
60 $result = $targeter->target(hold => $hold_id, skip_viable => 1)->[0];
62 is($result->{target}, $prev_target,
63 "Hold $hold_id target remains the same with --skip-viable");
65 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
67 is(scalar(@$maps), 25,
68 "Hold $hold_id retains 25 mapped potential copies with --skip-viable");
71 # == Metarecord hold tests
73 # Concerto hold 263 is a metarecord hold with pickup_lib 4, target 42, and
74 # holdable_format '{"0":[{"_attr":"mr_hold_format","_val":"score"}]}'.
77 $result = $targeter->target(hold => $hold_id)->[0];
79 ok($result->{success}, "Targeting hold $hold_id returned success");
81 $current_copy = $e->retrieve_asset_copy($result->{target});
82 is($current_copy->circ_lib.'', '9', 'Targeted copy lives at pickup lib');
84 $maps = $e->search_action_hold_copy_map([
88 flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
92 is(scalar(@$maps), 22, "Hold $hold_id has 22 mapped potential copies");
94 # Only 1 bib record (45) links to metarecord 42. It also satisfies the
95 # holdable_format criteria.
96 is(scalar(grep {$_->target_copy->call_number->record != 45} @$maps), 0,
97 'All targeted copies belong to the targeted bib record');
99 # Bib 101 has mr_hold_format 'book'. Link it to the targeted metabib
100 # and confirm the targeter does not select it.
103 my $mrmap_101 = $e->search_metabib_metarecord_source_map({source => 101})->[0];
104 my $orig_101_mr = $mrmap_101->metarecord;
105 $mrmap_101->metarecord(42);
106 $e->update_metabib_metarecord_source_map($mrmap_101) or die $e->die_event;
108 # Temporarily point the original bib (42) at another metarecord
110 my $mrmap_42 = $e->search_metabib_metarecord_source_map({source => 45})->[0];
111 my $orig_42_mr = $mrmap_42->metarecord;
112 $mrmap_42->metarecord(1);
113 $e->update_metabib_metarecord_source_map($mrmap_42) or die $e->die_event;
116 # This time no copies should be targeted, since no records match
117 # the holdable_formats criteria.
118 $result = $targeter->target(hold => $hold_id)->[0];
120 isnt($result->{success}, 1,
121 'Unable to target MR hold without copies matching holdable_format');
123 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
125 is(scalar(@$maps), 0,
126 'No potential copies exist that match the holdable_format criteria');
128 # Now remove the holdable format restriction and copies belonging to
129 # record 101 should now be acceptable potential copies.
131 my $hold = $e->retrieve_action_hold_request($hold_id);
132 $hold->clear_holdable_formats;
133 $e->update_action_hold_request($hold) or die $e->die_event;
136 $result = $targeter->target(hold => $hold_id)->[0];
138 $current_copy = $e->retrieve_asset_copy([
140 {flesh => 1, flesh_fields => {acp => ['call_number']}}
143 is($current_copy->call_number->record.'', '101',
144 'Metarecord hold targeted after removing holdable_format restriction');
146 # Return the hold and bib records to their original metarecord state
147 # for re-test-ability.
149 $hold->holdable_formats('{"0":[{"_attr":"mr_hold_format","_val":"score"}]}');
150 $e->update_action_hold_request($hold) or die $e->die_event;
151 $mrmap_101->metarecord($orig_101_mr);
152 $mrmap_42->metarecord(42);
153 $e->update_metabib_metarecord_source_map($mrmap_101) or die $e->die_event;
154 $e->update_metabib_metarecord_source_map($mrmap_42) or die $e->die_event;