Koha/t/db_dependent/Exporter/Record.t
Jonathan Druart 38f92df4e5 Bug 14722: Refactor the export tool
Why a refactoring was need for this script?
The export tool (tools/export.pl) can be called from the command line
and some parts of code were unnecessarity complicated (just look at the
code, you will understand).

Worse still, the script does not provide the same options for both
interface. For instance you cannot export records given a range of
biblionumbers, authids, callnumbers, etc. from the commandline.

What does this patch?
1/ Important: The script tools/export.pl does not work anymore if called from
the command line (should be in the release notes).
2/ The code used to generated a file (csv, iso2709 or xml) has been moved to a new
module (Koha::Exporter::Record) and tests have been provided.
3/ No change is done on the web interface
4/ Some new options have been added to the commandline script
(misc/export.pl):
    - starting_authid
    - ending_authid
    - authtype
    - starting_biblionumber
    - ending_biblionumber
    - itemtype
    - starting_callnumber
    - ending_callnumber
    - start_accession
    - end_accession
5/ There is a change in the behavior if an error occurs:
Can't call method "as_usmarc" on an undefined value at Koha/Exporter/Record.pm line 114.
record (number 5530) is invalid and therefore not exported because its reopening generates warnings above at Koha/Exporter/Record.pm line 117.

Before this patch, they were not displayed (using the command line).

What does not do this patch?
It does not provide the 'clean', 'timestamp' and 'deleted_barcodes' options to
the web interface (same as before).

What about the perfs?
With a DB with ~800 biblios (MARC21)
Before: perl tools/export.pl 14.79s user 0.83s system 71% cpu 21.905 total
After:  perl misc/export.pl  17.19s user 0.84s system 75% cpu 24.018 total

With a DB with ~6400 biblios (UNIMARC)
Before: perl tools/export.pl 26.55s user 0.76s system 76% cpu 35.498 total
After:  perl misc/export.pl  26.78s user 0.84s system 80% cpu 34.494 total

How to test this patch?
Test plan:
A. Web interface:
1/ On the current master, export some records, biblios and authorities (with
the 3 differents exports) playing with the different filters (item type,
libraries, callnumber, accession date, don't export items, remove
non-local items, don't export fields, etc.).
2/ Apply this patch, export again the same records, and compare the
generated files. They must be identical!
3/ Confirm that the export features on the checkout list
(circ/circulation.pl) works as before this patch.

B. The command line
1/ On the current master, export some records, biblios and authorities (with
the 2 differents exports) playing with the different options (date,
deleted_barcodes, clean).
2/ Apply this patch, export again the same records, and compare the
generated files. They must be identical!

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2015-10-27 17:01:28 -03:00

166 lines
5.7 KiB
Perl

use Modern::Perl;
use Test::More tests => 3;
use MARC::Record;
use MARC::File::USMARC;
use MARC::File::XML;# ( BinaryEncoding => 'utf-8' );
#use XML::Simple;
use MARC::Batch;
use t::lib::TestBuilder;
use File::Slurp;
#use utf8;
use Encode;
use C4::Biblio;
use C4::Context;
use Koha::Exporter::Record;
my $dbh = C4::Context->dbh;
#$dbh->{AutoCommit} = 0;
#$dbh->{RaiseError} = 1;
#$dbh->do(q|DELETE FROM issues|);
#$dbh->do(q|DELETE FROM reserves|);
#$dbh->do(q|DELETE FROM items|);
#$dbh->do(q|DELETE FROM biblio|);
#$dbh->do(q|DELETE FROM auth_header|);
my $biblio_1_title = 'Silence in the library';
#my $biblio_2_title = Encode::encode('UTF-8', 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é');
my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é';
my $biblio_1 = MARC::Record->new();
$biblio_1->leader('00266nam a22001097a 4500');
$biblio_1->append_fields(
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
MARC::Field->new('245', ' ', ' ', a => $biblio_1_title),
);
my ($biblionumber_1, $biblioitemnumber_1) = AddBiblio($biblio_1, '');
my $biblio_2 = MARC::Record->new();
$biblio_2->leader('00266nam a22001097a 4500');
$biblio_2->append_fields(
MARC::Field->new('100', ' ', ' ', a => 'Knuth, Donald Ervin'),
MARC::Field->new('245', ' ', ' ', a => $biblio_2_title),
);
my ($biblionumber_2, $biblioitemnumber_2) = AddBiblio($biblio_2, '');
my $builder = t::lib::TestBuilder->new;
my $item_1_1 = $builder->build({
source => 'Item',
value => {
biblionumber => $biblionumber_1,
more_subfields_xml => '',
}
});
my $item_1_2 = $builder->build({
source => 'Item',
value => {
biblionumber => $biblionumber_1,
more_subfields_xml => '',
}
});
my $item_2_1 = $builder->build({
source => 'Item',
value => {
biblionumber => $biblionumber_2,
more_subfields_xml => '',
}
});
subtest 'export csv' => sub {
plan tests => 2;
my $csv_content = q{Title=245$a|Barcode=952$p};
$dbh->do(q|INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)|, {}, "TEST_PROFILE_Records.t", "my useless desc", $csv_content, '|', ';', ',', 'utf8', 'marc');
my $csv_profile_id = $dbh->last_insert_id( undef, undef, 'export_format', undef );
my $generated_csv_file = '/tmp/test_export_1.csv';
# Get all item infos
Koha::Exporter::Record::export(
{
record_type => 'bibs',
record_ids => [ $biblionumber_1, $biblionumber_2 ],
format => 'csv',
csv_profile_id => $csv_profile_id,
output_filepath => $generated_csv_file,
}
);
my $expected_csv = <<EOF;
Title|Barcode
"$biblio_1_title"|$item_1_1->{barcode},$item_1_2->{barcode}
"$biblio_2_title"|$item_2_1->{barcode}
EOF
my $generated_csv_content = read_file( $generated_csv_file );
is( $generated_csv_content, $expected_csv, "Export CSV: All item's infos should have been retrieved" );
$generated_csv_file = '/tmp/test_export.csv';
# Get only 1 item info
Koha::Exporter::Record::export(
{
record_type => 'bibs',
record_ids => [ $biblionumber_1, $biblionumber_2 ],
itemnumbers => [ $item_1_1->{itemnumber}, $item_2_1->{itemnumber} ],
format => 'csv',
csv_profile_id => $csv_profile_id,
output_filepath => $generated_csv_file,
}
);
$expected_csv = <<EOF;
Title|Barcode
"$biblio_1_title"|$item_1_1->{barcode}
"$biblio_2_title"|$item_2_1->{barcode}
EOF
$generated_csv_content = read_file( $generated_csv_file );
is( $generated_csv_content, $expected_csv, "Export CSV: Only 1 item info should have been retrieved" );
};
subtest 'export xml' => sub {
plan tests => 2;
my $generated_xml_file = '/tmp/test_export.xml';
Koha::Exporter::Record::export(
{
record_type => 'bibs',
record_ids => [ $biblionumber_1, $biblionumber_2 ],
format => 'xml',
output_filepath => $generated_xml_file,
}
);
my $generated_xml_content = read_file( $generated_xml_file );
$MARC::File::XML::_load_args{BinaryEncoding} = 'utf-8';
open my $fh, '<', $generated_xml_file;
my $records = MARC::Batch->new( 'XML', $fh );
my @records;
# The following statement produces
# Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/MARC/File/XML.pm line 398, <$fh> chunk 5.
# Why?
while ( my $record = $records->next ) {
push @records, $record;
}
is( scalar( @records ), 2, 'Export XML: 2 records should have been exported' );
my $second_record = $records[1];
my $title = $second_record->subfield(245, 'a');
$title = Encode::encode('UTF-8', $title);
is( $title, $biblio_2_title, 'Export XML: The title is correctly encoded' );
};
subtest 'export iso2709' => sub {
plan tests => 2;
my $generated_mrc_file = '/tmp/test_export.mrc';
# Get all item infos
Koha::Exporter::Record::export(
{
record_type => 'bibs',
record_ids => [ $biblionumber_1, $biblionumber_2 ],
format => 'iso2709',
output_filepath => $generated_mrc_file,
}
);
my $records = MARC::File::USMARC->in( $generated_mrc_file );
my @records;
while ( my $record = $records->next ) {
push @records, $record;
}
is( scalar( @records ), 2, 'Export ISO2709: 2 records should have been exported' );
my $second_record = $records[1];
my $title = $second_record->subfield(245, 'a');
$title = Encode::encode('UTF-8', $title);
is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' );
};