From 5ed200a27ff6c6798a302fe51b4d663ff925f8fb Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 16 Apr 2019 17:57:57 +0200 Subject: [PATCH] Bug 18823: add unit tests for added/changed subroutines --- C4/ImportBatch.pm | 20 +++++-- t/db_dependent/Biblio/MarcTimestamp.t | 99 +++++++++++++++++++++++++++++++++++ t/db_dependent/ImportBatch.t | 35 ++++++++++++- 3 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 t/db_dependent/Biblio/MarcTimestamp.t diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index b2d53ae..7ca7384 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -1608,6 +1608,16 @@ sub _create_import_record { return $import_record_id; } +=head2 _get_import_record_timestamp + + my $timestamp = C4::ImportBatch::_get_import_record_timestamp($record); + +Get the date in field 005 and return it formatted as a timestamp. + +The argument is a C object + +=cut + sub _get_import_record_timestamp { my ( $marc_record ) = @_; my $upload_timestamp = DateTime->now(); @@ -1616,12 +1626,12 @@ sub _get_import_record_timestamp { # tenth-of-a-second ourselves. my $f005 = $marc_record->field('005'); if ($f005 && $f005->data =~ /(\d{8}\d{6})\.(\d)/ ) { - my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S' ); - my $parsed_timestamp = $parser->parse_datetime($1); + my $parser = DateTime::Format::Strptime->new( pattern => '%Y%m%d%H%M%S' ); + my $parsed_timestamp = $parser->parse_datetime($1); - # We still check for success because we only did enough validation above to extract the - # tenth-of-a-second; the timestamp could still be some nonsense like the 50th of Jantober. - if ( $parsed_timestamp ) { + # We still check for success because we only did enough validation above to extract the + # tenth-of-a-second; the timestamp could still be some nonsense like the 50th of Jantober. + if ( $parsed_timestamp ) { $parsed_timestamp->set_nanosecond( $2 * 100_000_000 ); $upload_timestamp = $parsed_timestamp; } diff --git a/t/db_dependent/Biblio/MarcTimestamp.t b/t/db_dependent/Biblio/MarcTimestamp.t new file mode 100644 index 0000000..8fb2b1f --- /dev/null +++ b/t/db_dependent/Biblio/MarcTimestamp.t @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . +# +use Modern::Perl; + +use Test::More tests => 5; + +use t::lib::TestBuilder; +use t::lib::Mocks; +use MARC::Record; +use C4::Biblio; +use C4::ImportBatch; +use DateTime; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new(); + +t::lib::Mocks::mock_preference( 'marcflavour', 'NORMARC' ); + +my $record = MARC::Record->new; +$record->append_fields( + MARC::Field->new( '100', '', '', a => 'My title' ), + MARC::Field->new( '005', '20140508101202.0' ), +); + +C4::Biblio::UpdateMarcTimestamp( $record ); + +my $f005 = $record->field('005')->data; + +is($f005, '20140508101202.0', "Field 005 didn't change because of the normarc marc flavour."); + +t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + +my $dt = DateTime->now; +my $expected = $dt->ymd(''); + +C4::Biblio::UpdateMarcTimestamp( $record ); + +$f005 = $record->field('005')->data; +ok($f005 =~ /^$expected\d{6}\.\d/); + +$record->field('005')->update('20140508101202.0'); + +my $timestamp = C4::ImportBatch::_get_import_record_timestamp($record); + +is($timestamp, '2014-05-08T10:12:02', "Get correct record's timestamp"); + +my $import_batch = $builder->build( + { + source => 'ImportBatch', + } +); + +my $import_record_id = C4::ImportBatch::_create_import_record( + $import_batch->{import_batch_id}, + 1, $record, 'biblio', 'UTF-8', +); + +$timestamp = get_import_record_timestamp($import_record_id); +ok($f005 =~ /^$expected\d{6}\.\d/); + + +C4::ImportBatch::_update_import_record_marc($import_record_id, $record); + +$timestamp = get_import_record_timestamp($import_record_id); +is($timestamp, '2014-05-08 10:12:02', "import_records.upload_timestamp correctly updated."); + + +sub get_import_record_timestamp { + my $import_record_id = shift; + + my $dbh = C4::Context->dbh; + my $timestamp = $dbh->selectrow_array(q| + SELECT upload_timestamp + FROM import_records + WHERE import_record_id = ? + |, undef, $import_record_id ); + + return $timestamp; +} + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/ImportBatch.t b/t/db_dependent/ImportBatch.t index f42e0d8..90571c2 100644 --- a/t/db_dependent/ImportBatch.t +++ b/t/db_dependent/ImportBatch.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 14; +use Test::More tests => 15; use File::Basename; use File::Temp qw/tempfile/; @@ -198,4 +198,37 @@ subtest "RecordsFromMarcPlugin" => sub { 'Checked one field in second record' ); }; +subtest "add_biblio_fields to import record" => sub { + +my $record = MARC::Record->new; + plan tests => 4; + + $record->append_fields( + MARC::Field->new( '100', '', '', a => 'My author' ), + MARC::Field->new( '245', '', '', a => 'My title' ), + MARC::Field->new( '001', '111' ), + ); + + my $import_batch = $builder->build( + { + source => 'ImportBatch', + } + ); + + my $import_record_id = C4::ImportBatch::_create_import_record( + $import_batch->{import_batch_id}, + 1, $record, 'biblio', 'UTF-8', + ); + + my $import_biblios = GetImportBiblios($import_record_id); + is(scalar(@$import_biblios), 0, 'No import_biblios yet'); + + C4::ImportBatch::_add_biblio_fields($import_record_id, $record); + + $import_biblios = GetImportBiblios($import_record_id); + is($import_biblios->[0]{title}, 'My title', 'Import biblio title'); + is($import_biblios->[0]{author}, 'My author', 'Import biblio author'); + is($import_biblios->[0]{control_number}, 111, 'Import biblio control number'); +}; + $schema->storage->txn_rollback; -- 2.7.4