1 package DBIC::SqlMakerTest;
6 use base qw/Test::Builder::Module Exporter/;
18 package DBIC::SqlMakerTest::SQLATest;
20 # replacement for SQL::Abstract::Test if not available
25 use base qw/Test::Builder::Module Exporter/;
27 use Scalar::Util qw(looks_like_number blessed reftype);
30 use Test::Deep qw(eq_deeply);
32 our $tb = __PACKAGE__->builder;
36 my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_;
38 my $same_sql = eq_sql($sql1, $sql2);
39 my $same_bind = eq_bind($bind_ref1, $bind_ref2);
41 $tb->ok($same_sql && $same_bind, $msg);
44 $tb->diag("SQL expressions differ\n"
50 $tb->diag("BIND values differ\n"
51 . " got: " . Dumper($bind_ref1)
52 . "expected: " . Dumper($bind_ref2)
59 my ($left, $right) = @_;
64 return $left eq $right;
67 # lifted from SQL::Abstract::Test
70 my ($bind_ref1, $bind_ref2) = @_;
72 return eq_deeply($bind_ref1, $bind_ref2);
76 eval "use SQL::Abstract::Test;";
78 # SQL::Abstract::Test available
80 *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
81 *eq_sql = \&SQL::Abstract::Test::eq_sql;
82 *eq_bind = \&SQL::Abstract::Test::eq_bind;
86 *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
87 *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
88 *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
99 DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC
104 use DBIC::SqlMakerTest;
106 my ($sql, @bind) = $schema->storage->sql_maker->select(%args);
109 $expected_sql, \@expected_bind,
115 Exports functions that can be used to compare generated SQL and bind values.
117 If L<SQL::Abstract::Test> (packaged in L<SQL::Abstract> versions 1.50 and
118 above) is available, then it is used to perform the comparisons (all functions
119 are delegated to id). Otherwise uses simple string comparison for the SQL
120 statements and simple L<Data::Dumper>-like recursive stringification for
121 comparison of bind values.
126 =head2 is_same_sql_bind
129 $given_sql, \@given_bind,
130 $expected_sql, \@expected_bind,
134 Compares given and expected pairs of C<($sql, \@bind)>, and calls
135 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
139 my $is_same = eq_sql($given_sql, $expected_sql);
141 Compares the two SQL statements. Returns true IFF they are equivalent.
145 my $is_same = eq_sql(\@given_bind, \@expected_bind);
147 Compares two lists of bind values. Returns true IFF their values are the same.
152 L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>.
156 Norbert Buchmuller, <norbi@nix.hu>
158 =head1 COPYRIGHT AND LICENSE
160 Copyright 2008 by Norbert Buchmuller.
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.