-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchecks.sql
145 lines (128 loc) · 5.1 KB
/
checks.sql
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
-- vim: set foldmethod=marker
\echo 'NEO-Crisis <http://github.com/dutc/neocrisis>'
\echo 'James Powell <[email protected]>'
\set VERBOSITY terse
\set ON_ERROR_STOP true
create function pg_temp.check_hits(num integer) -- {{{
returns void as $func$
begin
assert array_length(array(select 1 from hits), 1) = num, 'too many/few hits';
assert array_length(array(select distinct rock from hits), 1) = num
, 'repeated rock';
assert array_length(array(select distinct slug from hits), 1) = num
, 'repeated slug';
end;
$func$ language plpgsql; -- }}}
create function pg_temp.insert_slug( -- {{{
name text
, params slug_params
, fired timestamp with time zone default now()
)
returns void as $func$
begin
insert into slugs (name, fired, params)
values (name, fired, params);
end;
$func$ language plpgsql; -- }}}
create function pg_temp.insert_rock( -- {{{
name text
, params rock_params
, fired timestamp with time zone default now()
, mass integer default 4
)
returns void as $func$
begin
insert into rocks (name, fired, mass, params)
values (name, fired, mass, params);
end;
$func$ language plpgsql; -- }}}
do $$
declare
_rec record;
begin
set search_path = game, public;
raise info 'check initial';
perform pg_temp.check_hits(4);
assert (array(select slug from api.hits where rock = 'ceres'))[1]
= '100 @ ceres (hit)', 'wrong hit for ceres';
assert (array(select slug from api.hits where rock = 'ceres I'))[1]
= '700 @ ceres (miss - late)', 'wrong hit for ceres I';
assert (array(select slug from api.hits where rock = 'tycho'))[1]
= '600 @ tycho (hit)', 'wrong hit for tycho';
raise info 'check insert slug';
perform pg_temp.insert_slug('800 @ luna (hit)', row(3, 3, c()));
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'luna'))[1]
= '800 @ luna (hit)', 'wrong hit for luna';
raise info 'check insert slug miss';
perform pg_temp.insert_slug('900 @ ganymede (miss)', row(pi_()/4, pi_()/4, 0));
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'ganymede'))[1]
is null, 'wrong hit for ganymede';
raise info 'check update slug (miss → hit)';
update slugs
set name = '900 @ ganymede (hit)', params.v = c()
where name = '900 @ ganymede (miss)';
perform pg_temp.check_hits(6);
assert (array(select slug from api.hits where rock = 'ganymede'))[1]
= '900 @ ganymede (hit)', 'wrong hit for ganymede';
raise info 'check delete slug';
delete from slugs
where name = '900 @ ganymede (hit)';
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'ganymede'))[1]
is null, 'wrong hit for ganymede';
raise info 'check update slug (hit → faster hit)';
update slugs
set params.v = 100 * c(), name = '700 @ ceres (hit)'
where name = '700 @ ceres (miss - late)';
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'ceres'))[1]
= '700 @ ceres (hit)', 'wrong hit for ceres';
assert (array(select slug from api.hits where rock = 'ceres I'))[1]
= '100 @ ceres (hit)', 'wrong hit for ceres I';
raise info 'check update rock (lower mass)';
update rocks
set mass = 1
where name = 'ceres';
perform pg_temp.check_hits(4);
assert (array(select slug from api.hits where rock = 'ceres'))[1]
= '700 @ ceres (hit)', 'wrong hit for ceres';
raise info 'check update rock (increase mass)';
update rocks
set mass = 4
where name = 'ceres';
perform pg_temp.check_hits(5);
raise info 'check update rock (move → miss)';
update rocks
set params.b_theta = (params).b_theta + pi_() / 8
where name = 'eros';
perform pg_temp.check_hits(4);
assert (array(select slug from api.hits where rock = 'eros'))[1]
is null, 'wrong hit for eros';
assert array_length(array(select name from rocks where name like 'eros%'), 1)
= 1, 'incorrect eros fragments';
raise info 'check update rock (move → hit)';
update rocks
set params.b_theta = (params).b_theta - pi_() / 8
where name = 'eros';
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'eros'))[1]
= '400 @ eros (hit)', 'wrong hit for eros';
assert array_length(array(select name from rocks where name like 'eros%'), 1)
= 2, 'incorrect eros fragments';
raise info 'check insert rock';
perform pg_temp.insert_rock('venus', row(0, 3, 0, 3, c() * 1800, 0));
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'luna'))[1]
is null, 'wrong hit for luna';
assert (array(select slug from api.hits where rock = 'venus'))[1]
= '800 @ luna (hit)', 'wrong hit for venus';
raise info 'check delete rock';
delete from rocks
where name = 'venus';
perform pg_temp.check_hits(5);
assert (array(select slug from api.hits where rock = 'luna'))[1]
= '800 @ luna (hit)', 'wrong hit for luna';
raise info 'all checks passed';
end $$;