Skip to content

Commit b9c5253

Browse files
test: added database unit tests for session handling
1 parent e42a2e4 commit b9c5253

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

db/tests/unit/session_reset.test.sql

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(3);
4+
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
6+
SELECT
7+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
8+
'dedicated-test-1'::text AS name_,
9+
'aggregated'::text AS type_,
10+
'2023-09-28 06:00:00+0000'::timestamptz AS expires_at
11+
UNION
12+
SELECT
13+
'00000000-1111-1111-1111-000000000000'::uuid AS id,
14+
'dedicated-test-2'::text AS name_,
15+
'aggregated'::text AS type_,
16+
'2023-09-28 08:00:00+0000'::timestamptz AS expires_at;
17+
18+
19+
SELECT results_eq(
20+
$have$
21+
SELECT name_, type_
22+
FROM rate_limit.session_reset(
23+
'dedicated-test-1'::text,
24+
'aggregated'::text,
25+
'2023-09-29 06:00:00+0000'::timestamptz)
26+
$have$,
27+
$want$
28+
SELECT
29+
'dedicated-test-1'::text AS name_,
30+
'aggregated'::text AS type_
31+
$want$,
32+
'rate_limit.session_reset returns correct values'
33+
);
34+
35+
SELECT results_eq(
36+
$have$
37+
SELECT name_, type_, expires_at
38+
FROM rate_limit.sessions
39+
WHERE name_ = 'dedicated-test-1'
40+
$have$,
41+
$want$
42+
SELECT
43+
'dedicated-test-1'::text AS name_,
44+
'aggregated'::text AS type_,
45+
'2023-09-29 06:00:00+0000'::timestamptz AS expires_at
46+
$want$,
47+
'rate_limit.session_reset persists correct entries to table'
48+
);
49+
50+
SELECT results_ne(
51+
$have$
52+
SELECT id
53+
FROM rate_limit.sessions
54+
WHERE name_ = 'dedicated-test-1'
55+
$have$,
56+
$want$
57+
SELECT '00000000-0000-0000-0000-000000000000'::uuid AS id;
58+
$want$,
59+
'rate_limit.session_reset forces change of id while keeping name'
60+
);
61+
-- Finish the tests and clean up.
62+
SELECT finish FROM finish();
63+
ROLLBACK;

db/tests/unit/session_select.test.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(1);
4+
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
6+
SELECT
7+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
8+
'dedicated-test-1'::text AS name_,
9+
'aggregated'::text AS type_,
10+
'2023-09-28 06:00:00+0000'::timestamptz AS expires_at
11+
UNION
12+
SELECT
13+
'00000000-1111-1111-1111-000000000000'::uuid AS id,
14+
'dedicated-test-2'::text AS name_,
15+
'aggregated'::text AS type_,
16+
'2023-09-28 08:00:00+0000'::timestamptz AS expires_at;
17+
18+
19+
SELECT results_eq(
20+
$have$
21+
SELECT id, name_, type_, expires_at
22+
FROM rate_limit.session_select(
23+
'dedicated-test-1'::text,
24+
'aggregated'::text)
25+
$have$,
26+
$want$
27+
SELECT
28+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
29+
'dedicated-test-1'::text AS name_,
30+
'aggregated'::text AS type_,
31+
'2023-09-28 06:00:00+0000'::timestamptz AS expires_at
32+
$want$,
33+
'rate_limit.session_select returns correct values'
34+
);
35+
36+
-- Finish the tests and clean up.
37+
SELECT finish FROM finish();
38+
ROLLBACK;

0 commit comments

Comments
 (0)