Cleanup test and add a failing test case against RT#43483
[scpubgit/Q-Branch.git] / t / 03values.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6
7 use SQL::Abstract::Test import => [qw/is_same_sql_bind is_same_bind/];
8
9 use SQL::Abstract;
10
11 my @data = (
12     {
13         user => 'nwiger',
14         name => 'Nathan Wiger',
15         phone => '123-456-7890',
16         addr => 'Yeah, right',
17         city => 'Milwalkee',
18         state => 'Minnesota',
19     },
20
21     {
22         user => 'jimbo',
23         name => 'Jimbo Bobson',
24         phone => '321-456-0987',
25         addr => 'Yo Momma',
26         city => 'Yo City',
27         state => 'Minnesota',
28     },
29
30     {
31         user => 'mr.hat',
32         name => 'Mr. Garrison',
33         phone => '123-456-7890',
34         addr => undef,
35         city => 'South Park',
36         state => 'CO',
37     },
38
39     {
40         user => 'kennyg',
41         name => undef,
42         phone => '1-800-Sucky-Sucky',
43         addr => 'Mr. Garrison',
44         city => undef,
45         state => 'CO',
46     },
47
48     {
49         user => 'barbara_streisand',
50         name => 'MechaStreisand!',
51         phone => 0,
52         addr => -9230992340,
53         city => 42,
54         state => 'CO',
55     },
56 );
57
58
59 plan tests => (@data * 5  +  2);
60
61 # test insert() and values() for reentrancy
62 my($insert_hash, $insert_array, $numfields);
63 my $a_sql = SQL::Abstract->new;
64 my $h_sql = SQL::Abstract->new;
65
66 for my $record (@data) {
67
68   my $values = [ map { $record->{$_} } sort keys %$record ];
69
70   my ($h_stmt, @h_bind) = $h_sql->insert('h_table', $record);
71   my ($a_stmt, @a_bind) = $a_sql->insert('a_table', $values );
72
73   # init from first run, should not change afterwards
74   $insert_hash ||= $h_stmt;
75   $insert_array ||= $a_stmt;
76   $numfields ||= @$values;
77
78   is ( $a_stmt, $insert_array, 'Array-based insert statement unchanged' );
79   is ( $h_stmt, $insert_hash, 'Hash-based insert statement unchanged' );
80
81   is_deeply ( \@a_bind, \@h_bind, 'Bind values match after both insert() calls' );
82   is_deeply ( [$h_sql->values ($record)] , \@h_bind, 'values() output matches bind values after insert()' );
83
84   is ( scalar @h_bind, $numfields, 'Number of fields unchanged' );
85 }
86
87 # test values() with literal sql
88 {
89   my $sql = SQL::Abstract->new;
90
91   my $data = { event => 'rapture', time => \ 'now()', func => \ 'somefunc(?)', stuff => 'fluff', };
92
93   my ($stmt, @bind) = $sql->insert ('table', $data);
94
95   is_same_sql_bind (
96     $stmt,
97     \@bind,
98     'INSERT INTO table ( event, func, stuff, time) VALUES ( ?, somefunc (?), ?, now() )',
99     [qw/rapture fluff/],  # event < stuff
100   );
101
102   is_same_bind (
103     [$sql->values ($data)],
104     [\@bind],
105     'values() output matches that of initial bind'
106   );
107 }