X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flib%2FDBIC%2FSqlMakerTest.pm;h=44ccb4b2452767e2c2692e3e151efe1e981aaa68;hb=97c96475ed1bdd955ba0e6c4f22087ca05c64162;hp=0ab8939e73959c049cd0b5e374cd55f950b43db0;hpb=ce3b4eb94a66d94e1c0daf5b010897d29e1edef6;p=dbsrgits%2FDBIx-Class.git diff --git a/t/lib/DBIC/SqlMakerTest.pm b/t/lib/DBIC/SqlMakerTest.pm index 0ab8939..44ccb4b 100644 --- a/t/lib/DBIC/SqlMakerTest.pm +++ b/t/lib/DBIC/SqlMakerTest.pm @@ -1,95 +1,55 @@ -package # hide from PAUSE - DBIC::SqlMakerTest; +package DBIC::SqlMakerTest; use strict; use warnings; -use base qw/Test::Builder::Module Exporter/; +use base qw/Exporter/; -use 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 # hide from PAUSE - 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; } - # lifted from SQL::Abstract::Test - 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; @@ -116,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 ); @@ -136,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); @@ -148,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 @@ -162,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.