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::Application::AppUtils;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 my $script = OpenILS::Utils::TestUtils->new();
14 my $U = 'OpenILS::Application::AppUtils';
22 'open-ils.hold-targeter',
23 'open-ils.hold-targeter.target',
28 # == Targeting Concerto hold 67. Title hold.
31 my $result = target({hold => $hold_id});
33 ok($result->{success}, "Targeting hold $hold_id returned success");
35 # Concerto hold 67 targets record 70 with a pickup_lib of 4.
36 # There are several viable copies with circ lib 4.
37 my $current_copy = $e->retrieve_asset_copy($result->{target});
38 is($current_copy->circ_lib.'', '4', 'Targeted copy lives at pickup lib');
40 my $maps = $e->search_action_hold_copy_map([
44 flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
48 is(scalar(@$maps), 29, "Hold $hold_id has 29 mapped potential copies");
50 is(scalar(grep {$_->target_copy->call_number->record != 70} @$maps), 0,
51 'All targeted copies belong to the targeted bib record');
53 # Retarget to confirm a new copy is selected and that the previously
54 # targeted item has a new entry in action.unfulfilled_hold_list.
56 $result = target({hold => $hold_id});
58 isnt($result->{target}, $current_copy->id,
59 'Second targeter run on hold 67 selected targeted a different copy');
61 my $unfulfilled = $e->search_action_unfulfilled_hold_list(
62 {hold => $hold_id, current_copy => $current_copy->id})->[0];
64 isnt($unfulfilled, undef, 'Previous copy has unfulfilled hold entry');
66 my $prev_target = $result->{target};
68 $result = target({hold => $hold_id, soft_retarget_interval => '0s'});
70 is($result->{target}, $prev_target,
71 "Hold $hold_id target remains the same with soft_retarget_interval");
73 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
75 is(scalar(@$maps), 29,
76 "Hold $hold_id retains 29 mapped potential copies with soft_retarget_interval");
79 # == Metarecord hold tests
81 # Concerto hold 263 is a metarecord hold with pickup_lib 4, target 42, and
82 # holdable_format '{"0":[{"_attr":"mr_hold_format","_val":"score"}]}'.
85 $result = target({hold => $hold_id});
87 ok($result->{success}, "Targeting hold $hold_id returned success");
89 $current_copy = $e->retrieve_asset_copy($result->{target});
90 is($current_copy->circ_lib.'', '9', 'Targeted copy lives at pickup lib');
92 $maps = $e->search_action_hold_copy_map([
96 flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
100 is(scalar(@$maps), 24, "Hold $hold_id has 24 mapped potential copies");
102 # Only 1 bib record (42) links to metarecord 42. It also satisfies the
103 # holdable_format criteria.
104 is(scalar(grep {$_->target_copy->call_number->record != 42} @$maps), 0,
105 'All targeted copies belong to the targeted bib record');
107 # Bib 101 has mr_hold_format 'book'. Link it to the targeted metabib
108 # and confirm the targeter does not select it.
111 my $mrmap_101 = $e->search_metabib_metarecord_source_map({source => 101})->[0];
112 my $orig_101_mr = $mrmap_101->metarecord;
113 $mrmap_101->metarecord(42);
114 $e->update_metabib_metarecord_source_map($mrmap_101) or die $e->die_event;
116 # Temporarily point the original bib (42) at another metarecord
118 my $mrmap_42 = $e->search_metabib_metarecord_source_map({source => 42})->[0];
119 my $orig_42_mr = $mrmap_42->metarecord;
120 $mrmap_42->metarecord(1);
121 $e->update_metabib_metarecord_source_map($mrmap_42) or die $e->die_event;
124 # This time no copies should be targeted, since no records match
125 # the holdable_formats criteria.
126 $result = target({hold => $hold_id});
128 isnt($result->{success}, 1,
129 'Unable to target MR hold without copies matching holdable_format');
131 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
133 is(scalar(@$maps), 0,
134 'No potential copies exist that match the holdable_format criteria');
136 # Now remove the holdable format restriction and copies belonging to
137 # record 101 should now be acceptable potential copies.
139 my $hold = $e->retrieve_action_hold_request($hold_id);
140 $hold->clear_holdable_formats;
141 $e->update_action_hold_request($hold) or die $e->die_event;
144 $result = target({hold => $hold_id});
146 $current_copy = $e->retrieve_asset_copy([
148 {flesh => 1, flesh_fields => {acp => ['call_number']}}
151 is($current_copy->call_number->record.'', '101',
152 'Metarecord hold targeted after removing holdable_format restriction');
154 # Return the hold and bib records to their original metarecord state
155 # for re-test-ability.
157 $hold->holdable_formats('{"0":[{"_attr":"mr_hold_format","_val":"score"}]}');
158 $e->update_action_hold_request($hold) or die $e->die_event;
159 $mrmap_101->metarecord($orig_101_mr);
160 $mrmap_42->metarecord(42);
161 $e->update_metabib_metarecord_source_map($mrmap_101) or die $e->die_event;
162 $e->update_metabib_metarecord_source_map($mrmap_42) or die $e->die_event;