Debian Codegraph @

t/dumptruck.t

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/usr/bin/perl
2
3 use Test::More tests => 43;
4 use Test::Exception;
5 use Test::Deep;
6 use File::Temp;
7
8 use strict;
9 use warnings;
10 use utf8;
11
12 BEGIN { use_ok ('Database::DumpTruck'); }
13 my $dbname = new File::Temp (EXLOCK => 0);
14
15 # Initial data store initializaion and checks.
16
17 my $dt1 = new Database::DumpTruck { dbname => "$dbname" };
18
19 throws_ok { $dt1->drop } qr/no such table: dumptruck/,
20 'Nonexistent table drop attempt dies';
21
22 throws_ok { $dt1->dump } qr/no such table: dumptruck/,
23 'Nonexistent table dump attempt dies';
24
25 is_deeply ([$dt1->insert ({ Hello => 'World' })], [1],
26 'Insert of single row/column successful');
27 is_deeply ($dt1->dump, [
28 { hello => 'World' },
29 ], 'Database contents after single row/column are sound');
30
31 throws_ok { $dt1->insert ([]) } qr/No data passed/,
32 'Attempt of an empty insert dies';
33
34 is_deeply ([$dt1->insert ([
35 { Hello => 'World' },
36 ])], [2], 'Insert of another row/column successful');
37 is_deeply ($dt1->dump, [
38 { hello => 'World' },
39 { hello => 'World' },
40 ], 'Database contents after insert of another row/column are sound');
41
42 is_deeply ([$dt1->insert ({})], [3],
43 'Empty row insert attempt successful');
44 is_deeply ($dt1->dump, [
45 { hello => 'World' },
46 { hello => 'World' },
47 { hello => undef },
48 ], 'Database contents after empty row insert are sound');
49
50 is_deeply ([$dt1->insert ({ beast => 666 })], [4],
51 'Insert of new column successful');
52 is_deeply ($dt1->dump, [
53 { hello => 'World', beast => undef },
54 { hello => 'World', beast => undef },
55 { hello => undef, beast => undef },
56 { hello => undef, beast => 666 },
57 ], 'Database contents after insert of new column are sound');
58
59 is_deeply ([$dt1->insert ([
60 { beast => 666 },
61 { hello => 'Yolo' },
62 { beast => 666, hello => 'Yolo' },
63 ])], [5, 6, 7], 'Insert of multiple rows successful');
64 is_deeply ($dt1->dump, [
65 { hello => 'World', beast => undef },
66 { hello => 'World', beast => undef },
67 { hello => undef, beast => undef },
68 { hello => undef, beast => 666 },
69 { hello => undef, beast => 666 },
70 { hello => 'Yolo', beast => undef },
71 { beast => 666, hello => 'Yolo' },
72 ], 'Database contents after insert of multiple rows are sound');
73
74 is_deeply ($dt1->close, undef, 'Database close successful');
75
76 # Reopening the database with two clients now.
77 # One of them does not commit immediately.
78
79 my $dt2 = new Database::DumpTruck { dbname => "$dbname", auto_commit => 0 };
80 my $dt3 = new Database::DumpTruck { dbname => "$dbname" };
81
82 is_deeply ($dt2->drop, [], 'Delayed drop attempt seems successful');
83 throws_ok { $dt2->drop } qr/no such table: dumptruck/,
84 'Table does not seem to exist';
85
86 is_deeply ($dt3->dump, [
87 { hello => 'World', beast => undef },
88 { hello => 'World', beast => undef },
89 { hello => undef, beast => undef },
90 { hello => undef, beast => 666 },
91 { hello => undef, beast => 666 },
92 { hello => 'Yolo', beast => undef },
93 { beast => 666, hello => 'Yolo' },
94 ], 'Database contents still actually there');
95
96 is_deeply ([$dt2->commit], [1], 'Committing the drop successful');
97
98 throws_ok { $dt3->dump } qr/no such table: dumptruck/,
99 'Data are gone now';
100
101 # Operate on another table while checking constrains work fine
102
103 is_deeply ($dt3->create_table ({ hello => 'World', goodbye => 'Heavens' },
104 'table2'), '', 'Created a new table');
105 is_deeply ($dt3->dump ('table2'), [], 'Table is initially empty');
106 is_deeply ($dt3->create_index (['hello'], 'table2', undef, 1), [],
107 'Created an unique index');
108 is_deeply ([$dt3->insert ({ hello => 'World', goodbye => 'Heavens' },
109 'table2')], [1], 'Added a row');
110 is_deeply ($dt3->dump ('table2'), [
111 { hello => 'World', goodbye => 'Heavens' }
112 ], 'The row is there');
113 throws_ok { $dt3->insert ({ hello => 'World', goodbye => 'Hell' }, 'table2') }
114 qr/column hello is not unique|UNIQUE constraint failed: table2.hello/,
115 'Constrain violation caught';
116 is_deeply ([$dt3->upsert ({ hello => 'World', goodbye => 'Pandemonium' },
117 'table2')], [2], 'Updated a row');
118 is_deeply ($dt3->dump ('table2'), [
119 { hello => 'World', goodbye => 'Pandemonium' }
120 ], 'The row is updated');
121
122 # Verify that the variables work
123
124 is_deeply ($dt3->save_var('number_of_the_beast', 666), [],
125 'Variable inserted');
126 is ($dt3->get_var('number_of_the_beast'), 666,
127 'Variable retrieved');
128 is_deeply ($dt3->save_var('number_of_the_beast', 8086), [],
129 'Variable updated');
130 is ($dt3->get_var('number_of_the_beast'), 8086,
131 'Updated variable retrieved');
132 is_deeply ($dt3->save_var('array_of_the_beast', [666]), [],
133 'Array variable inserted');
134 is_deeply ($dt3->get_var('array_of_the_beast'), [666],
135 'Array variable retrieved');
136 is_deeply ($dt3->save_var('undef_of_the_beast', undef), [],
137 'Undefined variable inserted');
138 is_deeply ($dt3->get_var('undef_of_the_beast'), undef,
139 'Undefined variable retrieved');
140
141 # And some low-level stuff
142 cmp_deeply ($dt3->column_names ('table2'), [
143 { notnull => 0, pk => 0, name => 'goodbye', type => re(qr/^text$/i),
144 cid => 0, dflt_value => undef },
145 { notnull => 0, pk => 0, name => 'hello', type => re(qr/^text$/i),
146 cid => 1, dflt_value => undef }
147 ], 'Could retrieve table structure');
148
149 is_deeply ([$dt3->tables], ['table2', '_dumptruckvars'],
150 'Table list fine');
151
152 is_deeply ($dt3->execute ('DELETE FROM table2'), [],
153 'Issued a raw SQL statement');
154 is_deeply ($dt3->dump ('table2'), [],
155 'The statement run correctly');
156
157 # Try some structured and typed data
158
159 is_deeply ([$dt3->insert ({
160 name => 'Behemoth',
161 age => 666,
162 yes => !!1,
163 wide => 'Pišišvorík',
164 foo => undef,
165 random => {
166 name => 'Behemoth',
167 age => 666,
168 yes => !!1,
169 wide => 'Pišišvorík',
170 foo => undef,
171 }
172 })], [1], 'Insert of structured data successful');
173
174 cmp_deeply ($dt3->column_names, [
175 { notnull => 0, pk => 0, name => 'age', type => re(qr/^integer$/i),
176 cid => 0, dflt_value => undef },
177 { notnull => 0, pk => 0, name => 'foo', type => '',
178 cid => 1, dflt_value => undef },
179 { notnull => 0, pk => 0, name => 'name', type => re(qr/^text$/i),
180 cid => 2, dflt_value => undef },
181 { notnull => 0, pk => 0, name => 'random', type => 'json text',
182 cid => 3, dflt_value => undef },
183 { notnull => 0, pk => 0, name => 'wide', type => re(qr/^text$/i),
184 cid => 4, dflt_value => undef },
185 { notnull => 0, pk => 0, name => 'yes', type => 'bool',
186 cid => 5, dflt_value => undef }
187 ], 'Proper table structure creates');
188
189 is_deeply ($dt3->dump, [{
190 name => 'Behemoth',
191 age => 666,
192 yes => !!1,
193 wide => 'Pišišvorík',
194 foo => undef,
195 random => {
196 name => 'Behemoth',
197 age => 666,
198 yes => !!1,
199 wide => 'Pišišvorík',
200 foo => undef,
201 }
202 }], 'Proper data was retrieved from the database');