]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/MFHD/test/mfhd.t
LP2045292 Color contrast for AngularJS patron bills
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Utils / MFHD / test / mfhd.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use Test::More 'no_plan';
7 use File::Basename qw(dirname);
8
9 use MARC::Record;
10 use OpenILS::Utils::MFHD;
11
12 use testlib;
13
14 my $testno = 1;
15
16 sub right_answer {
17     my $holding = shift;
18     my $answer  = {};
19
20     foreach my $subfield (split(/\|/, $holding->subfield('x'))) {
21         next unless $subfield;
22
23         my ($key, $val) = unpack('aa*', $subfield);
24         $answer->{$key} = $val;
25     }
26
27     return $answer;
28 }
29
30
31 my $rec;
32 my @captions;
33
34 my $testfile = dirname(__FILE__) . "/mfhddata.txt";
35 open(my $testdata, "<", $testfile) or die("Cannot open '$testfile': $!");
36
37 while ($rec = testlib::load_MARC_rec($testdata, $testno++)) {
38     $rec = MFHD->new($rec);
39
40     foreach my $cap (sort { $a->tag <=> $b->tag } $rec->field('85.')) {
41         my $htag;
42         my @holdings;
43
44         ($htag = $cap->tag) =~ s/^85/86/;
45         @holdings = $rec->holdings($htag, $cap->subfield('8'));
46
47         if (!ok(scalar @holdings, "holdings defined " . $cap->subfield('8'))) {
48             next;
49         }
50
51         foreach my $field (@holdings) {
52           TODO: {
53                 local $TODO = "unimplemented"
54                   if ($field->subfield('z') =~ /^TODO/);
55                 is_deeply($field->next, right_answer($field),
56                     $field->subfield('8') . ': ' . $field->subfield('z'));
57
58                 if ($field->subfield('y')) {
59                     is($field->chron_to_date(), $field->subfield('y'), 'Chron-to-date test');
60                 }
61             }
62         }
63     }
64 }
65
66 close $testdata;
67
68 # test: passthru_open_ended
69 my $testfile2 = dirname(__FILE__) . "/mfhddata2.txt";
70 open($testdata, "<", $testfile2) or die("Cannot open '$testfile2': $!");
71
72 $rec = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
73 my $rec2 = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
74
75 my @holdings_a = $rec->get_decompressed_holdings(($rec->captions('853'))[0], {'passthru_open_ended' => 1});
76 my @holdings_b = $rec2->holdings_by_caption(($rec2->captions('853'))[0]);
77
78 is_deeply(\@holdings_a, \@holdings_b, 'passthru open ended');
79
80 # test: compressed to last
81 $testno++;
82
83 $rec = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
84 $rec2 = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
85
86 @holdings_a = $rec->holdings_by_caption(($rec->captions('853'))[0]);
87 @holdings_b = $rec2->holdings_by_caption(($rec2->captions('853'))[0]);
88
89 is_deeply($holdings_a[0]->compressed_to_last, $holdings_b[0], 'compressed to last, normal');
90 is($holdings_a[1]->compressed_to_last, undef, 'compressed to last, open ended');
91
92 # test: get compressed holdings
93 $testno++;
94
95 $rec = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
96 $rec2 = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
97
98 @holdings_a = $rec->get_compressed_holdings(($rec->captions('853'))[0]);
99 @holdings_b = $rec2->holdings_by_caption(($rec2->captions('853'))[0]);
100
101 is_deeply(\@holdings_a, \@holdings_b, 'get compressed holdings');
102
103
104 # test: get compressed holdings, open ended member
105 $testno++;
106
107 $rec = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
108 $rec2 = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
109
110 @holdings_a = $rec->get_compressed_holdings(($rec->captions('853'))[0]);
111 @holdings_b = $rec2->holdings_by_caption(($rec2->captions('853'))[0]);
112
113 is_deeply(\@holdings_a, \@holdings_b, 'get compressed holdings, open ended member');
114
115 # test comparisons, for all operands, for all types of holdings
116 $testno++;
117
118 $rec = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
119 $rec2 = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
120
121 @holdings_a = $rec->holdings_by_caption(($rec->captions('853'))[0]);
122 @holdings_b = $rec2->holdings_by_caption(($rec2->captions('853'))[0]);
123
124 unshift(@holdings_a, "zzz I am NOT a holding");
125 push(@holdings_b, "zzz I am NOT a holding");
126
127 push(@holdings_a, undef);
128 unshift(@holdings_b, undef);
129
130 @holdings_a = sort { $a cmp $b } @holdings_a;
131 my $seqno = 1;
132 foreach my $holding (@holdings_a) {
133     if (ref $holding) {
134         $holding->seqno($seqno);
135         $seqno++;
136     }
137 }
138
139 is_deeply(\@holdings_a, \@holdings_b, 'comparison testing via sort');
140
141 # test: get combined holdings
142 $testno++;
143
144 $rec = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
145 $rec2 = MFHD->new(testlib::load_MARC_rec($testdata, $testno));
146
147 @holdings_a = $rec->get_combined_holdings(($rec->captions('853'))[0]);
148 @holdings_b = $rec2->holdings_by_caption(($rec2->captions('853'))[0]);
149
150 is_deeply(\@holdings_a, \@holdings_b, 'get combined holdings');
151
152
153 close $testdata;
154 1;