From: Peter Rabbitson Date: Sat, 16 May 2009 22:06:40 +0000 (+0000) Subject: Rewrite SqlMakerTest to fully depend on SQLA::Test X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7bb3e1619d914bab32304dfbe102758461a61ff3;p=dbsrgits%2FDBIx-Class-Historic.git Rewrite SqlMakerTest to fully depend on SQLA::Test Allow is_same_sql_bind to accept an arrayrefref instead of any pair of $sql, \@bind --- diff --git a/t/lib/DBIC/SqlMakerTest.pm b/t/lib/DBIC/SqlMakerTest.pm index cf33fd9..1098e25 100644 --- a/t/lib/DBIC/SqlMakerTest.pm +++ b/t/lib/DBIC/SqlMakerTest.pm @@ -3,144 +3,52 @@ package DBIC::SqlMakerTest; use strict; use warnings; -use base qw/Test::Builder::Module Exporter/; +use base qw/Exporter/; + +use Carp; +use SQL::Abstract::Test; our @EXPORT = qw/ - &is_same_sql_bind - &is_same_sql - &is_same_bind - &eq_sql - &eq_bind - &eq_sql_bind + is_same_sql_bind + is_same_sql + is_same_bind +/; +our @EXPORT_OK = qw/ + eq_sql + eq_bind + eq_sql_bind /; +sub is_same_sql_bind { + # unroll possible as_query arrayrefrefs + my @args; -{ - package DBIC::SqlMakerTest::SQLATest; - - # replacement for SQL::Abstract::Test if not available - - use strict; - use warnings; - - use base qw/Test::Builder::Module Exporter/; - - use Scalar::Util qw(looks_like_number blessed reftype); - use Data::Dumper; - use Test::Builder; - use Test::Deep qw(eq_deeply); - - our $tb = __PACKAGE__->builder; - - sub is_same_sql_bind - { - my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_; - - my $same_sql = eq_sql($sql1, $sql2); - my $same_bind = eq_bind($bind_ref1, $bind_ref2); - - $tb->ok($same_sql && $same_bind, $msg); - - if (!$same_sql) { - _sql_differ_diag($sql1, $sql2); - } - if (!$same_bind) { - _bind_differ_diag($bind_ref1, $bind_ref2); - } - } - - sub is_same_sql - { - my ($sql1, $sql2, $msg) = @_; - - my $same_sql = eq_sql($sql1, $sql2); - - $tb->ok($same_sql, $msg); + for (1,2) { + my $chunk = shift @_; - if (!$same_sql) { - _sql_differ_diag($sql1, $sql2); + if ( ref $chunk eq 'REF' and ref $$chunk eq 'ARRAY' ) { + my ($sql, @bind) = @$$chunk; + push @args, ($sql, \@bind); } - } - - sub is_same_bind - { - my ($bind_ref1, $bind_ref2, $msg) = @_; - - my $same_bind = eq_bind($bind_ref1, $bind_ref2); - - $tb->ok($same_bind, $msg); - - if (!$same_bind) { - _bind_differ_diag($bind_ref1, $bind_ref2); + else { + push @args, $chunk, shift @_; } - } - - sub _sql_differ_diag - { - my ($sql1, $sql2) = @_; - $tb->diag("SQL expressions differ\n" - . " got: $sql1\n" - . "expected: $sql2\n" - ); } - sub _bind_differ_diag - { - my ($bind_ref1, $bind_ref2) = @_; + push @args, shift @_; - $tb->diag("BIND values differ\n" - . " got: " . Dumper($bind_ref1) - . "expected: " . Dumper($bind_ref2) - ); - } - - sub eq_sql - { - my ($left, $right) = @_; - - $left =~ s/\s+//g; - $right =~ s/\s+//g; - - return $left eq $right; - } - - sub eq_bind - { - my ($bind_ref1, $bind_ref2) = @_; - - return eq_deeply($bind_ref1, $bind_ref2); - } - - sub eq_sql_bind - { - my ($sql1, $bind_ref1, $sql2, $bind_ref2) = @_; - - return eq_sql($sql1, $sql2) && eq_bind($bind_ref1, $bind_ref2); - } -} + croak "Unexpected argument(s) supplied to is_same_sql_bind: " . join ('; ', @_) + if @_; -eval "use SQL::Abstract::Test;"; -if ($@ eq '') { - # SQL::Abstract::Test available - - *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind; - *is_same_sql = \&SQL::Abstract::Test::is_same_sql; - *is_same_bind = \&SQL::Abstract::Test::is_same_bind; - *eq_sql = \&SQL::Abstract::Test::eq_sql; - *eq_bind = \&SQL::Abstract::Test::eq_bind; - *eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind; -} else { - # old SQL::Abstract - - *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind; - *is_same_sql = \&DBIC::SqlMakerTest::SQLATest::is_same_sql; - *is_same_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_bind; - *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql; - *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind; - *eq_sql_bind = \&DBIC::SqlMakerTest::SQLATest::eq_sql_bind; + SQL::Abstract::Test::is_same_sql_bind (@args); } +*is_same_sql = \&SQL::Abstract::Test::is_same_sql; +*is_same_bind = \&SQL::Abstract::Test::is_same_bind; +*eq_sql = \&SQL::Abstract::Test::eq_sql; +*eq_bind = \&SQL::Abstract::Test::eq_bind; +*eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind; 1; @@ -167,19 +75,27 @@ DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC Exports functions that can be used to compare generated SQL and bind values. -If L (packaged in L versions 1.50 and -above) is available, then it is used to perform the comparisons (all functions -are delegated to id). Otherwise uses simple string comparison for the SQL -statements and simple L-like recursive stringification for -comparison of bind values. - +This is a thin wrapper around L, which makes it easier +to compare as_query sql/bind arrayrefrefs directly. =head1 FUNCTIONS =head2 is_same_sql_bind is_same_sql_bind( - $given_sql, \@given_bind, + $given_sql, \@given_bind, + $expected_sql, \@expected_bind, + $test_msg + ); + + is_same_sql_bind( + $rs->as_query + $expected_sql, \@expected_bind, + $test_msg + ); + + is_same_sql_bind( + \[$given_sql, @given_bind], $expected_sql, \@expected_bind, $test_msg ); @@ -245,4 +161,4 @@ Norbert Buchmuller, Copyright 2008 by Norbert Buchmuller. This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. +it under the same terms as Perl itself.