forked from Koha-Community/Koha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoha_MetadataRecord.t
executable file
·156 lines (133 loc) · 4.88 KB
/
Koha_MetadataRecord.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/perl
# Copyright 2013 C & P Bibliography Services
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 5;
use Test::Warn;
BEGIN {
use_ok('Koha::MetadataRecord');
}
my $marcrecord = MARC::Record->new;
$marcrecord->add_fields(
[ '001', '1234' ],
[ '150', ' ', ' ', a => 'Cooking' ],
[ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
);
my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
my $samplehash = [
{
'field' => [
{
'value' => '1234',
'tag' => '001',
}
]
},
{
'field' => [
{
'subfield' => [
{
'value' => 'Cooking',
'subtag' => 'a'
}
],
'indicator2' => ' ',
'tag' => 150,
'indicator1' => ' ',
}
]
},
{
'field' => [
{
'subfield' => [
{
'value' => 'Cookery',
'subtag' => 'a'
},
{
'value' => 'Instructional manuals',
'subtag' => 'z'
}
],
'indicator2' => ' ',
'tag' => 450,
'indicator1' => ' ',
}
]
}
];
my $hash = $record->createMergeHash();
my %fieldkeys;
foreach my $field (@$hash) {
$fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
if (defined $field->{'field'}->[0]->{'subfield'}) {
foreach my $subfield (@{$field->{'field'}->[0]->{'subfield'}}) {
$fieldkeys{delete $subfield->{'subkey'}}++;
}
}
}
is_deeply($hash, $samplehash, 'Generated hash correctly');
my $dupkeys = grep { $_ > 1 } values %fieldkeys;
is($dupkeys, 0, 'No duplicate keys');
subtest "new() tests" => sub {
plan tests => 14;
# Test default values with a MARC::Record record
my $record = MARC::Record->new();
my $metadata_record;
warning_is { $metadata_record = new Koha::MetadataRecord({
record => $record }) }
{ carped => 'No schema passed' },
"Metadata schema is mandatory, raise a carped warning if omitted";
is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted");
$metadata_record = new Koha::MetadataRecord({
record => $record,
schema => 'marc21'
});
is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved');
is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21');
is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc');
is( $metadata_record->id, undef, 'id is optional, undef if unspecifid');
# Test passed values, also no constraint on record type
my $weird_record = {};
bless $weird_record, 'Weird::Class';
$metadata_record = new Koha::MetadataRecord({
record => $weird_record,
schema => 'something',
format => 'else',
id => 'an id'
});
is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved');
is( $metadata_record->schema, 'something', 'Metadata schema correctly set');
is( $metadata_record->format, 'else', 'Serializacion format correctly set');
is( $metadata_record->id, 'an id', 'The id correctly set');
# Having a record object is mandatory
warning_is { $metadata_record = new Koha::MetadataRecord({
record => undef,
schema => 'something',
format => 'else',
id => 'an id'
}) }
{ carped => 'No record passed' },
'Undefined record raises carped warning';
is( $metadata_record, undef, 'record object mandatory')
};
1;