1 package DBIC::SqlMakerTest;
6 use base qw/Test::Builder::Module Exporter/;
18 package # hide from PAUSE
19 DBIC::SqlMakerTest::SQLATest;
21 # replacement for SQL::Abstract::Test if not available
26 use base qw/Test::Builder::Module Exporter/;
28 use Scalar::Util qw(looks_like_number blessed reftype);
31 use Test::Deep qw(eq_deeply);
33 our $tb = __PACKAGE__->builder;
37 my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_;
39 my $same_sql = eq_sql($sql1, $sql2);
40 my $same_bind = eq_bind($bind_ref1, $bind_ref2);
42 $tb->ok($same_sql && $same_bind, $msg);
45 $tb->diag("SQL expressions differ\n"
51 $tb->diag("BIND values differ\n"
52 . " got: " . Dumper($bind_ref1)
53 . "expected: " . Dumper($bind_ref2)
60 my ($left, $right) = @_;
65 return $left eq $right;
68 # lifted from SQL::Abstract::Test
71 my ($bind_ref1, $bind_ref2) = @_;
73 return eq_deeply($bind_ref1, $bind_ref2);
77 eval "use SQL::Abstract::Test;";
79 # SQL::Abstract::Test available
81 *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
82 *eq_sql = \&SQL::Abstract::Test::eq_sql;
83 *eq_bind = \&SQL::Abstract::Test::eq_bind;
87 *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
88 *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
89 *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
100 DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC
105 use DBIC::SqlMakerTest;
107 my ($sql, @bind) = $schema->storage->sql_maker->select(%args);
110 $expected_sql, \@expected_bind,
116 Exports functions that can be used to compare generated SQL and bind values.
118 If L<SQL::Abstract::Test> (packaged in L<SQL::Abstract> versions 1.50 and
119 above) is available, then it is used to perform the comparisons (all functions
120 are delegated to id). Otherwise uses simple string comparison for the SQL
121 statements and simple L<Data::Dumper>-like recursive stringification for
122 comparison of bind values.
127 =head2 is_same_sql_bind
130 $given_sql, \@given_bind,
131 $expected_sql, \@expected_bind,
135 Compares given and expected pairs of C<($sql, \@bind)>, and calls
136 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
140 my $is_same = eq_sql($given_sql, $expected_sql);
142 Compares the two SQL statements. Returns true IFF they are equivalent.
146 my $is_same = eq_sql(\@given_bind, \@expected_bind);
148 Compares two lists of bind values. Returns true IFF their values are the same.
153 L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>.
157 Norbert Buchmuller, <norbi@nix.hu>
159 =head1 COPYRIGHT AND LICENSE
161 Copyright 2008 by Norbert Buchmuller.
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself.