Skip to content

Commit b310706

Browse files
test: added database test specs
1 parent 2476211 commit b310706

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(4);
4+
5+
INSERT INTO rate_limit.sessions (name_, type_)
6+
SELECT
7+
md5(random()::text) AS name_,
8+
'aggregated' AS type_
9+
FROM pg_catalog.generate_series(1, 1000);
10+
11+
INSERT INTO rate_limit.sessions (id, name_, type_)
12+
SELECT
13+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
14+
'dedicated-test'::text AS name_,
15+
'aggregated'::text AS type_;
16+
17+
INSERT INTO rate_limit.records_aggregated (key, session_id, count)
18+
SELECT
19+
md5(random()::text) AS key_,
20+
id AS session_id,
21+
random() * 1000 + 1 AS count
22+
FROM rate_limit.sessions;
23+
24+
INSERT INTO rate_limit.records_aggregated (key, session_id, count)
25+
VALUES ('test-update', '00000000-0000-0000-0000-000000000000'::uuid, 20),
26+
('test-decrement', '00000000-0000-0000-0000-000000000000'::uuid, 30),
27+
('test-reset-key', '00000000-0000-0000-0000-000000000000'::uuid, 40);
28+
29+
PREPARE update_aggregation as
30+
INSERT INTO rate_limit.records_aggregated (key, session_id)
31+
VALUES ('test-update', '00000000-0000-0000-0000-000000000000')
32+
ON CONFLICT ON CONSTRAINT unique_session_key DO UPDATE
33+
SET count = records_aggregated.count + 1
34+
RETURNING count;
35+
36+
SELECT performs_ok(
37+
'update_aggregation',
38+
250,
39+
'inserting record should execute under 250ms'
40+
);
41+
42+
43+
PREPARE decrement_records as
44+
UPDATE rate_limit.records_aggregated
45+
SET count = greatest(0, count - 1)
46+
WHERE
47+
key = 'test-decrement'
48+
AND session_id = '00000000-0000-0000-0000-000000000000';
49+
50+
SELECT performs_ok(
51+
'decrement_records',
52+
250,
53+
'decrementing query execute under 250ms'
54+
);
55+
56+
PREPARE reset_key as
57+
DELETE FROM rate_limit.records_aggregated
58+
WHERE
59+
key = 'test-reset-key'
60+
AND session_id = '00000000-0000-0000-0000-000000000000';
61+
62+
63+
SELECT performs_ok(
64+
'reset_key',
65+
250,
66+
'resetting a key should execute under 250ms'
67+
);
68+
69+
PREPARE reset_all as
70+
DELETE FROM rate_limit.records_aggregated
71+
WHERE session_id = '00000000-0000-0000-0000-000000000000';
72+
73+
74+
SELECT performs_ok(
75+
'reset_all',
76+
250,
77+
'resetting a session should execute under 250ms'
78+
);
79+
80+
DEALLOCATE update_aggregation;
81+
DEALLOCATE decrement_records;
82+
DEALLOCATE reset_key;
83+
DEALLOCATE reset_all;
84+
85+
-- Finish the tests and clean up.
86+
SELECT finish FROM finish();
87+
ROLLBACK;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(5);
4+
5+
INSERT INTO rate_limit.sessions (name_, type_)
6+
SELECT
7+
md5(random()::text) AS name_,
8+
'individual' AS type_
9+
FROM pg_catalog.generate_series(1, 1000);
10+
11+
INSERT INTO rate_limit.sessions (id, name_, type_)
12+
SELECT
13+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
14+
'dedicated-test'::text AS name_,
15+
'individual'::text AS type_;
16+
17+
INSERT INTO rate_limit.individual_records (key, session_id)
18+
SELECT
19+
md5(random()::text) AS key_,
20+
session_.id AS session_id
21+
FROM rate_limit.sessions AS session_
22+
INNER JOIN pg_catalog.generate_series(1, 1000) ON true;
23+
24+
INSERT INTO rate_limit.individual_records (key, session_id)
25+
VALUES ('test-count', '00000000-0000-0000-0000-000000000000'::uuid),
26+
('test-decrement', '00000000-0000-0000-0000-000000000000'::uuid),
27+
('test-reset-key', '00000000-0000-0000-0000-000000000000'::uuid);
28+
29+
PREPARE insert_individual_record as
30+
INSERT INTO rate_limit.individual_records (key, session_id)
31+
VALUES ('test', '00000000-0000-0000-0000-000000000000'::uuid);
32+
33+
SELECT performs_ok(
34+
'insert_individual_record',
35+
250,
36+
'inserting record should execute under 250ms'
37+
);
38+
39+
PREPARE retrieve_count AS
40+
SELECT count(id) AS count
41+
FROM rate_limit.individual_records
42+
WHERE
43+
key = 'test-count'
44+
AND session_id = '00000000-0000-0000-0000-000000000000'::uuid;
45+
46+
SELECT performs_ok(
47+
'retrieve_count',
48+
250,
49+
'retrieving count should execute under 250ms'
50+
);
51+
52+
53+
PREPARE decrement_records as
54+
WITH
55+
rows_to_delete AS (
56+
SELECT id FROM rate_limit.individual_records
57+
WHERE
58+
key = 'test-decrement'
59+
AND session_id = '00000000-0000-0000-0000-000000000000'
60+
ORDER BY event_time
61+
LIMIT 1
62+
)
63+
DELETE FROM rate_limit.individual_records
64+
USING rows_to_delete WHERE individual_records.id = rows_to_delete.id;
65+
66+
SELECT performs_ok(
67+
'decrement_records',
68+
250,
69+
'decrementing query execute under 250ms'
70+
);
71+
72+
PREPARE reset_key as
73+
DELETE FROM rate_limit.individual_records
74+
WHERE
75+
key = 'test-reset-key'
76+
AND session_id = '00000000-0000-0000-0000-000000000000';
77+
78+
79+
SELECT performs_ok(
80+
'reset_key',
81+
250,
82+
'resetting a key should execute under 250ms'
83+
);
84+
85+
PREPARE reset_all as
86+
DELETE FROM rate_limit.individual_records
87+
WHERE session_id = '00000000-0000-0000-0000-000000000000';
88+
89+
90+
SELECT performs_ok(
91+
'reset_all',
92+
250,
93+
'resetting a session should execute under 250ms'
94+
);
95+
96+
DEALLOCATE insert_individual_record;
97+
DEALLOCATE retrieve_count;
98+
DEALLOCATE decrement_records;
99+
DEALLOCATE reset_key;
100+
DEALLOCATE reset_all;
101+
102+
-- Finish the tests and clean up.
103+
SELECT finish FROM finish();
104+
ROLLBACK;

0 commit comments

Comments
 (0)