| 1 | use strict; |
| 2 | use warnings; |
| 3 | use Test::More; |
| 4 | use MARC::Schema; |
| 5 | |
| 6 | # load default schema |
| 7 | { |
| 8 | my $schema = MARC::Schema->new(); |
| 9 | isa_ok $schema, 'MARC::Schema'; |
| 10 | can_ok $schema, qw(check _initialize _load_schema); |
| 11 | ok $schema->{fields}, 'load default schema'; |
| 12 | ok $schema->{fields}->{LDR}, 'got a schema definition'; |
| 13 | is $schema->{fields}->{LDR}->{positions}->{'00-04'}->{label}, |
| 14 | 'Record length', 'got a schema property \'label\''; |
| 15 | } |
| 16 | |
| 17 | # load inline schema |
| 18 | { |
| 19 | my $schema = MARC::Schema->new( |
| 20 | { |
| 21 | fields => { |
| 22 | LDR => { |
| 23 | positions => |
| 24 | [{position => '00-04', label => 'Record length'}], |
| 25 | repeatable => 0, |
| 26 | }, |
| 27 | '001' => {label => 'Control Number', repeatable => 0} |
| 28 | } |
| 29 | } |
| 30 | ); |
| 31 | ok $schema->{fields}, 'load inline schema'; |
| 32 | ok $schema->{fields}->{'LDR'}, 'got a schema definition for LDR'; |
| 33 | ok $schema->{fields}->{'001'}, q{got a schema definition for tag '001'}; |
| 34 | is $schema->{fields}->{'001'}->{label}, 'Control Number', |
| 35 | 'got schema property \'label\''; |
| 36 | } |
| 37 | |
| 38 | # validate record |
| 39 | |
| 40 | { |
| 41 | my $record = { |
| 42 | _id => 'fol05865967', |
| 43 | record => [ |
| 44 | ['LDR', undef, undef, '_', '00661nam 22002538a 4500'], |
| 45 | ['001', undef, undef, '_', 'fol05865967'], |
| 46 | ['001', undef, undef, '_', 'fol05865967'], |
| 47 | ['016', undef, undef, 'a', '730032015'], |
| 48 | ['016', undef, undef, 'a', '84074272XE'], |
| 49 | [ |
| 50 | '245', '1', |
| 51 | '0', 'a', |
| 52 | 'Programming Perl /', 'c', |
| 53 | 'Larry Wall, Tom Christiansen & Jon Orwant.', 'a', |
| 54 | 'subfield is not repeatable', 'x', |
| 55 | 'unknown subfield', '8', |
| 56 | '1', '8', |
| 57 | '2', |
| 58 | ], |
| 59 | ['130', 'a', 'x', 'a', 'test'], |
| 60 | ['240', '1', 'x', 'a', 'test'], |
| 61 | ['999', undef, undef, '_', 'not a standard field'], |
| 62 | ] |
| 63 | }; |
| 64 | |
| 65 | my $schema = MARC::Schema->new(); |
| 66 | my @check = $schema->check($record); |
| 67 | ok @check > 0, 'got errors'; |
| 68 | is $check[0]->{error}, 'field is not repeatable', |
| 69 | 'field is not repeatable'; |
| 70 | is $check[0]->{tag}, '001', 'got field tag'; |
| 71 | is $check[0]->{type}, 'field', 'got error type'; |
| 72 | is $check[1]->{error}, 'subfield is not repeatable', |
| 73 | 'subfield is not repeatable'; |
| 74 | is $check[1]->{value}, 'a', 'got subfield code'; |
| 75 | is $check[1]->{type}, 'subfield', 'got error type'; |
| 76 | is $check[2]->{error}, 'unknown subfield', 'unknown subfield'; |
| 77 | is $check[3]->{error}, 'unknown first indicator', |
| 78 | 'unknown first indicator'; |
| 79 | is $check[3]->{value}, 'a', 'got first indicator code'; |
| 80 | is $check[3]->{type}, 'indicator', 'got error type'; |
| 81 | is $check[4]->{error}, 'unknown second indicator', |
| 82 | 'unknown second indicator'; |
| 83 | is $check[5]->{error}, 'unknown field', 'unknown field'; |
| 84 | is $check[5]->{type}, 'field', 'got error type'; |
| 85 | } |
| 86 | |
| 87 | done_testing; |