1package DBIC::SqlMakerTest; 2 3use strict; 4use warnings; 5 6use base qw/Exporter/; 7 8use Carp; 9use SQL::Abstract::Test; 10 11our @EXPORT = qw/ 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 20/; 21 22sub 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 541; 55 56__END__ 57 58 59=head1 NAME 60 61DBIC::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 77Exports functions that can be used to compare generated SQL and bind values. 78 79This is a thin wrapper around L<SQL::Abstract::Test>, which makes it easier 80to 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 104Compares given and expected pairs of C<($sql, \@bind)>, and calls 105L<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 115Compares given and expected SQL statement, and calls L<Test::Builder/ok> on the 116result, 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 126Compares given and expected bind value lists, and calls L<Test::Builder/ok> on 127the result, with C<$test_msg> as message. 128 129=head2 eq_sql 130 131 my $is_same = eq_sql($given_sql, $expected_sql); 132 133Compares 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 139Compares 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 148Compares the two SQL statements and the two lists of bind values. Returns true 149IFF they are equivalent and the bind values are the same. 150 151 152=head1 SEE ALSO 153 154L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>. 155 156=head1 AUTHOR 157 158Norbert Buchmuller, <norbi@nix.hu> 159 160=head1 COPYRIGHT AND LICENSE 161 162Copyright 2008 by Norbert Buchmuller. 163 164This library is free software; you can redistribute it and/or modify 165it under the same terms as Perl itself. 166