From 6fa4d8361d7d406ec3fd15af31b7ffec1f4e5fcd Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Wed, 3 Jul 2024 09:53:26 +0000 Subject: [PATCH] Bug 36831: Add unit test prove t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t Signed-off-by: David Nind --- .../Koha/BackgroundJob/ImportKBARTFile.t | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t b/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t index e0abbb283a..c018a9414b 100755 --- a/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t +++ b/t/db_dependent/Koha/BackgroundJob/ImportKBARTFile.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use Test::MockModule; use Test::Warn; @@ -39,7 +39,6 @@ subtest 'enqueue' => sub { $schema->storage->txn_begin; - # FIXME: Should be an exception my $job_id = Koha::BackgroundJob::ImportKBARTFile->new->enqueue(); is( $job_id, undef, 'Nothing enqueued if missing file param' ); @@ -50,15 +49,13 @@ subtest 'calculate_chunked_params_size' => sub { plan tests => 2; - my $max_number_of_lines = - Koha::BackgroundJob::ImportKBARTFile::calculate_chunked_params_size( 500000, 100000, 50000 ); + my $max_number_of_lines = Koha::ERM::EHoldings::Title::calculate_chunked_params_size( 500000, 100000, 50000 ); is( $max_number_of_lines, 7500, 'Number of lines calculated correctly' ); - my $max_number_of_lines2 = - Koha::BackgroundJob::ImportKBARTFile::calculate_chunked_params_size( 400000, 100000, 60000 ); + my $max_number_of_lines2 = Koha::ERM::EHoldings::Title::calculate_chunked_params_size( 400000, 100000, 60000 ); is( $max_number_of_lines2, 11250, 'Number of lines calculated correctly' ); }; -subtest 'format_title' => sub { +subtest '_format_title' => sub { plan tests => 4; @@ -67,7 +64,7 @@ subtest 'format_title' => sub { coverage_notes => 'Test notes' }; - my $formatted_title = Koha::BackgroundJob::ImportKBARTFile::format_title($title); + my $formatted_title = Koha::BackgroundJob::ImportKBARTFile::_format_title($title); is( $formatted_title->{external_id}, 1, 'external_id formatted correctly' ); is( $formatted_title->{notes}, 'Test notes', 'notes formatted correctly' ); @@ -77,7 +74,7 @@ subtest 'format_title' => sub { subtest 'read_file' => sub { - plan tests => 7; + plan tests => 6; my $file = { filename => 'Test_file.csv', @@ -88,7 +85,7 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ) }; - my ( $column_headers, $lines, $error ) = Koha::BackgroundJob::ImportKBARTFile::read_file($file); + my ( $column_headers, $lines, $error ) = Koha::ERM::EHoldings::Title::read_file($file); is( @{$column_headers}, 25, '25 column headers found' ); is( @{$column_headers}[0], 'publication_title', 'First header correctly extracted' ); @@ -112,7 +109,24 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ], 'Line correctly identified' ); +}; + +subtest '_detect_delimiter_and_quote' => sub { + + plan tests => 3; + + my $file = { + filename => 'Test_file.txt', + file_content => encode_base64( + 'publication_title,print_identifier,online_identifier,date_first_issue_online,num_first_vol_online,num_first_issue_online,date_last_issue_online,num_last_vol_online,num_last_issue_online,title_url,first_author,title_id,embargo_info,coverage_depth,coverage_notes,publisher_name,publication_type,date_monograph_published_print,date_monograph_published_online,monograph_volume,monograph_edition,first_editor,parent_publication_title_id,preceding_publication_title_id,access_type +Nature Plants,,2055-0278,2015-01,1,1,,,,https://www.nature.com/nplants,,4aaa7,,fulltext,Hybrid (Open Choice),Nature Publishing Group UK,serial,,,,,,,,P +Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bbb0,,fulltext,Hybrid (Open Choice),Nature Publishing Group UK,serial,,,,,,,,P' + ) + }; + my ( $delimiter, $quote_char ) = Koha::ERM::EHoldings::Title::_detect_delimiter_and_quote($file); + is( $delimiter, ',', 'Comma delimiter identified' ); + is( $quote_char, '"', 'Quote character identified' ); my $file2 = { filename => 'Test_file2.csv', file_content => encode_base64( @@ -122,12 +136,12 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ) }; warning_is { - Koha::BackgroundJob::ImportKBARTFile::read_file($file2); + Koha::ERM::EHoldings::Title::read_file($file2); } '2023, EIQ - QUO character not allowed, 157', 'Error message correctly reported'; }; -subtest 'create_title_hash_from_line_data' => sub { +subtest '_create_title_hash_from_line_data' => sub { plan tests => 2; @@ -140,14 +154,14 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ) }; - my ( $column_headers, $lines ) = Koha::BackgroundJob::ImportKBARTFile::read_file($file); + my ( $column_headers, $lines ) = Koha::ERM::EHoldings::Title::read_file($file); my @invalid_columns; - my $title_from_line1 = Koha::BackgroundJob::ImportKBARTFile::create_title_hash_from_line_data( + my $title_from_line1 = Koha::BackgroundJob::ImportKBARTFile::_create_title_hash_from_line_data( @{$lines}[0], $column_headers, \@invalid_columns ); - my $title_from_line2 = Koha::BackgroundJob::ImportKBARTFile::create_title_hash_from_line_data( + my $title_from_line2 = Koha::BackgroundJob::ImportKBARTFile::_create_title_hash_from_line_data( @{$lines}[1], $column_headers, \@invalid_columns ); @@ -211,7 +225,7 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb is_deeply( $title_from_line2, $line2_match, 'Title hash created correctly' ); }; -subtest 'create_title_hash_from_line_data with invalid columns using csv' => sub { +subtest '_create_title_hash_from_line_data with invalid columns using csv' => sub { plan tests => 2; @@ -224,14 +238,14 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ) }; - my ( $column_headers, $lines ) = Koha::BackgroundJob::ImportKBARTFile::read_file($file); + my ( $column_headers, $lines ) = Koha::ERM::EHoldings::Title::read_file($file); my @invalid_columns = ('invalid_column'); - my $title_from_line1 = Koha::BackgroundJob::ImportKBARTFile::create_title_hash_from_line_data( + my $title_from_line1 = Koha::BackgroundJob::ImportKBARTFile::_create_title_hash_from_line_data( @{$lines}[0], $column_headers, \@invalid_columns ); - my $title_from_line2 = Koha::BackgroundJob::ImportKBARTFile::create_title_hash_from_line_data( + my $title_from_line2 = Koha::BackgroundJob::ImportKBARTFile::_create_title_hash_from_line_data( @{$lines}[1], $column_headers, \@invalid_columns ); @@ -317,7 +331,7 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ) }; - my ( $column_headers, $rows, $error ) = Koha::BackgroundJob::ImportKBARTFile::read_file($file); + my ( $column_headers, $rows, $error ) = Koha::ERM::EHoldings::Title::read_file($file); my $data = { column_headers => $column_headers, rows => $rows, @@ -373,7 +387,7 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb my $module = Test::MockModule->new('Koha::BackgroundJob::ImportKBARTFile'); $module->mock( - 'create_title_hash_from_line_data', + '_create_title_hash_from_line_data', sub { my ( $row, $column_headers ) = @_; @@ -413,10 +427,10 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb $job3->messages, [ { - 'type' => 'error', - 'code' => 'no_title_found', - 'title_id' => '12345', - 'title' => '(Unknown)' + 'type' => 'error', + 'code' => 'no_title_found', + 'title_id' => '12345', + 'title' => '(Unknown)' }, { 'code' => 'title_already_exists', @@ -429,7 +443,7 @@ Nature Astronomy,,2397-3366,2017-01,1,1,,,,https://www.nature.com/natastron,,4bb ); $module->mock( - 'create_title_hash_from_line_data', + '_create_title_hash_from_line_data', sub { my ( $row, $column_headers ) = @_; -- 2.39.5