File: dumptruck.t

package info (click to toggle)
libdatabase-dumptruck-perl 1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 128 kB
  • sloc: perl: 427; makefile: 2
file content (202 lines) | stat: -rw-r--r-- 6,518 bytes parent folder | download | duplicates (2)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/perl

use Test::More tests => 43;
use Test::Exception;
use Test::Deep;
use File::Temp;

use strict;
use warnings;
use utf8;

BEGIN { use_ok ('Database::DumpTruck'); }
my $dbname = new File::Temp (EXLOCK => 0);

# Initial data store initializaion and checks.

my $dt1 = new Database::DumpTruck { dbname => "$dbname" };

throws_ok { $dt1->drop } qr/no such table: dumptruck/,
	'Nonexistent table drop attempt dies';

throws_ok { $dt1->dump } qr/no such table: dumptruck/,
	'Nonexistent table dump attempt dies';

is_deeply ([$dt1->insert ({ Hello => 'World' })], [1],
	'Insert of single row/column successful');
is_deeply ($dt1->dump, [
	{ hello => 'World' },
], 'Database contents after single row/column are sound');

throws_ok { $dt1->insert ([]) } qr/No data passed/,
	'Attempt of an empty insert dies';

is_deeply ([$dt1->insert ([
	{ Hello => 'World' },
])], [2], 'Insert of another row/column successful');
is_deeply ($dt1->dump, [
	{ hello => 'World' },
	{ hello => 'World' },
], 'Database contents after insert of another row/column are sound');

is_deeply ([$dt1->insert ({})], [3],
	'Empty row insert attempt successful');
is_deeply ($dt1->dump, [
	{ hello => 'World' },
	{ hello => 'World' },
	{ hello => undef },
], 'Database contents after empty row insert are sound');

is_deeply ([$dt1->insert ({ beast => 666 })], [4],
	'Insert of new column successful');
is_deeply ($dt1->dump, [
	{ hello => 'World', beast => undef },
	{ hello => 'World', beast => undef },
	{ hello => undef, beast => undef },
	{ hello => undef, beast => 666 },
], 'Database contents after insert of new column are sound');

is_deeply ([$dt1->insert ([
	{ beast => 666 },
	{ hello => 'Yolo' },
	{ beast => 666, hello => 'Yolo' },
])], [5, 6, 7], 'Insert of multiple rows successful');
is_deeply ($dt1->dump, [
	{ hello => 'World', beast => undef },
	{ hello => 'World', beast => undef },
	{ hello => undef, beast => undef },
	{ hello => undef, beast => 666 },
	{ hello => undef, beast => 666 },
	{ hello => 'Yolo', beast => undef },
	{ beast => 666, hello => 'Yolo' },
], 'Database contents after insert of multiple rows are sound');

is_deeply ($dt1->close, undef, 'Database close successful');

# Reopening the database with two clients now.
# One of them does not commit immediately.

my $dt2 = new Database::DumpTruck { dbname => "$dbname", auto_commit => 0 };
my $dt3 = new Database::DumpTruck { dbname => "$dbname" };

is_deeply ($dt2->drop, [], 'Delayed drop attempt seems successful');
throws_ok { $dt2->drop } qr/no such table: dumptruck/,
	'Table does not seem to exist';

is_deeply ($dt3->dump, [
	{ hello => 'World', beast => undef },
	{ hello => 'World', beast => undef },
	{ hello => undef, beast => undef },
	{ hello => undef, beast => 666 },
	{ hello => undef, beast => 666 },
	{ hello => 'Yolo', beast => undef },
	{ beast => 666, hello => 'Yolo' },
], 'Database contents still actually there');

is_deeply ([$dt2->commit], [1], 'Committing the drop successful');

throws_ok { $dt3->dump } qr/no such table: dumptruck/,
	'Data are gone now';

# Operate on another table while checking constrains work fine

is_deeply ($dt3->create_table ({ hello => 'World', goodbye => 'Heavens' },
	'table2'), '', 'Created a new table');
is_deeply ($dt3->dump ('table2'), [], 'Table is initially empty');
is_deeply ($dt3->create_index (['hello'], 'table2', undef, 1), [],
	'Created an unique index');
is_deeply ([$dt3->insert ({ hello => 'World', goodbye => 'Heavens' },
	'table2')], [1], 'Added a row');
is_deeply ($dt3->dump ('table2'), [
	{ hello => 'World', goodbye => 'Heavens' }
], 'The row is there');
throws_ok { $dt3->insert ({ hello => 'World', goodbye => 'Hell' }, 'table2') }
	qr/column hello is not unique|UNIQUE constraint failed: table2.hello/,
	'Constrain violation caught';
is_deeply ([$dt3->upsert ({ hello => 'World', goodbye => 'Pandemonium' },
	'table2')], [2], 'Updated a row');
is_deeply ($dt3->dump ('table2'), [
	{ hello => 'World', goodbye => 'Pandemonium' }
], 'The row is updated');

# Verify that the variables work

is_deeply ($dt3->save_var('number_of_the_beast', 666), [],
	'Variable inserted');
is ($dt3->get_var('number_of_the_beast'), 666,
	'Variable retrieved');
is_deeply ($dt3->save_var('number_of_the_beast', 8086), [],
	'Variable updated');
is ($dt3->get_var('number_of_the_beast'), 8086,
	'Updated variable retrieved');
is_deeply ($dt3->save_var('array_of_the_beast', [666]), [],
	'Array variable inserted');
is_deeply ($dt3->get_var('array_of_the_beast'), [666],
	'Array variable retrieved');
is_deeply ($dt3->save_var('undef_of_the_beast', undef), [],
	'Undefined variable inserted');
is_deeply ($dt3->get_var('undef_of_the_beast'), undef,
	'Undefined variable retrieved');

# And some low-level stuff
cmp_deeply ($dt3->column_names ('table2'), [
	{ notnull => 0, pk => 0, name => 'goodbye', type => re(qr/^text$/i),
		cid => 0, dflt_value => undef },
	{ notnull => 0, pk => 0, name => 'hello', type => re(qr/^text$/i),
		cid => 1, dflt_value => undef }
], 'Could retrieve table structure');

is_deeply ([$dt3->tables], ['table2', '_dumptruckvars'],
	'Table list fine');

is_deeply ($dt3->execute ('DELETE FROM table2'), [],
	'Issued a raw SQL statement');
is_deeply ($dt3->dump ('table2'), [],
	'The statement run correctly');

# Try some structured and typed data

is_deeply ([$dt3->insert ({
	name => 'Behemoth',
	age => 666,
	yes => !!1,
	wide => 'Pišišvorík',
	foo => undef,
	random => {
		name => 'Behemoth',
		age => 666,
		yes => !!1,
		wide => 'Pišišvorík',
		foo => undef,
	}
})], [1], 'Insert of structured data successful');

cmp_deeply ($dt3->column_names, [
	{ notnull => 0, pk => 0, name => 'age', type => re(qr/^integer$/i),
		cid => 0, dflt_value => undef },
	{ notnull => 0, pk => 0, name => 'foo', type => '',
		cid => 1, dflt_value => undef },
	{ notnull => 0, pk => 0, name => 'name', type => re(qr/^text$/i),
		cid => 2, dflt_value => undef },
	{ notnull => 0, pk => 0, name => 'random', type => 'json text',
		cid => 3, dflt_value => undef },
	{ notnull => 0, pk => 0, name => 'wide', type => re(qr/^text$/i),
		cid => 4, dflt_value => undef },
	{ notnull => 0, pk => 0, name => 'yes', type => 'bool',
		cid => 5, dflt_value => undef }
], 'Proper table structure creates');

is_deeply ($dt3->dump, [{
	name => 'Behemoth',
	age => 666,
	yes => !!1,
	wide => 'Pišišvorík',
	foo => undef,
	random => {
		name => 'Behemoth',
		age => 666,
		yes => !!1,
		wide => 'Pišišvorík',
		foo => undef,
	}
}], 'Proper data was retrieved from the database');