File: SqlMakerTest.pm

package info (click to toggle)
libdbix-class-perl 0.08123-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,520 kB
  • ctags: 1,695
  • sloc: perl: 19,821; sql: 353; makefile: 10
file content (165 lines) | stat: -rw-r--r-- 3,305 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package DBIC::SqlMakerTest;

use strict;
use warnings;

use base qw/Exporter/;

use Carp;
use SQL::Abstract::Test;

our @EXPORT = qw/
  is_same_sql_bind
  is_same_sql
  is_same_bind
/;
our @EXPORT_OK = qw/
  eq_sql
  eq_bind
  eq_sql_bind
/;

sub is_same_sql_bind {
  # unroll possible as_query arrayrefrefs
  my @args;

  for (1,2) {
    my $chunk = shift @_;

    if ( ref $chunk eq 'REF' and ref $$chunk eq 'ARRAY' ) {
      my ($sql, @bind) = @$$chunk;
      push @args, ($sql, \@bind);
    }
    else {
      push @args, $chunk, shift @_;
    }

  }

  push @args, shift @_;

  croak "Unexpected argument(s) supplied to is_same_sql_bind: " . join ('; ', @_)
    if @_;

  @_ = @args;
  goto &SQL::Abstract::Test::is_same_sql_bind;
}

*is_same_sql = \&SQL::Abstract::Test::is_same_sql;
*is_same_bind = \&SQL::Abstract::Test::is_same_bind;
*eq_sql = \&SQL::Abstract::Test::eq_sql;
*eq_bind = \&SQL::Abstract::Test::eq_bind;
*eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind;

1;

__END__


=head1 NAME

DBIC::SqlMakerTest - Helper package for testing sql_maker component of DBIC

=head1 SYNOPSIS

  use Test::More;
  use DBIC::SqlMakerTest;
  
  my ($sql, @bind) = $schema->storage->sql_maker->select(%args);
  is_same_sql_bind(
    $sql, \@bind, 
    $expected_sql, \@expected_bind,
    'foo bar works'
  );

=head1 DESCRIPTION

Exports functions that can be used to compare generated SQL and bind values.

This is a thin wrapper around L<SQL::Abstract::Test>, which makes it easier
to compare as_query sql/bind arrayrefrefs directly.

=head1 FUNCTIONS

=head2 is_same_sql_bind

  is_same_sql_bind(
    $given_sql, \@given_bind,
    $expected_sql, \@expected_bind,
    $test_msg
  );

  is_same_sql_bind(
    $rs->as_query
    $expected_sql, \@expected_bind,
    $test_msg
  );

  is_same_sql_bind(
    \[$given_sql, @given_bind],
    $expected_sql, \@expected_bind,
    $test_msg
  );

Compares given and expected pairs of C<($sql, \@bind)>, and calls
L<Test::Builder/ok> on the result, with C<$test_msg> as message.

=head2 is_same_sql

  is_same_sql(
    $given_sql,
    $expected_sql,
    $test_msg
  );

Compares given and expected SQL statement, and calls L<Test::Builder/ok> on the
result, with C<$test_msg> as message.

=head2 is_same_bind

  is_same_bind(
    \@given_bind, 
    \@expected_bind,
    $test_msg
  );

Compares given and expected bind value lists, and calls L<Test::Builder/ok> on
the result, with C<$test_msg> as message.

=head2 eq_sql

  my $is_same = eq_sql($given_sql, $expected_sql);

Compares the two SQL statements. Returns true IFF they are equivalent.

=head2 eq_bind

  my $is_same = eq_sql(\@given_bind, \@expected_bind);

Compares two lists of bind values. Returns true IFF their values are the same.

=head2 eq_sql_bind

  my $is_same = eq_sql_bind(
    $given_sql, \@given_bind,
    $expected_sql, \@expected_bind
  );

Compares the two SQL statements and the two lists of bind values. Returns true
IFF they are equivalent and the bind values are the same.


=head1 SEE ALSO

L<SQL::Abstract::Test>, L<Test::More>, L<Test::Builder>.

=head1 AUTHOR

Norbert Buchmuller, <norbi@nix.hu>

=head1 COPYRIGHT AND LICENSE

Copyright 2008 by Norbert Buchmuller.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.