Trailing WS crusade - got to save them bits
[dbsrgits/DBIx-Class.git] / t / lib / DBIC / SqlMakerTest.pm
1 package DBIC::SqlMakerTest;
2
3 use strict;
4 use warnings;
5
6 use base qw/Exporter/;
7
8 use Carp;
9 use SQL::Abstract::Test;
10
11 our @EXPORT = qw/
12   is_same_sql_bind
13   is_same_sql
14   is_same_bind
15 /;
16 our @EXPORT_OK = qw/
17   eq_sql
18   eq_bind
19   eq_sql_bind
20 /;
21
22 sub is_same_sql_bind {
23   # unroll possible as_query arrayrefrefs
24   my @args;
25
26   for (1,2) {
27     my $chunk = shift @_;
28
29     if ( ref $chunk eq 'REF' and ref $$chunk eq 'ARRAY' ) {
30       my ($sql, @bind) = @$$chunk;
31       push @args, ($sql, \@bind);
32     }
33     else {
34       push @args, $chunk, shift @_;
35     }
36
37   }
38
39   push @args, shift @_;
40
41   croak "Unexpected argument(s) supplied to is_same_sql_bind: " . join ('; ', @_)
42     if @_;
43
44   @_ = @args;
45   goto &SQL::Abstract::Test::is_same_sql_bind;
46 }
47
48 *is_same_sql = \&SQL::Abstract::Test::is_same_sql;
49 *is_same_bind = \&SQL::Abstract::Test::is_same_bind;
50 *eq_sql = \&SQL::Abstract::Test::eq_sql;
51 *eq_bind = \&SQL::Abstract::Test::eq_bind;
52 *eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind;
53
54 1;
55
56 __END__
57
58
59 =head1 NAME
60
61 DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC
62
63 =head1 SYNOPSIS
64
65   use Test::More;
66   use DBIC::SqlMakerTest;
67
68   my ($sql, @bind) = $schema->storage->sql_maker->select(%args);
69   is_same_sql_bind(
70     $sql, \@bind,
71     $expected_sql, \@expected_bind,
72     'foo bar works'
73   );
74
75 =head1 DESCRIPTION
76
77 Exports functions that can be used to compare generated SQL and bind values.
78
79 This is a thin wrapper around L<SQL::Abstract::Test>, which makes it easier
80 to compare as_query sql/bind arrayrefrefs directly.
81
82 =head1 FUNCTIONS
83
84 =head2 is_same_sql_bind
85
86   is_same_sql_bind(
87     $given_sql, \@given_bind,
88     $expected_sql, \@expected_bind,
89     $test_msg
90   );
91
92   is_same_sql_bind(
93     $rs->as_query
94     $expected_sql, \@expected_bind,
95     $test_msg
96   );
97
98   is_same_sql_bind(
99     \[$given_sql, @given_bind],
100     $expected_sql, \@expected_bind,
101     $test_msg
102   );
103
104 Compares given and expected pairs of C<($sql, \@bind)>, and calls
105 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
106
107 =head2 is_same_sql
108
109   is_same_sql(
110     $given_sql,
111     $expected_sql,
112     $test_msg
113   );
114
115 Compares given and expected SQL statement, and calls L<Test::Builder/ok> on the
116 result, with C<$test_msg> as message.
117
118 =head2 is_same_bind
119
120   is_same_bind(
121     \@given_bind,
122     \@expected_bind,
123     $test_msg
124   );
125
126 Compares given and expected bind value lists, and calls L<Test::Builder/ok> on
127 the result, with C<$test_msg> as message.
128
129 =head2 eq_sql
130
131   my $is_same = eq_sql($given_sql, $expected_sql);
132
133 Compares the two SQL statements. Returns true IFF they are equivalent.
134
135 =head2 eq_bind
136
137   my $is_same = eq_sql(\@given_bind, \@expected_bind);
138
139 Compares two lists of bind values. Returns true IFF their values are the same.
140
141 =head2 eq_sql_bind
142
143   my $is_same = eq_sql_bind(
144     $given_sql, \@given_bind,
145     $expected_sql, \@expected_bind
146   );
147
148 Compares the two SQL statements and the two lists of bind values. Returns true
149 IFF they are equivalent and the bind values are the same.
150
151
152 =head1 SEE ALSO
153
154 L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>.
155
156 =head1 AUTHOR
157
158 Norbert Buchmuller, <norbi@nix.hu>
159
160 =head1 COPYRIGHT AND LICENSE
161
162 Copyright 2008 by Norbert Buchmuller.
163
164 This library is free software; you can redistribute it and/or modify
165 it under the same terms as Perl itself.