1 package # hide from PAUSE
7 use base qw/Test::Builder::Module Exporter/;
20 package # hide from PAUSE
21 DBIC::SqlMakerTest::SQLATest;
23 # replacement for SQL::Abstract::Test if not available
28 use base qw/Test::Builder::Module Exporter/;
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;
69 my ($bind_ref1, $bind_ref2) = @_;
71 return stringify_bind($bind_ref1) eq stringify_bind($bind_ref2);
79 /^$/ and return $bind;
80 /^ARRAY$/ and return join("\n", map { stringify_bind($_) } @$bind);
81 /^HASH$/ and return join(
82 "\n", map { $_ . " => " . stringify_bind($bind->{$_}) } keys %$bind
84 /^SCALAR$/ and return "\\" . stringify_bind($$bind);
90 eval "use SQL::Abstract::Test;";
92 # SQL::Abstract::Test available
94 *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
95 *eq_sql = \&SQL::Abstract::Test::eq_sql;
96 *eq_bind = \&SQL::Abstract::Test::eq_bind;
100 *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
101 *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
102 *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
113 DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC
118 use DBIC::SqlMakerTest;
120 my ($sql, @bind) = $schema->storage->sql_maker->select(%args);
123 $expected_sql, \@expected_bind,
129 Exports functions that can be used to compare generated SQL and bind values.
131 If L<SQL::Abstract::Test> (packaged in L<SQL::Abstract> versions 1.50 and
132 above) is available, then it is used to perform the comparisons (all functions
133 are delegated to id). Otherwise uses simple string comparison for the SQL
134 statements and simple L<Data::Dumper>-like recursive stringification for
135 comparison of bind values.
140 =head2 is_same_sql_bind
143 $given_sql, \@given_bind,
144 $expected_sql, \@expected_bind,
148 Compares given and expected pairs of C<($sql, \@bind)>, and calls
149 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
153 my $is_same = eq_sql($given_sql, $expected_sql);
155 Compares the two SQL statements. Returns true IFF they are equivalent.
159 my $is_same = eq_sql(\@given_bind, \@expected_bind);
161 Compares two lists of bind values. Returns true IFF their values are the same.
166 L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>.
170 Norbert Buchmuller, <norbi@nix.hu>
172 =head1 COPYRIGHT AND LICENSE
174 Copyright 2008 by Norbert Buchmuller.
176 This library is free software; you can redistribute it and/or modify
177 it under the same terms as Perl itself.