From 8513c24c33876a5dfcecf5950dfd74fb0dd63b67 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 8 Jul 2016 13:17:36 +0200 Subject: [PATCH] Bug 10407: Add two subtests to t/ImportBatch.t Creates a few simple tests for the two new subroutines in ImportBatch.pm. Test plan: Run t/ImportBatch.t. Signed-off-by: Marcel de Rooy Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- t/ImportBatch.t | 80 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/t/ImportBatch.t b/t/ImportBatch.t index 70a40afe97..9db2c279d9 100644 --- a/t/ImportBatch.t +++ b/t/ImportBatch.t @@ -17,13 +17,85 @@ use Modern::Perl; -use Test::More tests => 1; +use File::Temp qw|tempfile|; +use MARC::Field; +use MARC::File::XML; +use MARC::Record; +use Test::More tests => 3; use t::lib::Mocks; BEGIN { - # Mock the DB connexion and C4::Context - my $context = t::lib::Mocks::mock_dbh; use_ok('C4::ImportBatch'); } -1; +t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); + +subtest 'RecordsFromISO2709File' => sub { + plan tests => 4; + + my ( $errors, $recs ); + my $file = create_file({ whitespace => 1, format => 'marc' }); + ( $errors, $recs ) = C4::ImportBatch::RecordsFromISO2709File( $file, 'biblio', 'UTF-8' ); + is( @$recs, 0, 'No records from empty marc file' ); + + $file = create_file({ garbage => 1, format => 'marc' }); + ( $errors, $recs ) = C4::ImportBatch::RecordsFromISO2709File( $file, 'biblio', 'UTF-8' ); + is( @$recs, 1, 'Garbage returns one record' ); + my @fields = @$recs? $recs->[0]->fields: (); + is( @fields, 0, 'That is an empty record' ); + + $file = create_file({ two => 1, format => 'marc' }); + ( $errors, $recs ) = C4::ImportBatch::RecordsFromISO2709File( $file, 'biblio', 'UTF-8' ); + is( @$recs, 2, 'File contains 2 records' ); + +}; + +subtest 'RecordsFromMARCXMLFile' => sub { + plan tests => 3; + + my ( $errors, $recs ); + my $file = create_file({ whitespace => 1, format => 'marcxml' }); + ( $errors, $recs ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, 'UTF-8' ); + is( @$recs, 0, 'No records from empty marcxml file' ); + + $file = create_file({ garbage => 1, format => 'marcxml' }); + ( $errors, $recs ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, 'UTF-8' ); + is( @$recs, 0, 'Garbage returns no records' ); + + $file = create_file({ two => 1, format => 'marcxml' }); + ( $errors, $recs ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, 'UTF-8' ); + is( @$recs, 2, 'File has two records' ); + +}; + +sub create_file { + my ( $params ) = @_; + my ( $fh, $name ) = tempfile( SUFFIX => '.' . $params->{format} ); + if( $params->{garbage} ) { + print $fh "Just some garbage\n\nAnd another line"; + } elsif( $params->{whitespace} ) { + print $fh " "; + } elsif ( $params->{two} ) { + my $rec1 = MARC::Record->new; + my $rec2 = MARC::Record->new; + my $fld1 = MARC::Field->new('245','','','a','Title1'); + my $fld2 = MARC::Field->new('245','','','a','Title2'); + $rec1->append_fields( $fld1 ); + $rec2->append_fields( $fld2 ); + if( $params->{format} eq 'marcxml' ) { + my $str = $rec1->as_xml; + # remove ending collection tag + $str =~ s/<\/collection>//; + print $fh $str; + $str = $rec2->as_xml_record; # no collection tag + # remove line from 2nd record, add collection + $str =~ s/<\?xml.*\n//; + $str .= ''; + print $fh $str; + } else { + print $fh $rec1->as_formatted, "\x1D", $rec2->as_formatted; + } + } + close $fh; + return $name; +} -- 2.39.5