Bug 14778: Make 3 tests pass
[koha.git] / t / Koha_MetadataRecord.t
blobf53e13dd4f469838abd06c7f250802c75d2eaa65
1 #!/usr/bin/perl
3 # Copyright 2013 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 5;
23 use Test::Warn;
25 BEGIN {
26 use_ok('Koha::MetadataRecord');
29 my $marcrecord = MARC::Record->new;
31 $marcrecord->add_fields(
32 [ '001', '1234' ],
33 [ '150', ' ', ' ', a => 'Cooking' ],
34 [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
36 my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
38 is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
40 my $samplehash = [
42 'field' => [
44 'value' => '1234',
45 'tag' => '001',
50 'field' => [
52 'subfield' => [
54 'value' => 'Cooking',
55 'subtag' => 'a'
58 'indicator2' => ' ',
59 'tag' => 150,
60 'indicator1' => ' ',
65 'field' => [
67 'subfield' => [
69 'value' => 'Cookery',
70 'subtag' => 'a'
73 'value' => 'Instructional manuals',
74 'subtag' => 'z'
77 'indicator2' => ' ',
78 'tag' => 450,
79 'indicator1' => ' ',
85 my $hash = $record->createMergeHash();
86 my %fieldkeys;
87 foreach my $field (@$hash) {
88 $fieldkeys{delete $field->{'field'}->[0]->{'key'}}++;
89 if (defined $field->{'field'}->[0]->{'subfield'}) {
90 foreach my $subfield (@{$field->{'field'}->[0]->{'subfield'}}) {
91 $fieldkeys{delete $subfield->{'subkey'}}++;
96 is_deeply($hash, $samplehash, 'Generated hash correctly');
97 my $dupkeys = grep { $_ > 1 } values %fieldkeys;
98 is($dupkeys, 0, 'No duplicate keys');
101 subtest "new() tests" => sub {
103 plan tests => 14;
105 # Test default values with a MARC::Record record
106 my $record = MARC::Record->new();
107 my $metadata_record;
109 warning_is { $metadata_record = new Koha::MetadataRecord({
110 record => $record }) }
111 { carped => 'No schema passed' },
112 "Metadata schema is mandatory, raise a carped warning if omitted";
113 is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted");
115 $metadata_record = new Koha::MetadataRecord({
116 record => $record,
117 schema => 'marc21'
120 is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
121 is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved');
122 is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21');
123 is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc');
124 is( $metadata_record->id, undef, 'id is optional, undef if unspecifid');
126 # Test passed values, also no constraint on record type
127 my $weird_record = {};
128 bless $weird_record, 'Weird::Class';
130 $metadata_record = new Koha::MetadataRecord({
131 record => $weird_record,
132 schema => 'something',
133 format => 'else',
134 id => 'an id'
137 is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct');
138 is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved');
139 is( $metadata_record->schema, 'something', 'Metadata schema correctly set');
140 is( $metadata_record->format, 'else', 'Serializacion format correctly set');
141 is( $metadata_record->id, 'an id', 'The id correctly set');
143 # Having a record object is mandatory
144 warning_is { $metadata_record = new Koha::MetadataRecord({
145 record => undef,
146 schema => 'something',
147 format => 'else',
148 id => 'an id'
149 }) }
150 { carped => 'No record passed' },
151 'Undefined record raises carped warning';
153 is( $metadata_record, undef, 'record object mandatory')