Bug 13281: t/ImportBatch.t shouldn't depend on the DB
[koha.git] / t / Koha_MetadataRecord.t
1 #!/usr/bin/perl
2
3 # Copyright 2013 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use Test::More tests => 4;
24
25 BEGIN {
26         use_ok('Koha::MetadataRecord');
27 }
28
29 my $marcrecord = MARC::Record->new;
30
31 $marcrecord->add_fields(
32         [ '001', '1234' ],
33         [ '150', ' ', ' ', a => 'Cooking' ],
34         [ '450', ' ', ' ', a => 'Cookery', z => 'Instructional manuals' ],
35         );
36 my $record = Koha::MetadataRecord->new({ 'record' => $marcrecord, 'schema' => 'marc21' });
37
38 is(ref($record), 'Koha::MetadataRecord', 'Created valid Koha::MetadataRecord object');
39
40 my $samplehash = [
41     {
42         'field' => [
43             {
44                 'value' => '1234',
45                 'tag'   => '001',
46             }
47         ]
48     },
49     {
50         'field' => [
51             {
52                 'subfield' => [
53                     {
54                         'value'  => 'Cooking',
55                         'subtag' => 'a'
56                     }
57                 ],
58                 'indicator2' => ' ',
59                 'tag'        => 150,
60                 'indicator1' => ' ',
61             }
62         ]
63     },
64     {
65         'field' => [
66             {
67                 'subfield' => [
68                     {
69                         'value'  => 'Cookery',
70                         'subtag' => 'a'
71                     },
72                     {
73                         'value' => 'Instructional manuals',
74                         'subtag' => 'z'
75                     }
76                 ],
77                 'indicator2' => ' ',
78                 'tag'        => 450,
79                 'indicator1' => ' ',
80             }
81         ]
82     }
83 ];
84
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'}}++;
92         }
93     }
94 }
95
96 is_deeply($hash, $samplehash, 'Generated hash correctly');
97 my $dupkeys = grep { $_ > 1 } values %fieldkeys;
98 is($dupkeys, 0, 'No duplicate keys');