]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/20-hold-targeter.t
LP#1596595 AOUS lookup batch by org id
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 20-hold-targeter.t
1 #!perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 15;
6 diag("General hold targeter tests");
7
8 use OpenILS::Const qw/:const/;
9 use OpenILS::Utils::TestUtils;
10 use OpenILS::Utils::HoldTargeter;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
12
13 my $script = OpenILS::Utils::TestUtils->new();
14 my $targeter = OpenILS::Utils::HoldTargeter->new;
15 my $e = new_editor();
16
17 $script->bootstrap;
18 $e->init;
19
20 # == Targeting Concerto hold 1.  Title hold.
21
22 my $hold_id = 1;
23 my $result = $targeter->target(hold => $hold_id)->[0];
24
25 ok($result->{success}, "Targeting hold $hold_id returned success");
26
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');
31
32 my $maps = $e->search_action_hold_copy_map([
33     {hold => $hold_id},
34     {
35         flesh => 2, 
36         flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
37     }
38 ]);
39
40 is(scalar(@$maps), 25, "Hold $hold_id has 25 mapped potential copies");
41
42 is(scalar(grep {$_->target_copy->call_number->record != 2} @$maps), 0,
43     'All targeted copies belong to the targeted bib record');
44
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.
47
48 $result = $targeter->target(hold => $hold_id)->[0];
49
50 isnt($result->{target}, $current_copy->id, 
51     'Second targeter run on hold 1 selected targeted a different copy');
52
53 my $unfulfilled = $e->search_action_unfulfilled_hold_list(
54     {hold => $hold_id, current_copy => $current_copy->id})->[0];
55
56 isnt($unfulfilled, undef, 'Previous copy has unfulfilled hold entry');
57
58 my $prev_target = $result->{target};
59
60 $result = $targeter->target(hold => $hold_id, skip_viable => 1)->[0];
61
62 is($result->{target}, $prev_target, 
63     "Hold $hold_id target remains the same with --skip-viable");
64
65 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
66
67 is(scalar(@$maps), 25, 
68     "Hold $hold_id retains 25 mapped potential copies with --skip-viable");
69
70
71 # == Metarecord hold tests
72 #
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"}]}'.
75
76 $hold_id = 263;
77 $result = $targeter->target(hold => $hold_id)->[0];
78
79 ok($result->{success}, "Targeting hold $hold_id returned success");
80
81 $current_copy = $e->retrieve_asset_copy($result->{target});
82 is($current_copy->circ_lib.'', '9', 'Targeted copy lives at pickup lib');
83
84 $maps = $e->search_action_hold_copy_map([
85     {hold => $hold_id},
86     {
87         flesh => 2, 
88         flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
89     }
90 ]);
91
92 is(scalar(@$maps), 22, "Hold $hold_id has 22 mapped potential copies");
93
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');
98
99 # Bib 101 has mr_hold_format 'book'.  Link it to the targeted metabib
100 # and confirm the targeter does not select it.
101
102 $e->xact_begin;
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;
107
108 # Temporarily point the original bib (42) at another metarecord
109
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;
114 $e->xact_commit;
115
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];
119
120 isnt($result->{success}, 1, 
121     'Unable to target MR hold without copies matching holdable_format');
122
123 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
124
125 is(scalar(@$maps), 0, 
126     'No potential copies exist that match the holdable_format criteria');
127
128 # Now remove the holdable format restriction and copies belonging to
129 # record 101 should now be acceptable potential copies.
130 $e->xact_begin;
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;
134 $e->xact_commit;
135
136 $result = $targeter->target(hold => $hold_id)->[0];
137
138 $current_copy = $e->retrieve_asset_copy([
139     $result->{target},
140     {flesh => 1, flesh_fields => {acp => ['call_number']}}
141 ]);
142
143 is($current_copy->call_number->record.'', '101', 
144     'Metarecord hold targeted after removing holdable_format restriction');
145
146 # Return the hold and bib records to their original metarecord state 
147 # for re-test-ability.
148 $e->xact_begin;
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;
155 $e->xact_commit;
156
157