Skip to content

Commit

Permalink
Add Fix: pica_update (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Aug 11, 2023
1 parent 265a86c commit f469496
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog for Catmandu-PICA

{{$NEXT}}
- Add Fix: pica_update (#82)

1.15 2023-08-09T19:03:38Z
- Update PICA::Data to support parsing PICA Import format (#88)
Expand Down
3 changes: 2 additions & 1 deletion lib/Catmandu/Fix/pica_add.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ If PICA field does not exist, it will be created.
=head1 SEE ALSO
See L<Catmandu::Fix::pica_set> for setting a new value to an existing subfield.
See L<Catmandu::Fix::pica_set> and L<Catmandu::Fix:pica_update> for setting
new values to (sub)fields.
See L<Catmandu::Fix::pica_map> if you want to copy values from a PICA record.
Expand Down
5 changes: 3 additions & 2 deletions lib/Catmandu/Fix/pica_set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sub _build_fixer {
}
}
$data;
}
}
}

1;
Expand Down Expand Up @@ -76,7 +76,8 @@ This fix sets the value from PATH to a subfield defined through PICA_PATH.
=head1 SEE ALSO
See L<Catmandu::Fix::pica_add> for adding new fields and subfields to a PICA record.
See L<Catmandu::Fix::pica_update> and L<Catmandu::Fix::pica_add> for adding new
fields and subfields to a PICA record.
See L<Catmandu::Fix::pica_map> if you want to copy values from a PICA record.
Expand Down
2 changes: 2 additions & 0 deletions lib/Catmandu/PICA.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ introduction into Catmandu.
=item * L<Catmandu::Fix::Condition::pica_match>
=item * L<Catmandu::Fix::pica_update>
=item * L<Catmandu::Fix::pica_add>
=item * L<Catmandu::Fix::pica_set>
Expand Down
68 changes: 68 additions & 0 deletions t/09-pica-update.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use v5.14;
use Test::More;
use Test::Exception;

use Catmandu::Fix::pica_update as => 'pica_update';
use Catmandu::Importer::PICA;

my $record = { record => [
['021A', '', 'a', 'title'],
['021A', '', 'a', 'nother'],
] };

note "update/add full field";

pica_update($record, '021A','$abook$hto read', all => 0);
is_deeply $record->{record}, [
['021A', '', 'a', 'book', 'h', 'to read'],
['021A', '', 'a', 'nother'],
], 'update full field (all: 0)';

pica_update($record, '021A','$aX');
is_deeply $record->{record}, [
['021A', '', 'a', 'X'],
['021A', '', 'a', 'X'],
], 'update full field (all)';

pica_update($record, '003@', '$099');
is scalar @{$record->{record}}, 2, "don't add missing field by default";

pica_update($record, '003@', '$099', add => 1);
is scalar @{$record->{record}}, 3, "add missing field";

note "update/add subfield(s)";

my $record = { record => [] };

pica_update($record, '003@$0', '123');
is scalar @{$record->{record}}, 0, "update subfield (don't add field)";

pica_update($record, '003@$0', '123', add => 1);
is_deeply $record->{record}, [['003@', '', '0', '123']], "update subfield (add field)";

pica_update($record, '003@$*', '99');
is_deeply $record->{record}, [['003@', '', '0', '99']], "update subfield (existing)";

$record->{record} = [
['021A', '', 'a', 'A'],
['021A', '', 'a', 'B', 'h', 'C', 'h', 'D']];
pica_update($record, '021A$h', 'Z', add => 1, all => 0);

is_deeply $record->{record}, [
['021A', '', 'a', 'A', 'h', 'Z'],
['021A', '', 'a', 'B', 'h', 'Z', 'h', 'D'],
], 'add subfields (only first)';

pica_update($record, '021A$ah', 'Q', all => 1);
is_deeply $record->{record}, [
['021A', '', 'a', 'Q', 'h', 'Q'],
['021A', '', 'a', 'Q', 'h', 'Q', 'h', 'Q'],
], 'update subfields (all)';

note "exceptions";

throws_ok { pica_update($record, '021A', '$' ) } qr/invalid/, 'invalid PICA field value';
throws_ok { pica_update($record, '021A', 'a' ) } qr/invalid/, 'invalid PICA field value';
throws_ok { pica_update($record, '021A', '' ) } qr/invalid/, 'missing PICA field value';

done_testing;

0 comments on commit f469496

Please sign in to comment.