From 0adbf36ab1d1d9238b80324b8e12cc99a1d72cf2 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 8 Oct 2015 11:40:43 +0100 Subject: [PATCH] Bug 14778: Example - Replace DBI::Mock with Test::DBIx::Class - Sitemapper.t We can use Test::DBIx::Class to install fixtures before our tests. Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Sitemapper.t | 75 ++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/t/db_dependent/Sitemapper.t b/t/db_dependent/Sitemapper.t index 293edad632..c53ac8ebff 100755 --- a/t/db_dependent/Sitemapper.t +++ b/t/db_dependent/Sitemapper.t @@ -18,11 +18,12 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use Modern::Perl; -use Test::MockModule; use File::Basename; use File::Path; use DateTime; -use Test::More tests => 14; +use Test::MockModule; +use Test::More tests => 16; +use Koha::Schema; BEGIN { @@ -30,7 +31,6 @@ BEGIN { use_ok('Koha::Sitemapper::Writer'); } - sub slurp { my $file = shift; open my $fh, '<', $file or die; @@ -40,28 +40,38 @@ sub slurp { return $cont; } +use Test::DBIx::Class { + schema_class => 'Koha::Schema', + connect_info => ['dbi:SQLite:dbname=:memory:','',''], + connect_opts => { name_sep => '.', quote_char => '`', }, + fixture_class => '::Populate', +}, 'Biblio' ; + +sub fixtures { + my ( $data ) = @_; + fixtures_ok [ + Biblio => [ + [ qw/ biblionumber datecreated timestamp / ], + @$data, + ], + ], 'add fixtures'; +} -# Create 3 mocked dataset to be used by Koha::Sitemaper in place of DB content -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 -}); -my $dbh = C4::Context->dbh(); -my $two_bibs = [ - [ qw/ biblionumber timestamp / ], - [ qw/ 1234 2013-11-15 / ], - [ qw/ 9875 2015-08-31 / ], -]; -my $lotof_bibs = [ [ qw/ biblionumber timestamp / ] ]; -push @$lotof_bibs, [ $_, '2015-08-31' ] for 1..75000; -$dbh->{mock_add_resultset} = $two_bibs; -$dbh->{mock_add_resultset} = $two_bibs; -$dbh->{mock_add_resultset} = $lotof_bibs; +# Make the code in the module use our mocked Koha::Schema/Koha::Database +my $db = Test::MockModule->new('Koha::Database'); +$db->mock( + # Schema() gives us the DB connection set up by Test::DBIx::Class + _new_schema => sub { return Schema(); } +); my $dir = File::Spec->rel2abs( dirname(__FILE__) ); + +my $data = [ + [ qw/ 1 2013-11-15 2013-11-15/ ], + [ qw/ 2 2015-08-31 2015-08-31/ ], +]; +fixtures($data); # Create a sitemap for a catalog containg 2 biblios, with option 'long url' my $sitemaper = Koha::Sitemapper->new( verbose => 0, @@ -86,7 +96,7 @@ my $expected_content = < EOS chop $expected_content; -ok( $file_content eq $expected_content, "Its content is valid" ); +is( $file_content, $expected_content, "Its content is valid" ); $file = "$dir/sitemap0001.xml"; ok( -e $file, "File sitemap0001.xml created"); @@ -96,16 +106,16 @@ $expected_content = < - http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1234 + http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1 2013-11-15 - http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=9875 + http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2 2015-08-31 EOS -ok( $file_content eq $expected_content, "Its content is valid" ); +is( $file_content, $expected_content, "Its content is valid" ); # Create a sitemap for a catalog containg 2 biblios, with option 'short url'. @@ -126,21 +136,24 @@ $expected_content = < - http://www.mylibrary.org/bib/1234 + http://www.mylibrary.org/bib/1 2013-11-15 - http://www.mylibrary.org/bib/9875 + http://www.mylibrary.org/bib/2 2015-08-31 EOS -ok( $file_content eq $expected_content, "Its content is valid" ); +is( $file_content, $expected_content, "Its content is valid" ); # Create a sitemap for a catalog containing 75000 biblios, with option 'short # url'. Test that 3 files are created: index file + 2 urls file with # respectively 50000 et 25000 urls. +$data = []; +push @$data, [ $_, '2015-08-31', '2015-08-31'] for 3..75000; +fixtures($data); $sitemaper = Koha::Sitemapper->new( verbose => 0, url => 'http://www.mylibrary.org', @@ -167,7 +180,7 @@ $expected_content = < EOS chop $expected_content; -ok( $file_content eq $expected_content, "Its content is valid" ); +is( $file_content, $expected_content, "Its content is valid" ); $file = "$dir/sitemap0001.xml"; ok( -e $file, "File sitemap0001.xml created"); @@ -177,7 +190,7 @@ my $count = 0; while (<$fh>) { $count++ if //; } -ok ( $count == 50000, "It contains 50000 URLs"); +is( $count, 50000, "It contains 50000 URLs"); $file = "$dir/sitemap0002.xml"; ok( -e $file, "File sitemap0002.xml created"); @@ -187,7 +200,7 @@ $count = 0; while (<$fh>) { $count++ if //; } -ok ( $count == 25000, "It contains 25000 URLs"); +is( $count, 25000, "It contains 25000 URLs"); # Cleanup unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /; -- 2.39.5