Debian Codegraph @

t/dumptruck.t

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