| 1 |
#!perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More; |
| 6 |
use Test::Exception; |
| 7 |
|
| 8 |
my $pkg; |
| 9 |
|
| 10 |
BEGIN { |
| 11 |
$pkg = 'Catmandu::Fix::marc_xml'; |
| 12 |
use_ok $pkg; |
| 13 |
} |
| 14 |
|
| 15 |
require_ok $pkg; |
| 16 |
|
| 17 |
my $mrc = <<'MRC'; |
| 18 |
<?xml version="1.0" encoding="UTF-8"?> |
| 19 |
<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"> |
| 20 |
<marc:record> |
| 21 |
<marc:controlfield tag="001"> 92005291 </marc:controlfield> |
| 22 |
<marc:datafield ind1="1" ind2="0" tag="245"> |
| 23 |
<marc:subfield code="a">Title / </marc:subfield> |
| 24 |
<marc:subfield code="c">Name</marc:subfield> |
| 25 |
</marc:datafield> |
| 26 |
<marc:datafield ind1=" " ind2=" " tag="998"> |
| 27 |
<marc:subfield code="a">X</marc:subfield> |
| 28 |
<marc:subfield code="a">Y</marc:subfield> |
| 29 |
<marc:subfield code="b">Z</marc:subfield> |
| 30 |
</marc:datafield> |
| 31 |
<marc:datafield ind1=" " ind2=" " tag="999"> |
| 32 |
<marc:subfield code="a">X</marc:subfield> |
| 33 |
<marc:subfield code="a">Y</marc:subfield> |
| 34 |
</marc:datafield> |
| 35 |
<marc:datafield ind1=" " ind2=" " tag="999"> |
| 36 |
<marc:subfield code="a">Z</marc:subfield> |
| 37 |
</marc:datafield> |
| 38 |
</marc:record> |
| 39 |
</marc:collection> |
| 40 |
MRC |
| 41 |
|
| 42 |
note 'marc_xml(record)'; |
| 43 |
{ |
| 44 |
my $importer = Catmandu->importer( |
| 45 |
'MARC', |
| 46 |
file => \$mrc, |
| 47 |
type => 'XML', |
| 48 |
fix => 'marc_xml(record)' |
| 49 |
); |
| 50 |
my $record = $importer->first; |
| 51 |
|
| 52 |
like $record->{record} , qr/^<marc:record/, 'ok match'; |
| 53 |
} |
| 54 |
|
| 55 |
note 'marc_xml(record2)'; |
| 56 |
{ |
| 57 |
my $importer = Catmandu->importer( |
| 58 |
'MARC', |
| 59 |
file => \$mrc, |
| 60 |
type => 'XML', |
| 61 |
fix => 'copy_field(record,record2); marc_xml(record2)' |
| 62 |
); |
| 63 |
my $record = $importer->first; |
| 64 |
|
| 65 |
like $record->{record2} , qr/^<marc:record/, 'ok match'; |
| 66 |
|
| 67 |
is $record->{record}->[0]->[0] , 'LDR' , 'still have a record'; |
| 68 |
} |
| 69 |
|
| 70 |
note 'marc_xml(record2)'; |
| 71 |
{ |
| 72 |
my $importer = Catmandu->importer( |
| 73 |
'MARC', |
| 74 |
file => \$mrc, |
| 75 |
type => 'XML', |
| 76 |
fix => 'move_field(record,record2); marc_xml(record2)' |
| 77 |
); |
| 78 |
my $record = $importer->first; |
| 79 |
|
| 80 |
like $record->{record2} , qr/^<marc:record/, 'ok match'; |
| 81 |
|
| 82 |
ok ! $record->{record} , 'still have a record'; |
| 83 |
} |
| 84 |
|
| 85 |
note 'marc_xml(record,reverse:1)'; |
| 86 |
{ |
| 87 |
my $fixer = Catmandu::Fix->new(fixes => |
| 88 |
[q|marc_xml(record,reverse:1)|] |
| 89 |
); |
| 90 |
my $result = $fixer->fix({ record => $mrc }); |
| 91 |
|
| 92 |
ok $result , 'got a result'; |
| 93 |
|
| 94 |
is ref($result->{record}) , 'ARRAY' , 'got an array'; |
| 95 |
|
| 96 |
is $result->{record}->[0]->[0], 'LDR' , 'smells like marc'; |
| 97 |
} |
| 98 |
|
| 99 |
done_testing; |