From 6e4a3d4cd360e814334bf7a132cad6cae82bcd8d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 10 Apr 2013 15:50:50 -0300 Subject: [PATCH 2/2] [PASSED QA] Bug 9659 - QA Follow up: Unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added some tests against the methods added by this patch. To test, prove -v - t/Koha.t - t/ReportsGuided.t Edit: fixed the amount of tests in the skip block. Tests fail for people having earlier versions of DBD::Mock. Sponsored-by: Universidad Nacional de Córdoba Signed-off-by: Katrin Fischer Works quite nicely! All tests and QA script pass. --- t/Koha.t | 36 +++++++++++++++++- t/ReportsGuided.t | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) create mode 100755 t/ReportsGuided.t diff --git a/t/Koha.t b/t/Koha.t index 025ab98..3fde068 100755 --- a/t/Koha.t +++ b/t/Koha.t @@ -2,10 +2,43 @@ use strict; use warnings; -use Test::More tests => 8; +use C4::Context; +use Test::More tests => 11; +use Test::MockModule; +use DBD::Mock; use_ok('C4::Koha'); +my $module_context = new Test::MockModule('C4::Context'); +$module_context->mock( + '_new_dbh', + sub { + my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) + || die "Cannot create handle: $DBI::errstr\n"; + return $dbh; + } +); + +SKIP: { + + skip "DBD::Mock is too old", 3 + unless $DBD::Mock::VERSION >= 1.45; + + my @loc_results = (['category'],['LOC']); + my @empty_results = ([]); + my @relterms_results = (['category'],['RELTERMS']); + + my $dbh = C4::Context->dbh(); + + $dbh->{mock_add_resultset} = \@loc_results; + is ( IsAuthorisedValueCategory('LOC'), 1, 'LOC is a valid authorized value category'); + $dbh->{mock_add_resultset} = \@empty_results; + is ( IsAuthorisedValueCategory('something'), 0, 'something is not a valid authorized value category'); + $dbh->{mock_add_resultset} = \@relterms_results; + is ( IsAuthorisedValueCategory('RELTERMS'), 1, 'RELTERMS is a valid authorized value category'); + +} # End SKIP block + # # test that &slashifyDate returns correct (non-US) date # @@ -28,4 +61,3 @@ is(C4::Koha::_isbn_cleanup('0-590-35340-3'), '0590353403', '_isbn_cleanup remove is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical'); is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10'); - diff --git a/t/ReportsGuided.t b/t/ReportsGuided.t new file mode 100755 index 0000000..26e1a49 --- /dev/null +++ b/t/ReportsGuided.t @@ -0,0 +1,110 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 12; +use Test::MockModule; +use DBD::Mock; + +use_ok('C4::Reports::Guided'); + +my $context = new Test::MockModule('C4::Context'); +my $koha = new Test::MockModule('C4::Koha'); + +$context->mock( + '_new_dbh', + sub { + my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) + || die "Cannot create handle: $DBI::errstr\n"; + return $dbh; + } +); + + +sub MockedIsAuthorisedValueCategory { + my $authorised_value = shift; + + if ( $authorised_value eq 'LOC' ) { + return 1; + } else { + return 0; + } +} + +$koha->mock( + 'IsAuthorisedValueCategory', + \&MockedIsAuthorisedValueCategory +); + +{ # GetReservedAuthorisedValues tests + # This one will catch new reserved words not added + # to GetReservedAuthorisedValues + my %test_authval = ( + 'date' => 1, + 'branches' => 1, + 'itemtypes' => 1, + 'cn_source' => 1, + 'categorycode' => 1 + ); + + my $reserved_authorised_values = GetReservedAuthorisedValues(); + is_deeply(\%test_authval, $reserved_authorised_values, + 'GetReservedAuthorisedValues returns a fixed list'); +} + +SKIP: { + + skip "DBD::Mock is too old", 7 + unless $DBD::Mock::VERSION >= 1.45; + + ok( IsAuthorisedValueValid('LOC'), + 'User defined authorised value category is valid'); + + ok( ! IsAuthorisedValueValid('XXX'), + 'Not defined authorised value category is invalid'); + + # Loop through the reserved authorised values + foreach my $authorised_value ( keys GetReservedAuthorisedValues() ) { + ok( IsAuthorisedValueValid($authorised_value), + '\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value'); + } +} + +{ # GetParametersFromSQL tests + + my $test_query_1 = " + SELECT date_due + FROM old_issues + WHERE YEAR(timestamp) = <> AND + branchcode = <> AND + borrowernumber = <> + "; + + my @test_parameters_with_custom_list = ( + { 'name' => 'Year', 'authval' => 'custom_list' }, + { 'name' => 'Branch', 'authval' => 'branches' }, + { 'name' => 'Borrower', 'authval' => undef } + ); + + is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list, + 'SQL params are correctly parsed'); + + # ValidateSQLParameters tests + my @problematic_parameters = (); + push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' }; + is_deeply( ValidateSQLParameters( $test_query_1 ), + \@problematic_parameters, + '\'custom_list\' not a valid category' ); + + my $test_query_2 = " + SELECT date_due + FROM old_issues + WHERE YEAR(timestamp) = <> AND + branchcode = <> AND + borrowernumber = <> + "; + + is_deeply( ValidateSQLParameters( $test_query_2 ), + [], + 'All parameters valid, empty problematic authvals list'); +} -- 1.7.9.5