1 package DBIC::SqlMakerTest;
6 use base qw/Test::Builder::Module Exporter/;
16 package DBIC::SqlMakerTest::SQLATest;
18 # replacement for SQL::Abstract::Test if not available
23 use base qw/Test::Builder::Module Exporter/;
25 use Scalar::Util qw(looks_like_number blessed reftype);
28 use Test::Deep qw(eq_deeply);
30 our $tb = __PACKAGE__->builder;
34 my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_;
36 my $same_sql = eq_sql($sql1, $sql2);
37 my $same_bind = eq_bind($bind_ref1, $bind_ref2);
39 $tb->ok($same_sql && $same_bind, $msg);
42 $tb->diag("SQL expressions differ\n"
48 $tb->diag("BIND values differ\n"
49 . " got: " . Dumper($bind_ref1)
50 . "expected: " . Dumper($bind_ref2)
57 my ($left, $right) = @_;
62 return $left eq $right;
67 my ($bind_ref1, $bind_ref2) = @_;
69 return eq_deeply($bind_ref1, $bind_ref2);
73 eval "use SQL::Abstract::Test;";
75 # SQL::Abstract::Test available
77 *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
78 *eq_sql = \&SQL::Abstract::Test::eq_sql;
79 *eq_bind = \&SQL::Abstract::Test::eq_bind;
83 *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
84 *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
85 *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
96 DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC
101 use DBIC::SqlMakerTest;
103 my ($sql, @bind) = $schema->storage->sql_maker->select(%args);
106 $expected_sql, \@expected_bind,
112 Exports functions that can be used to compare generated SQL and bind values.
114 If L<SQL::Abstract::Test> (packaged in L<SQL::Abstract> versions 1.50 and
115 above) is available, then it is used to perform the comparisons (all functions
116 are delegated to id). Otherwise uses simple string comparison for the SQL
117 statements and simple L<Data::Dumper>-like recursive stringification for
118 comparison of bind values.
123 =head2 is_same_sql_bind
126 $given_sql, \@given_bind,
127 $expected_sql, \@expected_bind,
131 Compares given and expected pairs of C<($sql, \@bind)>, and calls
132 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
136 my $is_same = eq_sql($given_sql, $expected_sql);
138 Compares the two SQL statements. Returns true IFF they are equivalent.
142 my $is_same = eq_sql(\@given_bind, \@expected_bind);
144 Compares two lists of bind values. Returns true IFF their values are the same.
149 L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>.
153 Norbert Buchmuller, <norbi@nix.hu>
155 =head1 COPYRIGHT AND LICENSE
157 Copyright 2008 by Norbert Buchmuller.
159 This library is free software; you can redistribute it and/or modify
160 it under the same terms as Perl itself.