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