File: Test.pm

package info (click to toggle)
libsql-abstract-limit-perl 2%3A0.14.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 148 kB
  • ctags: 31
  • sloc: perl: 530; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 3,480 bytes parent folder | download | duplicates (4)
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
package SQL::Abstract::Limit::Test;

# Lifted from DBIx::Class, originally was DBIC::SqlMakerTest.

use strict;
use warnings;

use base qw/Test::Builder::Module Exporter/;

use Exporter;

our @EXPORT = qw/
  &is_same_sql_bind
  &eq_sql
  &eq_bind
/;


{
  package DBIC::SqlMakerTest::SQLATest;

  # replacement for SQL::Abstract::Test if not available

  use strict;
  use warnings;

  use base qw/Test::Builder::Module Exporter/;

  use Scalar::Util qw(looks_like_number blessed reftype);
  use Data::Dumper;
  use Test::Builder;
  use Test::Deep qw(eq_deeply);

  our $tb = __PACKAGE__->builder;

  sub is_same_sql_bind
  {
    my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_;

    my $same_sql = eq_sql($sql1, $sql2);
    my $same_bind = eq_bind($bind_ref1, $bind_ref2);

    $tb->ok($same_sql && $same_bind, $msg);

    if (!$same_sql) {
      $tb->diag("SQL expressions differ\n"
        . "     got: $sql1\n"
        . "expected: $sql2\n"
      );
    }
    if (!$same_bind) {
      $tb->diag("BIND values differ\n"
        . "     got: " . Dumper($bind_ref1)
        . "expected: " . Dumper($bind_ref2)
      );
    }
  }

  sub eq_sql
  {
    my ($left, $right) = @_;

    $left =~ s/\s+//g;
    $right =~ s/\s+//g;

    return $left eq $right;
  }

  sub eq_bind
  {
    my ($bind_ref1, $bind_ref2) = @_;

    return eq_deeply($bind_ref1, $bind_ref2);
  }
}

eval "use SQL::Abstract::Test;";
if ($@ eq '') {
  # SQL::Abstract::Test available

  *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
  *eq_sql = \&SQL::Abstract::Test::eq_sql;
  *eq_bind = \&SQL::Abstract::Test::eq_bind;
} else {
  # old SQL::Abstract

  *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
  *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
  *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
}


1;

__END__


=head1 NAME

SQL::Abstract::Limit::Test - Helper package for testing generated SQL and bind values

=head1 SYNOPSIS

  use Test::More;
  use SQL::Abstract::Limit::Test;
  
  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.

If L<SQL::Abstract::Test> (packaged in L<SQL::Abstract> versions 1.50 and
above) is available, then it is used to perform the comparisons (all functions
are delegated to id). Otherwise uses simple string comparison for the SQL
statements and simple L<Data::Dumper>-like recursive stringification for
comparison of bind values.


=head1 FUNCTIONS

=head2 is_same_sql_bind

  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 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.


=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.