Debian Codegraph @

t/Catmandu/Exporter/MARC/XML.t

raw
1 #!perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7 use Catmandu::Exporter::MARC;
8 use XML::XPath;
9 use XML::LibXML;
10
11 my $pkg;
12
13 BEGIN {
14 $pkg = 'Catmandu::Exporter::MARC::XML';
15 use_ok $pkg;
16 }
17
18 require_ok $pkg;
19
20 my $xml = undef;
21
22 my $exporter = Catmandu::Exporter::MARC->new(file => \$xml, type=> 'XML' , collection => 0);
23
24 ok $exporter , 'got an MARC/XML exporter';
25
26 ok $exporter->add({
27 record => [
28 ['001', undef, undef, undef, 'rec001'],
29 ['100', ' ', ' ', 'a', 'Davis, Miles'],
30 ['245', ' ', ' ',
31 'a', 'Sketches in Blue' ,
32 ],
33 ['500', ' ', ' ', 'a', undef],
34 ['501', ' ', ' ' ],
35 ['502', ' ', ' ', 'a', undef, 'b' , 'ok'],
36 ['503', ' ', ' ', 'a', ''],
37 ['504', ' ', ' ', '=', 'test'],
38 ]
39 });
40
41 ok $exporter->commit;
42
43 my $xp;
44 ok($xp = XML::XPath->new(xml => $xml), "parse XML");
45 ok($xp->findvalue('/marc:record/marc:controlfield[@tag="001"]') eq 'rec001','test 001');
46 ok($xp->findvalue('/marc:record/marc:datafield[@tag="245"]/marc:subfield[@code="a"]') eq 'Sketches in Blue','test 245');
47 ok(! $xp->exists('/marc:record/marc:datafield[@tag="500"]') ,'skipped 500 - only empty subfields');
48 ok(! $xp->exists('/marc:record/marc:datafield[@tag="501"]') ,'skipped 501 - no subfields');
49 ok(! $xp->exists('/marc:record/marc:datafield[@tag="502"]/marc:subfield[@code="a"]') ,'skipped 502a - empty subfields');
50 ok(! $xp->exists('/marc:record/marc:datafield[@tag="503"]/marc:subfield[@code="a"]') ,'skipped 503a - empty subfields');
51 ok($xp->exists('/marc:record/marc:datafield[@tag="504"]/marc:subfield[@code="="]') ,'found 504= - special subfields');
52
53 $xml = undef;
54
55 $exporter = Catmandu::Exporter::MARC->new(file => \$xml, type=> 'XML', record_format => 'MARC-in-JSON', collection => 0);
56
57 ok($exporter, "create exporter MARC-in-JSON");
58
59 ok $exporter->add({
60 fields => [
61 { '001' => 'rec001' } ,
62 { '100' => { 'subfields' => [ { 'a' => 'Davis, Miles'}], 'ind1' => ' ', 'ind2' => ' '}} ,
63 { '245' => { 'subfields' => [ { 'a' => 'Sketches in Blue'}], 'ind1' => ' ', 'ind2' => ' '}} ,
64 { '500' => { 'subfields' => [ { 'a' => undef }] , 'ind1' => ' ', 'ind2' => ' '}} ,
65 { '501' => { 'ind1' => ' ', 'ind2' => ' ' }} ,
66 { '502' => { 'subfields' => [ { 'a' => undef} , { 'b' , 'ok' } ] , 'ind1' => ' ', 'ind2' => ' ' } } ,
67 { '503' => { 'subfields' => [ { 'a' => '' }] , 'ind1' => ' ', 'ind2' => ' '}} ,
68 { '504' => { 'subfields' => [ { '=' => 'test' }] , 'ind1' => ' ', 'ind2' => ' '}} ,
69 ]
70 });
71
72 ok $exporter->commit;
73
74 ok($xp = XML::XPath->new(xml => $xml), "parse XML");
75 ok($xp->findvalue('/marc:record/marc:controlfield[@tag="001"]') eq 'rec001','test 001');
76 ok($xp->findvalue('/marc:record/marc:datafield[@tag="245"]/marc:subfield[@code="a"]') eq 'Sketches in Blue','test 245');
77 ok(! $xp->exists('/marc:record/marc:datafield[@tag="500"]') ,'skipped 500 - only empty subfields');
78 ok(! $xp->exists('/marc:record/marc:datafield[@tag="501"]') ,'skipped 501 - no subfields');
79 ok(! $xp->exists('/marc:record/marc:datafield[@tag="502"]/marc:subfield[@code="a"]') ,'skipped 502a - empty subfields');
80 ok(! $xp->exists('/marc:record/marc:datafield[@tag="503"]/marc:subfield[@code="a"]') ,'skipped 503a - empty subfields');
81 ok($xp->exists('/marc:record/marc:datafield[@tag="504"]/marc:subfield[@code="="]') ,'found 504= - special subfields');
82
83 {
84 my $record = {
85 record => [
86 ['001', undef, undef, undef, 'rec002'],
87 ['100', ' ', ' ', 'a', 'Slayer'],
88 ['245', ' ', ' ',
89 'a', 'Reign in Blood' ,
90 ]
91 ]
92 };
93
94 # XML exporter with default arguments
95 my $xml = undef;
96 my $exporter = Catmandu::Exporter::MARC->new(file => \$xml, type => 'XML');
97 $exporter->add($record);
98 $exporter->commit;
99 my $dom = XML::LibXML->load_xml( string => $xml );
100 ok($dom->version() eq '1.0', 'document version');
101 ok($dom->encoding() eq 'UTF-8', 'document encoding');
102 my $root = $dom->documentElement();
103 ok($root->localname eq 'collection', 'root collection');
104 ok($root->prefix eq 'marc', 'namespace prefix');
105
106
107 # XML exporter with arguments
108 $xml = undef;
109 $exporter = Catmandu::Exporter::MARC->new(file => \$xml, type => 'XML', collection => 0, xml_declaration => 1);
110 $exporter->add($record);
111 $exporter->commit;
112 $dom = XML::LibXML->load_xml( string => $xml );
113 ok($dom->version() eq '1.0', 'document version');
114 ok($dom->encoding() eq 'UTF-8', 'document encoding');
115 $root = $dom->documentElement();
116 ok($root->localname eq 'record', 'root record');
117 ok($root->prefix eq 'marc', 'namespace prefix');
118
119 # XML exporter with arguments
120
121 $xml = undef;
122 $exporter = Catmandu::Exporter::MARC->new(file => \$xml, type => 'XML' , collection => 1, xml_declaration => 0);
123 $exporter->add($record);
124 $exporter->commit;
125 $dom = XML::LibXML->load_xml( string => $xml );
126 $root = $dom->documentElement();
127 ok($root->localname eq 'collection', 'root collection');
128 ok($root->prefix eq 'marc', 'namespace prefix');
129 }
130
131 done_testing;