|
Lines 17-29
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 1; |
20 |
use File::Temp qw|tempfile|; |
|
|
21 |
use MARC::Field; |
| 22 |
use MARC::File::XML; |
| 23 |
use MARC::Record; |
| 24 |
use Test::More tests => 3; |
| 21 |
use t::lib::Mocks; |
25 |
use t::lib::Mocks; |
| 22 |
|
26 |
|
| 23 |
BEGIN { |
27 |
BEGIN { |
| 24 |
# Mock the DB connexion and C4::Context |
|
|
| 25 |
my $context = t::lib::Mocks::mock_dbh; |
| 26 |
use_ok('C4::ImportBatch'); |
28 |
use_ok('C4::ImportBatch'); |
| 27 |
} |
29 |
} |
| 28 |
|
30 |
|
| 29 |
1; |
31 |
t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); |
|
|
32 |
|
| 33 |
subtest 'RecordsFromISO2709File' => sub { |
| 34 |
plan tests => 4; |
| 35 |
|
| 36 |
my ( $errors, $recs ); |
| 37 |
my $file = create_file({ whitespace => 1, format => 'marc' }); |
| 38 |
( $errors, $recs ) = C4::ImportBatch::RecordsFromISO2709File( $file, 'biblio', 'UTF-8' ); |
| 39 |
is( @$recs, 0, 'No records from empty marc file' ); |
| 40 |
|
| 41 |
$file = create_file({ garbage => 1, format => 'marc' }); |
| 42 |
( $errors, $recs ) = C4::ImportBatch::RecordsFromISO2709File( $file, 'biblio', 'UTF-8' ); |
| 43 |
is( @$recs, 1, 'Garbage returns one record' ); |
| 44 |
my @fields = @$recs? $recs->[0]->fields: (); |
| 45 |
is( @fields, 0, 'That is an empty record' ); |
| 46 |
|
| 47 |
$file = create_file({ two => 1, format => 'marc' }); |
| 48 |
( $errors, $recs ) = C4::ImportBatch::RecordsFromISO2709File( $file, 'biblio', 'UTF-8' ); |
| 49 |
is( @$recs, 2, 'File contains 2 records' ); |
| 50 |
|
| 51 |
}; |
| 52 |
|
| 53 |
subtest 'RecordsFromMARCXMLFile' => sub { |
| 54 |
plan tests => 3; |
| 55 |
|
| 56 |
my ( $errors, $recs ); |
| 57 |
my $file = create_file({ whitespace => 1, format => 'marcxml' }); |
| 58 |
( $errors, $recs ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, 'biblio', 'UTF-8' ); |
| 59 |
is( @$recs, 0, 'No records from empty marcxml file' ); |
| 60 |
|
| 61 |
$file = create_file({ garbage => 1, format => 'marcxml' }); |
| 62 |
( $errors, $recs ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, 'biblio', 'UTF-8' ); |
| 63 |
is( @$recs, 0, 'Garbage returns no records' ); |
| 64 |
|
| 65 |
$file = create_file({ two => 1, format => 'marcxml' }); |
| 66 |
( $errors, $recs ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, 'biblio', 'UTF-8' ); |
| 67 |
is( @$recs, 2, 'File has two records' ); |
| 68 |
|
| 69 |
}; |
| 70 |
|
| 71 |
sub create_file { |
| 72 |
my ( $params ) = @_; |
| 73 |
my ( $fh, $name ) = tempfile( SUFFIX => '.' . $params->{format} ); |
| 74 |
if( $params->{garbage} ) { |
| 75 |
print $fh "Just some garbage\n\nAnd another line"; |
| 76 |
} elsif( $params->{whitespace} ) { |
| 77 |
print $fh " "; |
| 78 |
} elsif ( $params->{two} ) { |
| 79 |
my $rec1 = MARC::Record->new; |
| 80 |
my $rec2 = MARC::Record->new; |
| 81 |
my $fld1 = MARC::Field->new('245','','','a','Title1'); |
| 82 |
my $fld2 = MARC::Field->new('245','','','a','Title2'); |
| 83 |
$rec1->append_fields( $fld1 ); |
| 84 |
$rec2->append_fields( $fld2 ); |
| 85 |
if( $params->{format} eq 'marcxml' ) { |
| 86 |
my $str = $rec1->as_xml; |
| 87 |
# remove ending collection tag |
| 88 |
$str =~ s/<\/collection>//; |
| 89 |
print $fh $str; |
| 90 |
$str = $rec2->as_xml_record; # no collection tag |
| 91 |
# remove <?xml> line from 2nd record, add collection |
| 92 |
$str =~ s/<\?xml.*\n//; |
| 93 |
$str .= '</collection>'; |
| 94 |
print $fh $str; |
| 95 |
} else { |
| 96 |
print $fh $rec1->as_formatted, "\x1D", $rec2->as_formatted; |
| 97 |
} |
| 98 |
} |
| 99 |
close $fh; |
| 100 |
return $name; |
| 101 |
} |
| 30 |
- |
|
|