Bug 21749: (QA follow-up) Add more tests for ImportFramework

This patch adds tests for:
- Output of ImportFramework on fields removal and error conditions (file
  not found)
- Different file format behaviour (csv, xml and ods are tested).

To test:
- Run
  $ kshell
 k$ prove t/db_dependent/ImportExportFramework.t
=> SUCCESS: All green :-D
- Sign off :-D

Note: the biblio_framework* files are based on the original one on the
patchset, and exported in 17.11 with different formats.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
This commit is contained in:
Tomas Cohen Arazi 2018-11-12 15:03:34 -03:00 committed by Nick Clemens
parent 2d1b80d216
commit ecf68132b7
6 changed files with 157 additions and 17 deletions

View File

@ -1,34 +1,100 @@
#!/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 <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 3;
use Test::More tests => 1;
use t::lib::TestBuilder;
use File::Basename qw( dirname );
use Koha::Database;
use Koha::BiblioFrameworks;
use Koha::MarcSubfieldStructures;
use t::lib::TestBuilder;
use C4::ImportExportFramework;
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
my $data_filepath = dirname(__FILE__) . '/data/biblio_framework.csv';
subtest 'ImportFramework() tests' => sub {
my $frameworkcode = '4T';
C4::ImportExportFramework::ImportFramework($data_filepath, $frameworkcode);
plan tests => 3;
my $dbh = C4::Context->dbh;
subtest 'CSV tests' => sub {
plan tests => 9;
# FIXME Import does not create the biblio framework
#my $biblio_framework = Koha::BiblioFrameworks->find($frameworkcode);
#ok( $biblio_framework );
run_tests('csv');
};
my $nb_tags = $dbh->selectrow_array(q|SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode="4T"|);
is( $nb_tags, 4, "4 tags should have been imported" );
subtest 'ODS tests' => sub {
plan tests => 9;
my $nb_subfields =
Koha::MarcSubfieldStructures->search( { frameworkcode => $frameworkcode } )
->count;
is( $nb_subfields, 12, "12 subfields should have been imported" );
run_tests('ods');
};
subtest 'XML tests' => sub {
plan tests => 9;
run_tests('xml');
};
};
sub run_tests {
my ($format) = @_;
$schema->storage->txn_begin;
my $data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework.$format";
my $fw_1 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
my $result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
is( $result, 0, 'Import successful, no tags removed' );
my $nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
is( $nb_tags, 4, "4 tags should have been imported" );
my $nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
is( $nb_subfields, 12, "12 subfields should have been imported" );
# bad file tests
my $fw_2 = $builder->build_object({ class => 'Koha::BiblioFrameworks' });
$result = C4::ImportExportFramework::ImportFramework( '', $fw_2->id );
is( $result, -1, 'Bad file makes it return -1' );
$nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_2->id })->count;
is( $nb_tags, 0, "0 tags should have been imported" );
$nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_2->id })->count;
is( $nb_subfields, 0, "0 subfields should have been imported" );
# framework overwrite
$data_filepath = dirname(__FILE__) . "/data/frameworks/biblio_framework_smaller.$format";
$result = C4::ImportExportFramework::ImportFramework( $data_filepath, $fw_1->id );
is( $result, 5, 'Smaller fw import successful, 4 tags removed' );
$nb_tags = $schema->resultset('MarcTagStructure')->search({ frameworkcode => $fw_1->id })->count;
is( $nb_tags, 3, "3 tags should have been imported" );
$nb_subfields = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw_1->id })->count;
is( $nb_subfields, 8, "8 subfields should have been imported" );
$schema->storage->txn_rollback;
}

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,18 @@
"tagfield","liblibrarian","libopac","repeatable","mandatory","authorised_value","frameworkcode"
"000","LEADER","LEADER","0","1","","T5"
"001","CONTROL NUMBER","CONTROL NUMBER","0","0","","T5"
"003","CONTROL NUMBER IDENTIFIER","CONTROL NUMBER IDENTIFIER","0","1","","T5"
"#-#","#-#","#-#","#-#","#-#","#-#","#-#"
"tagfield","tagsubfield","liblibrarian","libopac","repeatable","mandatory","kohafield","tab","authorised_value","authtypecode","value_builder","isurl","hidden","frameworkcode","seealso","link","defaultvalue","maxlength"
"000","@","fixed length control field","fixed length control field","0","1","","0","","","marc21_leader.pl","0","0","T5","","","","24"
"001","@","control field","control field","0","0","","0","","","","0","0","T5","","","","9999"
"003","@","control field","control field","0","1","","0","","","marc21_orgcode.pl","0","0","T5","","","","9999"
"005","@","control field","control field","0","1","","0","","","marc21_field_005.pl","0","0","T5","","","","9999"
"007","@","fixed length control field","fixed length control field","0","0","","0","","","marc21_field_007.pl","0","0","T5","","","","9999"
"008","@","fixed length control field","fixed length control field","0","1","","0","","","marc21_field_008.pl","0","0","T5","","","","40"
"010","8","Field link and sequence number","Field link and sequence number","1","0","","0","","","","0","-6","T5","","","","9999"
"010","b","NUCMC control number","NUCMC control number","1","0","","0","","","","0","-1","T5","","","","9999"
"#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#","#-#"
Can't render this file because it has a wrong number of fields in line 8.

File diff suppressed because one or more lines are too long