Skip to content

Commit e42a2e4

Browse files
test: added database unit tests for individual store
1 parent 38223d4 commit e42a2e4

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

db/tests/unit/ind_decrement.test.sql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(2);
4+
5+
INSERT INTO rate_limit.sessions (id, name_, type_)
6+
SELECT
7+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
8+
'dedicated-test'::text AS name_,
9+
'individual'::text AS type_;
10+
11+
INSERT INTO rate_limit.individual_records (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
15+
UNION ALL
16+
SELECT
17+
'existing-key' AS key_,
18+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
19+
UNION ALL
20+
SELECT
21+
'existing-key' AS key_,
22+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
23+
24+
SELECT lives_ok(
25+
$have$
26+
SELECT * FROM rate_limit.ind_decrement('existing-key', '00000000-0000-0000-0000-000000000000')
27+
$have$,
28+
'rate_limit.ind_decrement does not throw an error'
29+
);
30+
31+
SELECT bag_eq(
32+
$have$
33+
SELECT key FROM rate_limit.individual_records
34+
WHERE session_id = '00000000-0000-0000-0000-000000000000'
35+
$have$,
36+
$want$
37+
SELECT 'existing-key'::text as key
38+
UNION ALL
39+
SELECT 'existing-key'::text as key;
40+
$want$,
41+
'rate_limit.ind_decrement applies correct logic on table rows'
42+
);
43+
44+
-- Finish the tests and clean up.
45+
SELECT finish FROM finish();
46+
ROLLBACK;

db/tests/unit/ind_increment.test.sql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(3);
4+
5+
INSERT INTO rate_limit.sessions (id, name_, type_)
6+
SELECT
7+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
8+
'dedicated-test'::text AS name_,
9+
'individual'::text AS type_;
10+
11+
INSERT INTO rate_limit.individual_records (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
15+
16+
SELECT results_eq(
17+
$have$
18+
SELECT ind_increment AS count FROM rate_limit.ind_increment('new-key', '00000000-0000-0000-0000-000000000000')
19+
$have$,
20+
$want$
21+
SELECT 1::int AS count;
22+
$want$,
23+
'rate_limit.ind_increment returns correct count for new key'
24+
);
25+
26+
SELECT results_eq(
27+
$have$
28+
SELECT ind_increment AS count FROM rate_limit.ind_increment('existing-key', '00000000-0000-0000-0000-000000000000')
29+
$have$,
30+
$want$
31+
SELECT 2::int AS count;
32+
$want$,
33+
'rate_limit.ind_increment returns correct count for existing key'
34+
);
35+
36+
SELECT bag_eq(
37+
$have$
38+
SELECT key FROM rate_limit.individual_records
39+
WHERE session_id = '00000000-0000-0000-0000-000000000000'
40+
$have$,
41+
$want$
42+
SELECT 'existing-key'::text as key
43+
UNION ALL
44+
SELECT 'existing-key'::text as key
45+
UNION ALL
46+
SELECT 'new-key'::text as key;
47+
$want$,
48+
'rate_limit.ind_increment applies correct logic on table rows'
49+
);
50+
51+
-- Finish the tests and clean up.
52+
SELECT finish FROM finish();
53+
ROLLBACK;

db/tests/unit/ind_reset_key.test.sql

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(2);
4+
5+
INSERT INTO rate_limit.sessions (id, name_, type_)
6+
SELECT
7+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
8+
'dedicated-test'::text AS name_,
9+
'individual'::text AS type_;
10+
11+
INSERT INTO rate_limit.individual_records (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
15+
UNION ALL
16+
SELECT
17+
'existing-key' AS key_,
18+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
19+
UNION ALL
20+
SELECT
21+
'existing-key' AS key_,
22+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
23+
24+
SELECT lives_ok(
25+
$have$
26+
SELECT * FROM rate_limit.ind_reset_key('existing-key', '00000000-0000-0000-0000-000000000000')
27+
$have$,
28+
'rate_limit.ind_reset_key does not throw an error'
29+
);
30+
31+
SELECT is_empty(
32+
$have$
33+
SELECT * FROM rate_limit.individual_records
34+
WHERE key = 'existing-key'
35+
$have$,
36+
'rate_limit.ind_reset_key applies correct logic on table rows'
37+
);
38+
39+
-- Finish the tests and clean up.
40+
SELECT finish FROM finish();
41+
ROLLBACK;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
BEGIN;
2+
-- Plan the tests.
3+
SELECT plan(2);
4+
5+
INSERT INTO rate_limit.sessions (id, name_, type_)
6+
SELECT
7+
'00000000-0000-0000-0000-000000000000'::uuid AS id,
8+
'dedicated-test'::text AS name_,
9+
'individual'::text AS type_;
10+
11+
INSERT INTO rate_limit.individual_records (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
15+
UNION ALL
16+
SELECT
17+
'existing-key' AS key_,
18+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
19+
UNION ALL
20+
SELECT
21+
'existing-key' AS key_,
22+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
23+
UNION ALL
24+
SELECT
25+
'other-existing-key' AS key_,
26+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
27+
28+
SELECT lives_ok(
29+
$have$
30+
SELECT * FROM rate_limit.ind_reset_session('00000000-0000-0000-0000-000000000000')
31+
$have$,
32+
'rate_limit.ind_reset_session does not throw an error'
33+
);
34+
35+
SELECT is_empty(
36+
$have$
37+
SELECT * FROM rate_limit.individual_records
38+
WHERE session_id = '00000000-0000-0000-0000-000000000000'
39+
$have$,
40+
'rate_limit.ind_reset_session applies correct logic on table rows'
41+
);
42+
43+
-- Finish the tests and clean up.
44+
SELECT finish FROM finish();
45+
ROLLBACK;

0 commit comments

Comments
 (0)