X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flib%2FDBIC%2FSqlMakerTest.pm;h=44ccb4b2452767e2c2692e3e151efe1e981aaa68;hb=d52d4d6e066eca4c85c5f4f6cf7b005f0dc883bc;hp=8c2406c46d80e4f9ddfaa08dd921da55f8903773;hpb=478503c9ad0e26000fc78f44da5d3cc1d84c56be;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/lib/DBIC/SqlMakerTest.pm b/t/lib/DBIC/SqlMakerTest.pm index 8c2406c..44ccb4b 100644 --- a/t/lib/DBIC/SqlMakerTest.pm +++ b/t/lib/DBIC/SqlMakerTest.pm @@ -3,88 +3,53 @@ 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 - &eq_sql - &eq_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); + for (1,2) { + my $chunk = shift @_; - if (!$same_sql) { - $tb->diag("SQL expressions differ\n" - . " got: $sql1\n" - . "expected: $sql2\n" - ); + if ( ref $chunk eq 'REF' and ref $$chunk eq 'ARRAY' ) { + my ($sql, @bind) = @$$chunk; + push @args, ($sql, \@bind); } - if (!$same_bind) { - $tb->diag("BIND values differ\n" - . " got: " . Dumper($bind_ref1) - . "expected: " . Dumper($bind_ref2) - ); + else { + push @args, $chunk, shift @_; } - } - 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); } -} -eval "use SQL::Abstract::Test;"; -if ($@ eq '') { - # SQL::Abstract::Test available + push @args, shift @_; - *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind; - *eq_sql = \&SQL::Abstract::Test::eq_sql; - *eq_bind = \&SQL::Abstract::Test::eq_bind; -} else { - # old SQL::Abstract + croak "Unexpected argument(s) supplied to is_same_sql_bind: " . join ('; ', @_) + if @_; - *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind; - *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql; - *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind; + @_ = @args; + goto &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; 1; @@ -111,19 +76,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 ); @@ -131,6 +104,28 @@ comparison of bind values. Compares given and expected pairs of C<($sql, \@bind)>, and calls L on the result, with C<$test_msg> as message. +=head2 is_same_sql + + is_same_sql( + $given_sql, + $expected_sql, + $test_msg + ); + +Compares given and expected SQL statement, and calls L on the +result, with C<$test_msg> as message. + +=head2 is_same_bind + + is_same_bind( + \@given_bind, + \@expected_bind, + $test_msg + ); + +Compares given and expected bind value lists, and calls L on +the result, with C<$test_msg> as message. + =head2 eq_sql my $is_same = eq_sql($given_sql, $expected_sql); @@ -143,6 +138,16 @@ Compares the two SQL statements. Returns true IFF they are equivalent. Compares two lists of bind values. Returns true IFF their values are the same. +=head2 eq_sql_bind + + my $is_same = eq_sql_bind( + $given_sql, \@given_bind, + $expected_sql, \@expected_bind + ); + +Compares the two SQL statements and the two lists of bind values. Returns true +IFF they are equivalent and the bind values are the same. + =head1 SEE ALSO @@ -157,4 +162,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.