Skip to content

Commit 38223d4

Browse files
test: added database unit tests for aggregated store
1 parent b9e235f commit 38223d4

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

db/cli/run_tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ set -e
33
psql --quiet -U $USER -h $HOST -p $PORT -d $DATABASE -c 'CREATE EXTENSION IF NOT EXISTS pgtap;'
44

55
pg_prove -U $USER -h $HOST -p $PORT -d $DATABASE db/tests/performance/*.test.sql
6+
7+
pg_prove -U $USER -h $HOST -p $PORT -d $DATABASE db/tests/unit/*.test.sql

db/linting/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ echo 'Linting migrations folder'
22
sqlfluff lint source/migrations/ --dialect postgres --ignore parsing --config db/linting/.sqlfluff
33

44
echo 'Linting tests folder'
5-
sqlfluff lint db/tests/performance/ --dialect postgres --ignore parsing --config db/linting/.sqlfluff
5+
sqlfluff lint db/tests/**/ --dialect postgres --ignore parsing --config db/linting/.sqlfluff

db/tests/unit/agg_decrement.test.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
'aggregated'::text AS type_;
10+
11+
INSERT INTO rate_limit.records_aggregated (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
15+
16+
SELECT lives_ok(
17+
$have$
18+
SELECT * FROM rate_limit.agg_decrement('existing-key', '00000000-0000-0000-0000-000000000000')
19+
$have$,
20+
'rate_limit.agg_decrement does not throw an error'
21+
);
22+
23+
SELECT results_eq(
24+
$have$
25+
SELECT count FROM rate_limit.records_aggregated
26+
WHERE key = 'existing-key'
27+
$have$,
28+
$want$
29+
SELECT 0::int AS count;
30+
$want$,
31+
'rate_limit.agg_decrement decrements count for key'
32+
);
33+
34+
-- Finish the tests and clean up.
35+
SELECT finish FROM finish();
36+
ROLLBACK;

db/tests/unit/agg_increment.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(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+
'aggregated'::text AS type_;
10+
11+
INSERT INTO rate_limit.records_aggregated (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 agg_increment AS count FROM rate_limit.agg_increment('new-key', '00000000-0000-0000-0000-000000000000')
19+
$have$,
20+
$want$
21+
SELECT 1::int AS count;
22+
$want$,
23+
'rate_limit.agg_increment returns correct count for new key'
24+
);
25+
26+
SELECT results_eq(
27+
$have$
28+
SELECT agg_increment AS count FROM rate_limit.agg_increment('existing-key', '00000000-0000-0000-0000-000000000000')
29+
$have$,
30+
$want$
31+
SELECT 2::int AS count;
32+
$want$,
33+
'rate_limit.agg_increment returns correct count for existing key'
34+
);
35+
36+
-- Finish the tests and clean up.
37+
SELECT finish FROM finish();
38+
ROLLBACK;

db/tests/unit/agg_reset_key.test.sql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
'aggregated'::text AS type_;
10+
11+
INSERT INTO rate_limit.records_aggregated (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
15+
16+
SELECT lives_ok(
17+
$have$
18+
SELECT * FROM rate_limit.agg_reset_key('existing-key', '00000000-0000-0000-0000-000000000000')
19+
$have$,
20+
'rate_limit.agg_reset_key does not throw an error'
21+
);
22+
23+
SELECT is_empty(
24+
$have$
25+
SELECT * FROM rate_limit.records_aggregated
26+
WHERE key = 'existing-key'
27+
$have$,
28+
'rate_limit.agg_reset_key decrements count for key'
29+
);
30+
31+
-- Finish the tests and clean up.
32+
SELECT finish FROM finish();
33+
ROLLBACK;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
'aggregated'::text AS type_;
10+
11+
INSERT INTO rate_limit.records_aggregated (key, session_id)
12+
SELECT
13+
'existing-key' AS key_,
14+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id
15+
UNION
16+
SELECT
17+
'another-existing-key' AS key_,
18+
'00000000-0000-0000-0000-000000000000'::uuid AS session_id;
19+
20+
SELECT lives_ok(
21+
$have$
22+
SELECT * FROM rate_limit.agg_reset_session('00000000-0000-0000-0000-000000000000')
23+
$have$,
24+
'rate_limit.agg_reset_session does not throw an error'
25+
);
26+
27+
SELECT is_empty(
28+
$have$
29+
SELECT * FROM rate_limit.records_aggregated
30+
WHERE session_id = '00000000-0000-0000-0000-000000000000'
31+
$have$,
32+
'rate_limit.agg_reset_session removes all keys of session'
33+
);
34+
35+
-- Finish the tests and clean up.
36+
SELECT finish FROM finish();
37+
ROLLBACK;

0 commit comments

Comments
 (0)