Line 0
Link Here
|
|
|
1 |
use Modern::Perl; |
2 |
|
3 |
use Test::More;#tests => 1; |
4 |
|
5 |
use Data::Dumper; |
6 |
|
7 |
use_ok("MARC::Field"); |
8 |
use_ok("MARC::Record"); |
9 |
use_ok("Koha::SimpleMARC"); |
10 |
|
11 |
|
12 |
sub new_record { |
13 |
my $record = MARC::Record->new; |
14 |
$record->leader('03174nam a2200445 a 4500'); |
15 |
my @fields = ( |
16 |
MARC::Field->new( |
17 |
100, '1', ' ', |
18 |
a => 'Knuth, Donald Ervin', |
19 |
d => '1938', |
20 |
), |
21 |
MARC::Field->new( |
22 |
245, '1', '4', |
23 |
a => 'The art of computer programming', |
24 |
c => 'Donald E. Knuth.', |
25 |
), |
26 |
MARC::Field->new( |
27 |
650, ' ', '0', |
28 |
a => 'Computer programming.', |
29 |
9 => '462', |
30 |
), |
31 |
MARC::Field->new( |
32 |
952, ' ', ' ', |
33 |
p => '3010023917', |
34 |
y => 'BK', |
35 |
c => 'GEN', |
36 |
d => '2001-06-25', |
37 |
), |
38 |
); |
39 |
$record->append_fields(@fields); |
40 |
return $record; |
41 |
} |
42 |
|
43 |
my $record = new_record; |
44 |
|
45 |
# field_exists |
46 |
is( field_exists( $record, '650', 'a'), 'Computer programming.', '650$a exists' ); |
47 |
is( field_exists( $record, '650', 'b'), undef, '650$b does not exist' ); |
48 |
|
49 |
$record->append_fields( |
50 |
MARC::Field->new( |
51 |
650, ' ', '0', |
52 |
a => 'Computer algorithms.', |
53 |
9 => '463', |
54 |
) |
55 |
); |
56 |
|
57 |
is( field_exists( $record, '650', 'a'), 'Computer programming.', '650$a exists, field_exists returns the first one' ); |
58 |
|
59 |
# read_field |
60 |
my @fields_650a = read_field( $record, '650', 'a'); |
61 |
is( $fields_650a[0], 'Computer programming.', 'first 650$a' ); |
62 |
is( $fields_650a[1], 'Computer algorithms.', 'second 650$a' ); |
63 |
is( read_field( $record, '650', 'a', 1 ), 'Computer programming.', 'first 650$a bis' ); |
64 |
is( read_field( $record, '650', 'a', 2 ), 'Computer algorithms.', 'second 650$a bis' ); |
65 |
is( read_field( $record, '650', 'a', 3 ), undef, 'There is no 3 650$a' ); |
66 |
|
67 |
# copy_field |
68 |
copy_field( $record, '245', 'a', '246', 'a' ); |
69 |
is_deeply( read_field( $record, '245', 'a' ), 'The art of computer programming', 'After copy 245$a still exists' ); |
70 |
is_deeply( read_field( $record, '246', 'a' ), 'The art of computer programming', '246$a is a new field' ); |
71 |
delete_field( $record, '246' ); |
72 |
is( field_exists( $record, '246', 'a', '246$a does not exist anymore' ), undef ); |
73 |
|
74 |
copy_field( $record, '650', 'a', '651', 'a' ); |
75 |
my @fields_651a = read_field( $record, '651', 'a' ); |
76 |
is_deeply( \@fields_651a, ['Computer programming.', 'Computer algorithms.'], 'Copy multivalued field' ); |
77 |
delete_field( $record, '651' ); |
78 |
|
79 |
copy_field( $record, '650', 'a', '651', 'a', undef, 1 ); |
80 |
@fields_651a = read_field( $record, '651', 'a' ); |
81 |
is_deeply( read_field( $record, '651', 'a' ), 'Computer programming.', 'Copy first field 650$a' ); |
82 |
delete_field( $record, '651' ); |
83 |
|
84 |
copy_field( $record, '650', 'a', '651', 'a', undef, 2 ); |
85 |
@fields_651a = read_field( $record, '651', 'a' ); |
86 |
is_deeply( read_field( $record, '651', 'a' ), 'Computer algorithms.', 'Copy second field 650$a' ); |
87 |
delete_field( $record, '651' ); |
88 |
|
89 |
copy_field( $record, '650', 'a', '651', 'a', '/Computer/The art of/' ); |
90 |
@fields_651a = read_field( $record, '651', 'a' ); |
91 |
is_deeply( \@fields_651a, ['The art of programming.', 'The art of algorithms.'], 'Copy field using regex' ); |
92 |
|
93 |
copy_field( $record, '650', 'a', '651', 'a', '/Computer/The mistake of/' ); |
94 |
@fields_651a = read_field( $record, '651', 'a' ); |
95 |
is_deeply( \@fields_651a, ['The mistake of programming.', 'The mistake of algorithms.'], 'Copy fields using regex on existing fields' ); |
96 |
delete_field( $record, '651' ); |
97 |
|
98 |
copy_field( $record, '650', 'a', '651', 'a', '/Computer/The art of/' ); |
99 |
copy_field( $record, '650', 'a', '651', 'a', '/Computer/The mistake of/', 1, "dont_erase" ); |
100 |
@fields_651a = read_field( $record, '651', 'a' ); |
101 |
is_deeply( \@fields_651a, [ |
102 |
'The art of programming.', |
103 |
'The mistake of programming.', |
104 |
'The art of algorithms.', |
105 |
'The mistake of programming.' |
106 |
], 'Copy first field using regex on existing fields without erase existing values' ); |
107 |
delete_field( $record, '651' ); |
108 |
|
109 |
copy_field( $record, '650', 'a', '651', 'a', '/Computer/The art of/' ); |
110 |
copy_field( $record, '650', 'a', '651', 'a', '/Computer/The mistake of/', undef , "dont_erase" ); |
111 |
@fields_651a = read_field( $record, '651', 'a' ); |
112 |
is_deeply( \@fields_651a, [ |
113 |
'The art of programming.', |
114 |
'The mistake of programming.', |
115 |
'The mistake of algorithms.', |
116 |
'The art of algorithms.', |
117 |
'The mistake of programming.', |
118 |
'The mistake of algorithms.' |
119 |
], 'Copy fields using regex on existing fields without erase existing values' ); |
120 |
delete_field( $record, '651' ); |
121 |
|
122 |
|
123 |
# update_field |
124 |
update_field( $record, '952', 'p', undef, '3010023918' ); |
125 |
is_deeply( read_field( $record, '952', 'p' ), '3010023918', 'update existing subfield 952$p' ); |
126 |
delete_field( $record, '952' ); |
127 |
update_field( $record, '952', 'p', undef, '3010023918' ); |
128 |
update_field( $record, '952', 'y', undef, 'BK' ); |
129 |
is_deeply( read_field( $record, '952', 'p' ), '3010023918', 'create subfield 952$p' ); |
130 |
is_deeply( read_field( $record, '952', 'y' ), 'BK', 'create subfield 952$k on existing 952 field' ); |
131 |
$record->append_fields( |
132 |
MARC::Field->new( |
133 |
952, ' ', ' ', |
134 |
p => '3010023917', |
135 |
y => 'BK', |
136 |
), |
137 |
); |
138 |
update_field( $record, '952', 'p', undef, '3010023919' ); |
139 |
my @fields_952p = read_field( $record, '952', 'p' ); |
140 |
is_deeply( \@fields_952p, ['3010023919', '3010023919'], 'update all subfields 952$p with the same value' ); |
141 |
|
142 |
update_field( $record, '952', 'p', undef, ('3010023917', '3010023918') ); |
143 |
@fields_952p = read_field( $record, '952', 'p' ); |
144 |
is_deeply( \@fields_952p, ['3010023917', '3010023918'], 'update all subfields 952$p with the different values' ); |
145 |
|
146 |
# move_field |
147 |
$record = new_record; |
148 |
my ( @fields_952d, @fields_952c, @fields_954c, @fields_954p); |
149 |
$record->append_fields( |
150 |
MARC::Field->new( |
151 |
952, ' ', ' ', |
152 |
p => '3010023917', |
153 |
y => 'BK', |
154 |
), |
155 |
); |
156 |
copy_field( $record, '952', 'd', '952', 'd' ); |
157 |
@fields_952d = read_field( $record, '952', 'd' ); |
158 |
is_deeply( \@fields_952d, ['2001-06-25', '2001-06-25'], 'copy 952$d into others 952 field' ); |
159 |
|
160 |
move_field( $record, '952', 'c', '954', 'c' ); |
161 |
@fields_952c = read_field( $record, '952', 'c' ); |
162 |
@fields_954c = read_field( $record, '954', 'c' ); |
163 |
is_deeply( \@fields_952c, [], 'The 952$c has moved' ); |
164 |
is_deeply( \@fields_954c, ['GEN'], 'Now 954$c exists' ); |
165 |
|
166 |
move_field( $record, '952', 'p', '954', 'p', undef, 1 ); # Move the first field |
167 |
@fields_952p = read_field( $record, '952', 'p' ); |
168 |
@fields_954p = read_field( $record, '954', 'p' ); |
169 |
is_deeply( \@fields_952p, ['3010023917'], 'One of 952$p has moved' ); |
170 |
is_deeply( \@fields_954p, ['3010023917'], 'Now 954$p exists' ); |
171 |
|
172 |
$record = new_record; |
173 |
$record->append_fields( |
174 |
MARC::Field->new( |
175 |
952, ' ', ' ', |
176 |
p => '3010023917', |
177 |
y => 'BK', |
178 |
), |
179 |
); |
180 |
|
181 |
move_field( $record, '952', 'p', '954', 'p' ); # Move all field |
182 |
@fields_952p = read_field( $record, '952', 'p' ); |
183 |
@fields_954p = read_field( $record, '954', 'p' ); |
184 |
is_deeply( \@fields_952p, [], 'All 952$p have moved' ); |
185 |
is_deeply( \@fields_954p, ['3010023917', '3010023917'], 'Now 2 954$p exist' ); |
186 |
|
187 |
|
188 |
done_testing; |