Converted all test cases where the tests are queued in an array to calculate the...
[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 => ['is_same_sql_bind'];
8
9 use SQL::Abstract;
10
11 my $sql = SQL::Abstract->new;
12
13 my @data = (
14     {
15         user => 'nwiger',
16         name => 'Nathan Wiger',
17         phone => '123-456-7890',
18         addr => 'Yeah, right',
19         city => 'Milwalkee',
20         state => 'Minnesota',
21     },
22
23     {
24         user => 'jimbo',
25         name => 'Jimbo Bobson',
26         phone => '321-456-0987',
27         addr => 'Yo Momma',
28         city => 'Yo City',
29         state => 'Minnesota',
30     },
31
32     {
33         user => 'mr.hat',
34         name => 'Mr. Garrison',
35         phone => '123-456-7890',
36         addr => undef,
37         city => 'South Park',
38         state => 'CO',
39     },
40
41     {
42         user => 'kennyg',
43         name => undef,
44         phone => '1-800-Sucky-Sucky',
45         addr => 'Mr. Garrison',
46         city => undef,
47         state => 'CO',
48     },
49
50     {
51         user => 'barbara_streisand',
52         name => 'MechaStreisand!',
53         phone => 0,
54         addr => -9230992340,
55         city => 42,
56         state => 'CO',
57     },
58 );
59
60
61 plan tests => scalar(@data);
62
63 # Note to self: I have no idea what this does anymore
64 # It looks like a cool fucking segment of code though!
65 # I just wish I remembered writing it... :-\
66
67 my($sth, $stmt);
68 my($laststmt, $numfields);
69 for my $t (@data) {
70       local $"=', ';
71
72       $stmt = $sql->insert('yo_table', $t);
73       my @val = $sql->values($t);
74       $numfields ||= @val;
75
76       ok((! $laststmt || $stmt eq $laststmt) && @val == $numfields
77           && equal(\@val, [map { $t->{$_} } sort keys %$t])) or
78               print "got\n",
79                     "[$stmt] [@val]\n",
80                     "instead of\n",
81                     "[$t->{stmt}] [stuff]\n\n";
82       $laststmt = $stmt;
83 }
84
85 sub equal {
86       my ($a, $b) = @_;
87       return 0 if @$a != @$b;
88       for (my $i = 0; $i < $#{$a}; $i++) {
89               next if (! defined($a->[$i])) && (! defined($b->[$i]));
90               return 0 if $a->[$i] ne $b->[$i];
91       }
92       return 1;
93 }
94