File: 06multiple.t

package info (click to toggle)
libtest-databaserow-perl 2.04-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 296 kB
  • ctags: 177
  • sloc: perl: 3,062; makefile: 2
file content (189 lines) | stat: -rw-r--r-- 5,119 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/perl -w

use strict;

use Test::More tests => 13;

use Test::DatabaseRow;
use Test::Builder::Tester;

$Test::DatabaseRow::dbh = FakeDBI->new(results => 2);

test_out("ok 1 - matches");
row_ok(table => "dummy",
       where => [ dummy => "dummy" ],
       tests => [ fooid => 123,
                  name  => "fred",
                  name  => qr/re/  ],
       label => "matches");
test_test("basic");

test_out("ok 1 - matches");
row_ok(table   => "dummy",
       where   => [ dummy => "dummy" ],
       results => 2,
       label   => "matches");
test_test("right number");

test_out("ok 1 - matches");
row_ok(table       => "dummy",
       where       => [ dummy => "dummy" ],
       min_results => 2,
       label       => "matches");
test_test("right number, min");

test_out("ok 1 - matches");
row_ok(table       => "dummy",
       where       => [ dummy => "dummy" ],
       max_results => 2,
       label       => "matches");
test_test("right number, max");

test_out("not ok 1 - matches");
test_fail(+4);
test_diag("Got the wrong number of rows back from the database.");
test_diag("  got:      2 rows back");
test_diag("  expected: 3 rows back");
row_ok(table   => "dummy",
       where   => [ dummy => "dummy" ],
       results => 3,
       label   => "matches");
test_test("wrong number");

test_out("not ok 1 - matches");
test_fail(+4);
test_diag("Got too few rows back from the database.");
test_diag("  got:      2 rows back");
test_diag("  expected: 3 rows or more back");
row_ok(table   => "dummy",
       where   => [ dummy => "dummy" ],
       min_results => 3,
       label   => "matches");
test_test("wrong number, min");

test_out("not ok 1 - matches");
test_fail(+4);
test_diag("Got too many rows back from the database.");
test_diag("  got:      2 rows back");
test_diag("  expected: 1 rows or fewer back");
row_ok(table   => "dummy",
       where   => [ dummy => "dummy" ],
       max_results => 1,
       label   => "matches");
test_test("wrong number, max");

$Test::DatabaseRow::dbh = FakeDBI->new(results => 0);

test_out("ok 1 - matches");
not_row_ok(table   => "dummy",
           where   => [ dummy => "dummy" ],
           label   => "matches");
test_test("not_row");

$Test::DatabaseRow::dbh = FakeDBI->new(results => 3);

test_out("ok 1 - matches");
all_row_ok(table   => "dummy",
           where   => [ dummy => "dummy" ],
           tests   => [ name => qr/e/ ],
           label   => "matches");
test_test("all_row_ok pass");

test_out("not ok 1 - matches");
test_fail(+4);           
test_diag("While checking column 'name' on 2nd row");
test_diag("         got: 'bert'");
test_diag("    expected: 'fred'");
all_row_ok(table   => "dummy",
           where   => [ dummy => "dummy" ],
           tests   => [ name => "fred" ],
           label   => "matches");
test_test("all_row_ok fail");

test_out("not ok 1 - matches");
test_fail(+2);           
test_diag("No 4th row");
Test::DatabaseRow::Object->new(
  dbh          => FakeDBI->new(results => 2),
  sql_and_bind => "dummy",
)->row_at_index_ok(3)->pass_to_test_builder("matches");
test_test("missing row");

test_out("ok 1 - matches");
Test::DatabaseRow::Object->new(
  dbh          => FakeDBI->new(results => 2),
  sql_and_bind => "dummy",
)->row_at_index_ok(1)->pass_to_test_builder("matches");
test_test("row_at_index_ok with no tests");

$Test::DatabaseRow::dbh = FakeDBI->new(results => 4);

# note the following test also checks undef <-> NULL handing
test_out("not ok 1 - matches");
test_fail(+10);           
test_diag("While checking column 'gender' on 4th row");
test_diag("         got: NULL");
test_diag("    expected: 'm'");
test_diag("The SQL executed was:");
test_diag("  dummy sql");
test_diag("The bound parameters were:");
test_diag("  '7'");
test_diag("  undef");
test_diag("on database 'fakename'");
all_row_ok(
  sql     => ["dummy sql",7,undef],
  tests   => [ gender => "m" ],
  label   => "matches",
  verbose => 1,
);
test_test("verbose");

# fake database package
package FakeDBI;
sub new { my $class = shift; return bless { @_, Name => "fakename" }, $class };
sub quote { return "qtd<$_[1]>" };

sub prepare
{
  my $this = shift;

  # die if we need to
  if ($this->fallover)
    { die "Khaaaaaaaaaaaaan!" }

  return FakeSTH->new($this);
}

sub results  { return $_[0]->{results}  }
sub nomatch  { return $_[0]->{nomatch}  }
sub fallover { return $_[0]->{fallover} }

package FakeSTH;
sub new { return bless { parent => $_[1] }, $_[0] };
sub execute { return 1 };

sub fetchrow_hashref
{
  my $this = shift;
  my $parent = $this->{parent};

  $this->{returned}++;

  return if $parent->nomatch;
  return if $this->{returned} > $parent->results;

  if ($this->{returned} == 1)
    { return { fooid => 123, name => "fred", gender => 'm'} }

  if ($this->{returned} == 2)
    { return { fooid => 124, name => "bert", gender => 'm'} }

  if ($this->{returned} == 3)
    { return { fooid => 125, name => "ernie", gender => 'm'} }

  if ($this->{returned} == 4)
    { return { fooid => 125, name => undef, gender => undef } }

  # oops, someone wanted more results than we prepared
  return;
}