[libcatmandu-marc-perl] 202/208: Fixing tests for marc_all_match and marc_any_match

Jonas Smedegaard dr at jones.dk
Sat Oct 28 03:42:50 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag upstream/1.19
in repository libcatmandu-marc-perl.

commit 8a2efd31090ddb7dbfa6bed4086077adb1393aae
Author: Patrick Hochstenbach <patrick.hochstenbach at ugent.be>
Date:   Wed Sep 27 15:40:53 2017 +0200

    Fixing tests for marc_all_match and marc_any_match
---
 lib/Catmandu/Fix/Condition/marc_all_match.pm |  6 +++
 lib/Catmandu/Fix/Condition/marc_any_match.pm |  6 +++
 lib/Catmandu/Fix/Condition/marc_match.pm     | 65 +++++++++++++---------------
 t/Catmandu/Fix/Condition/marc_all_match.t    | 46 ++++++++++++++++++++
 t/Catmandu/Fix/Condition/marc_any_match.t    | 59 +++++++++++++++++++++++++
 t/Catmandu/Fix/Condition/marc_match.t        | 47 ++++++++++++++++++++
 6 files changed, 194 insertions(+), 35 deletions(-)

diff --git a/lib/Catmandu/Fix/Condition/marc_all_match.pm b/lib/Catmandu/Fix/Condition/marc_all_match.pm
index bd85277..4b8ff5e 100644
--- a/lib/Catmandu/Fix/Condition/marc_all_match.pm
+++ b/lib/Catmandu/Fix/Condition/marc_all_match.pm
@@ -60,6 +60,12 @@ Catmandu::Fix::Condition::marc_all_match - Test if a MARC (sub)field matches a v
      add_field('has_digits','true')
    end
 
+   # Match if /one/ of the 650 fields contain digits
+   do marc_each()
+       if marc_all_match('650','[0-9]')
+         add_field('has_digits','true')
+       end
+   end
 =head1 DESCRIPTION
 
 Evaluate the enclosing fixes only if the MARC (sub)field matches a
diff --git a/lib/Catmandu/Fix/Condition/marc_any_match.pm b/lib/Catmandu/Fix/Condition/marc_any_match.pm
index ba93ec1..55e2e20 100644
--- a/lib/Catmandu/Fix/Condition/marc_any_match.pm
+++ b/lib/Catmandu/Fix/Condition/marc_any_match.pm
@@ -60,6 +60,12 @@ Catmandu::Fix::Condition::marc_any_match - Test if a MARC (sub)field matches a v
      add_field('has_digits','true')
    end
 
+   # Match if /one/ of the 650 fields contain digits
+   do marc_each()
+       if marc_all_match('650','[0-9]')
+         add_field('has_digits','true')
+       end
+   end
 =head1 DESCRIPTION
 
 Evaluate the enclosing fixes only if the MARC (sub)field matches a
diff --git a/lib/Catmandu/Fix/Condition/marc_match.pm b/lib/Catmandu/Fix/Condition/marc_match.pm
index 32b9154..8ea21b5 100644
--- a/lib/Catmandu/Fix/Condition/marc_match.pm
+++ b/lib/Catmandu/Fix/Condition/marc_match.pm
@@ -1,41 +1,10 @@
 package Catmandu::Fix::Condition::marc_match;
-use Catmandu::Sane;
-use Catmandu::Fix::marc_map;
-use Catmandu::Fix::Condition::any_match;
-use Catmandu::Fix::set_field;
-use Catmandu::Fix::remove_field;
+
 use Moo;
-use Catmandu::Fix::Has;
 
 our $VERSION = '1.18';
 
-with 'Catmandu::Fix::Condition';
-
-has marc_path  => (fix_arg => 1);
-has value      => (fix_arg => 1);
-
-sub emit {
-    my ($self,$fixer,$label) = @_;
-
-    my $perl;
-
-    my $tmp_var  = '_tmp_' . int(rand(9999));
-    my $marc_map = Catmandu::Fix::marc_map->new($self->marc_path , "$tmp_var.\$append");
-    $perl .= $marc_map->emit($fixer,$label);
-
-    my $any_match = Catmandu::Fix::Condition::any_match->new("$tmp_var.*",$self->value);
-    my $remove_field = Catmandu::Fix::remove_field->new($tmp_var);
-
-    my $pass_fixes = $self->pass_fixes;
-    my $fail_fixes = $self->fail_fixes;
-
-    $any_match->pass_fixes([ $remove_field , @$pass_fixes ]);
-    $any_match->fail_fixes([ $remove_field , @$fail_fixes ]);
-
-    $perl .= $any_match->emit($fixer,$label);
-
-    $perl;
-}
+extends 'Catmandu::Fix::Condition::marc_all_match';
 
 =head1 NAME
 
@@ -45,14 +14,40 @@ Catmandu::Fix::Condition::marc_match - Test if a MARC (sub)field matches a value
 
    # marc_match(MARC_PATH,REGEX)
 
+   # Match if 245 contains the value "My funny title"
    if marc_match('245','My funny title')
    	add_field('my.funny.title','true')
    end
 
+   # Match if 245a contains the value "My funny title"
+   if marc_match('245a','My funny title')
+   	add_field('my.funny.title','true')
+   end
+
+   # Match if all 650 fields contain digits
+   if marc_match('650','[0-9]')
+     add_field('has_digits','true')
+   end
+
+   # Match if /one/   # Match if one of the 650 fields contain digits
+      do marc_each()
+          if marc_all_match('650','[0-9]')
+            add_field('has_digits','true')
+          end
+      end of the 650 fields contain digits
+   do marc_each()
+       if marc_match('650','[0-9]')
+         add_field('has_digits','true')
+       end
+   end
 =head1 DESCRIPTION
 
 Evaluate the enclosing fixes only if the MARC (sub)field matches a
-regular expression.
+regular expression. When the MARC field is a repeated fiels, then all
+the MARC fields should match the regular expression.
+
+DEPRECATED: This condition is the same as L<Catmandu::Fix::Condition::marc_all_match>
+and will be deleted in the future
 
 =head1 METHODS
 
@@ -63,7 +58,7 @@ Evaluates to true when the MARC_PATH values matches the REGEX, false otherwise.
 =head1 SEE ALSO
 
 L<Catmandu::Fix::Condition::marc_all_match>,
-L<Catmandu::Fix::Condition::marc_any_match>, 
+L<Catmandu::Fix::Condition::marc_any_match>,
 
 =cut
 
diff --git a/t/Catmandu/Fix/Condition/marc_all_match.t b/t/Catmandu/Fix/Condition/marc_all_match.t
index abda08b..c55a134 100644
--- a/t/Catmandu/Fix/Condition/marc_all_match.t
+++ b/t/Catmandu/Fix/Condition/marc_all_match.t
@@ -14,4 +14,50 @@ BEGIN {
 
 require_ok $pkg;
 
+my $mrc = <<'MRC';
+<?xml version="1.0" encoding="UTF-8"?>
+<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">
+    <marc:record>
+        <marc:controlfield tag="001">   92005291 </marc:controlfield>
+        <marc:datafield ind1="1" ind2="0" tag="245">
+            <marc:subfield code="a">Title / </marc:subfield>
+            <marc:subfield code="c">Name</marc:subfield>
+        </marc:datafield>
+        <marc:datafield ind1=" " ind2=" " tag="999">
+            <marc:subfield code="a">X</marc:subfield>
+            <marc:subfield code="a">Y</marc:subfield>
+        </marc:datafield>
+        <marc:datafield ind1=" " ind2=" " tag="999">
+            <marc:subfield code="a">Z</marc:subfield>
+        </marc:datafield>
+    </marc:record>
+</marc:collection>
+MRC
+
+note 'marc_all_match(999,X)';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_all_match(999,X) add_field(test,failed) end'
+    );
+    my $record = $importer->first;
+
+    ok ! $record->{test} , 'ok matched nothing'
+}
+
+note 'marc_all_match(999,[XYZ])';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_all_match(999,[XYZ]) add_field(test,ok) end'
+    );
+    my $record = $importer->first;
+
+    is $record->{test} , 'ok' , 'ok matched everything'
+}
+
 done_testing;
diff --git a/t/Catmandu/Fix/Condition/marc_any_match.t b/t/Catmandu/Fix/Condition/marc_any_match.t
index 91063c1..72e4b5b 100644
--- a/t/Catmandu/Fix/Condition/marc_any_match.t
+++ b/t/Catmandu/Fix/Condition/marc_any_match.t
@@ -14,4 +14,63 @@ BEGIN {
 
 require_ok $pkg;
 
+my $mrc = <<'MRC';
+<?xml version="1.0" encoding="UTF-8"?>
+<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">
+    <marc:record>
+        <marc:controlfield tag="001">   92005291 </marc:controlfield>
+        <marc:datafield ind1="1" ind2="0" tag="245">
+            <marc:subfield code="a">Title / </marc:subfield>
+            <marc:subfield code="c">Name</marc:subfield>
+        </marc:datafield>
+        <marc:datafield ind1=" " ind2=" " tag="999">
+            <marc:subfield code="a">X</marc:subfield>
+            <marc:subfield code="a">Y</marc:subfield>
+        </marc:datafield>
+        <marc:datafield ind1=" " ind2=" " tag="999">
+            <marc:subfield code="a">Z</marc:subfield>
+        </marc:datafield>
+    </marc:record>
+</marc:collection>
+MRC
+
+note 'marc_any_match(999,A)';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_any_match(999,A) add_field(test,failed) end'
+    );
+    my $record = $importer->first;
+
+    ok ! $record->{test} , 'ok matched nothing';
+}
+
+note 'marc_any_match(999,X)';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_any_match(999,X) add_field(test,ok) end'
+    );
+    my $record = $importer->first;
+
+    is $record->{test} , 'ok' , 'ok matched something'
+}
+
+note 'marc_any_match(999,[XYZ])';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_any_match(999,[XYZ]) add_field(test,ok) end'
+    );
+    my $record = $importer->first;
+
+    is $record->{test} , 'ok' , 'ok matched something'
+}
+
 done_testing;
diff --git a/t/Catmandu/Fix/Condition/marc_match.t b/t/Catmandu/Fix/Condition/marc_match.t
index 9eafe77..d6fe42d 100644
--- a/t/Catmandu/Fix/Condition/marc_match.t
+++ b/t/Catmandu/Fix/Condition/marc_match.t
@@ -14,4 +14,51 @@ BEGIN {
 
 require_ok $pkg;
 
+my $mrc = <<'MRC';
+<?xml version="1.0" encoding="UTF-8"?>
+<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim">
+    <marc:record>
+        <marc:controlfield tag="001">   92005291 </marc:controlfield>
+        <marc:datafield ind1="1" ind2="0" tag="245">
+            <marc:subfield code="a">Title / </marc:subfield>
+            <marc:subfield code="c">Name</marc:subfield>
+        </marc:datafield>
+        <marc:datafield ind1=" " ind2=" " tag="999">
+            <marc:subfield code="a">X</marc:subfield>
+            <marc:subfield code="a">Y</marc:subfield>
+        </marc:datafield>
+        <marc:datafield ind1=" " ind2=" " tag="999">
+            <marc:subfield code="a">Z</marc:subfield>
+        </marc:datafield>
+    </marc:record>
+</marc:collection>
+MRC
+
+note 'marc_all_match(999,X)';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_all_match(999,X) add_field(test,failed) end'
+    );
+    my $record = $importer->first;
+
+    ok ! $record->{test} , 'ok matched nothing'
+}
+
+note 'marc_all_match(999,[XYZ])';
+{
+    my $importer = Catmandu->importer(
+        'MARC',
+        file => \$mrc,
+        type => 'XML',
+        fix  => 'if marc_all_match(999,[XYZ]) add_field(test,ok) end'
+    );
+    my $record = $importer->first;
+
+    is $record->{test} , 'ok' , 'ok matched everything'
+}
+
+
 done_testing;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-marc-perl.git



More information about the Pkg-perl-cvs-commits mailing list