From 2b73ba258191526d1570cdcf79bff9daa52226d1 Mon Sep 17 00:00:00 2001 From: Andrew Moore Date: Fri, 19 Dec 2008 16:12:25 -0600 Subject: [PATCH] test suite: C4::ImportBatch Here are some tests for functions in C4::ImportBatch. Signed-off-by: Galen Charlton --- t/lib/KohaTest/ImportBatch.pm | 23 +++++++++ t/lib/KohaTest/ImportBatch/AddImportBatch.pm | 31 +++++++++++ .../ImportBatch/AddItemsToImportBiblio.pm | 29 +++++++++++ t/lib/KohaTest/ImportBatch/GetImportBatch.pm | 45 ++++++++++++++++ .../ImportBatch/GetImportRecordMarc.pm | 51 +++++++++++++++++++ t/lib/KohaTest/ImportBatch/GetZ3950BatchId.pm | 42 +++++++++++++++ 6 files changed, 221 insertions(+) create mode 100644 t/lib/KohaTest/ImportBatch/AddImportBatch.pm create mode 100644 t/lib/KohaTest/ImportBatch/AddItemsToImportBiblio.pm create mode 100644 t/lib/KohaTest/ImportBatch/GetImportBatch.pm create mode 100644 t/lib/KohaTest/ImportBatch/GetImportRecordMarc.pm create mode 100644 t/lib/KohaTest/ImportBatch/GetZ3950BatchId.pm diff --git a/t/lib/KohaTest/ImportBatch.pm b/t/lib/KohaTest/ImportBatch.pm index 82c703f153..35f2b2edfc 100644 --- a/t/lib/KohaTest/ImportBatch.pm +++ b/t/lib/KohaTest/ImportBatch.pm @@ -103,4 +103,27 @@ sub shutdown_50_remove_matcher : Test( shutdown => 6) { delete $self->{'matcher_id'}; } +=head2 UTILITY METHODS + +=cut + +sub add_import_batch { + my $self = shift; + my $test_batch = shift + || { + overlay_action => 'create_new', + import_status => 'staging', + batch_type => 'batch', + file_name => 'foo', + comments => 'inserted during automated testing', + }; + my $batch_id = AddImportBatch( $test_batch->{'overlay_action'}, + $test_batch->{'import_status'}, + $test_batch->{'batch_type'}, + $test_batch->{'file_name'}, + $test_batch->{'comments'}, ); + return $batch_id; +} + + 1; diff --git a/t/lib/KohaTest/ImportBatch/AddImportBatch.pm b/t/lib/KohaTest/ImportBatch/AddImportBatch.pm new file mode 100644 index 0000000000..7b97e72537 --- /dev/null +++ b/t/lib/KohaTest/ImportBatch/AddImportBatch.pm @@ -0,0 +1,31 @@ +package KohaTest::ImportBatch::AddImportBatch; +use base qw( KohaTest::ImportBatch ); + +use strict; +use warnings; + +use Test::More; + +use C4::ImportBatch; +use C4::Matcher; +use C4::Biblio; + + +=head3 add_one + +=cut + +sub add_one : Test( 1 ) { + my $self = shift; + + my $batch_id = AddImportBatch( + 'create_new', #overlay_action + 'staging', # import_status + 'batch', # batc_type + 'foo', # file_name + 'inserted during automated testing', # comments + ); + ok( $batch_id, "successfully inserted batch: $batch_id" ); +} + +1; diff --git a/t/lib/KohaTest/ImportBatch/AddItemsToImportBiblio.pm b/t/lib/KohaTest/ImportBatch/AddItemsToImportBiblio.pm new file mode 100644 index 0000000000..39d36df7d6 --- /dev/null +++ b/t/lib/KohaTest/ImportBatch/AddItemsToImportBiblio.pm @@ -0,0 +1,29 @@ +package KohaTest::ImportBatch::getImportBatch; +use base qw( KohaTest::ImportBatch ); + +use strict; +use warnings; + +use Test::More; + +use C4::ImportBatch; +use C4::Matcher; +use C4::Biblio; + + +sub something : Test( 2 ) { + my $self = shift; + + my $batch_id = $self->add_import_batch(); + ok( $batch_id, 'we have a batch_id' ); + + my $import_record_id = 0; + + my $marc_record = MARC::Record->new(); + + my @import_item_ids = C4::ImportBatch::AddItemsToImportBiblio( $batch_id, $import_record_id, $marc_record ); + is( scalar( @import_item_ids ), 0, 'none inserted' ); + +} + +1; diff --git a/t/lib/KohaTest/ImportBatch/GetImportBatch.pm b/t/lib/KohaTest/ImportBatch/GetImportBatch.pm new file mode 100644 index 0000000000..0b01707df1 --- /dev/null +++ b/t/lib/KohaTest/ImportBatch/GetImportBatch.pm @@ -0,0 +1,45 @@ +package KohaTest::ImportBatch::getImportBatch; +use base qw( KohaTest::ImportBatch ); + +use strict; +use warnings; + +use Test::More; + +use C4::ImportBatch; +use C4::Matcher; +use C4::Biblio; + + +=head3 add_one_and_find_it + +=cut + +sub add_one_and_find_it : Test( 7 ) { + my $self = shift; + + my $batch = { + overlay_action => 'create_new', + import_status => 'staging', + batch_type => 'batch', + file_name => 'foo', + comments => 'inserted during automated testing', + }; + my $batch_id = AddImportBatch( + $batch->{'overlay_action'}, + $batch->{'import_status'}, + $batch->{'batch_type'}, + $batch->{'file_name'}, + $batch->{'comments'}, + ); + ok( $batch_id, "successfully inserted batch: $batch_id" ); + + my $retrieved = GetImportBatch( $batch_id ); + + foreach my $key ( keys %$batch ) { + is( $retrieved->{$key}, $batch->{$key}, "both objects agree on $key" ); + } + is( $retrieved->{'import_batch_id'}, $batch_id, 'batch_id' ); +} + +1; diff --git a/t/lib/KohaTest/ImportBatch/GetImportRecordMarc.pm b/t/lib/KohaTest/ImportBatch/GetImportRecordMarc.pm new file mode 100644 index 0000000000..b933b35bec --- /dev/null +++ b/t/lib/KohaTest/ImportBatch/GetImportRecordMarc.pm @@ -0,0 +1,51 @@ +package KohaTest::ImportBatch::GetImportRecordMarc; +use base qw( KohaTest::ImportBatch ); + +use strict; +use warnings; + +use Test::More; + +use C4::ImportBatch; +use C4::Matcher; +use C4::Biblio; + + +=head3 record_does_not_exist + +=cut + +sub record_does_not_exist : Test( 1 ) { + my $self = shift; + + my $id = '999999999999'; + my $marc = GetImportRecordMarc( $id ); + ok( ! defined( $marc ), 'this marc is undefined' ); + +} + +sub record_does_exist : Test( 4 ) { + my $self = shift; + + # we need an import_batch, so let GetZ3950BatchId create one: + my $new_batch_id = GetZ3950BatchId('foo'); + ok( $new_batch_id, "got a new batch ID: $new_batch_id" ); + + my $sth = C4::Context->dbh->prepare( + "INSERT INTO import_records (import_batch_id, marc, marcxml) + VALUES (?, ?, ?)" + ); + my $execute = $sth->execute( + $new_batch_id, # batch_id + 'marc', # marc + 'marcxml', # marcxml + ); + ok( $execute, 'succesfully executed' ); + my $import_record_id = C4::Context->dbh->{'mysql_insertid'}; + ok( $import_record_id, 'we got an import_record_id' ); + + my $marc = GetImportRecordMarc($import_record_id); + ok( defined($marc), 'this marc is defined' ); +} + +1; diff --git a/t/lib/KohaTest/ImportBatch/GetZ3950BatchId.pm b/t/lib/KohaTest/ImportBatch/GetZ3950BatchId.pm new file mode 100644 index 0000000000..f3f5d9cdb2 --- /dev/null +++ b/t/lib/KohaTest/ImportBatch/GetZ3950BatchId.pm @@ -0,0 +1,42 @@ +package KohaTest::ImportBatch::GetZ3950BatchId; +use base qw( KohaTest::ImportBatch ); + +use strict; +use warnings; + +use Test::More; + +use C4::ImportBatch; +use C4::Matcher; +use C4::Biblio; + + +=head3 batch_does_not_exist + +=cut + +sub batch_does_not_exist : Test( 5 ) { + my $self = shift; + + my $file_name = 'testing batch'; + + # lets make sure it doesn't exist first + my $sth = C4::Context->dbh->prepare('SELECT import_batch_id FROM import_batches + WHERE batch_type = ? + AND file_name = ?'); + ok( $sth->execute( 'z3950', $file_name, ), 'execute' ); + my $rowref = $sth->fetchrow_arrayref(); + ok( !defined( $rowref ), 'this batch does not exist' ); + + # now let GetZ3950BatchId create one + my $new_batch_id = GetZ3950BatchId( $file_name ); + ok( $new_batch_id, "got a new batch ID: $new_batch_id" ); + + # now search for the one that was just created + my $second_batch_id = GetZ3950BatchId( $file_name ); + ok( $second_batch_id, "got a second batch ID: $second_batch_id" ); + is( $second_batch_id, $new_batch_id, 'we got the same batch both times.' ); +} + + +1; -- 2.39.5