Skip to content

Commit 723dc32

Browse files
test: added tests for aggregated store new implementation
1 parent 492836e commit 723dc32

File tree

6 files changed

+68
-65
lines changed

6 files changed

+68
-65
lines changed

db/tests/performance/aggregated_ip.test.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ VALUES ('test-update', '00000000-0000-0000-0000-000000000000'::uuid, 20),
2828

2929
SELECT performs_ok(
3030
$bd$
31-
SELECT agg_increment as count FROM rate_limit.agg_increment('test-update', '00000000-0000-0000-0000-000000000000');
31+
SELECT * from rate_limit.agg_increment('test-update', 'dedicated-test', 1000) AS (count int, expires_at timestamptz);
3232
$bd$,
3333
250,
3434
'inserting record should execute under 250ms'
3535
);
3636

3737
SELECT performs_ok(
3838
$bd$
39-
SELECT * FROM rate_limit.agg_decrement('test-decrement', '00000000-0000-0000-0000-000000000000');
39+
SELECT * FROM rate_limit.agg_decrement('test-decrement', 'dedicated-test');
4040
$bd$,
4141
250,
4242
'decrementing query execute under 250ms'
4343
);
4444

4545
SELECT performs_ok(
4646
$bd$
47-
SELECT * FROM rate_limit.agg_reset_key('test-reset-key', '00000000-0000-0000-0000-000000000000')
47+
SELECT * FROM rate_limit.agg_reset_key('test-reset-key', 'dedicated-test')
4848
$bd$,
4949
250,
5050
'resetting a key should execute under 250ms'
5151
);
5252

5353
SELECT performs_ok(
5454
$bd$
55-
SELECT * FROM rate_limit.agg_reset_session('00000000-0000-0000-0000-000000000000');
55+
SELECT * FROM rate_limit.agg_reset_session('dedicated-test');
5656
$bd$,
5757
250,
5858
'resetting a session should execute under 250ms'

db/tests/unit/agg_decrement.test.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ BEGIN;
22
-- Plan the tests.
33
SELECT plan(2);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'aggregated'::text AS type_;
9+
'aggregated'::text AS type_,
10+
'2023-01-01 10:00:00+0' AS expires_at;
1011

1112
INSERT INTO rate_limit.records_aggregated (key, session_id)
1213
SELECT
@@ -15,7 +16,7 @@ SELECT
1516

1617
SELECT lives_ok(
1718
$have$
18-
SELECT * FROM rate_limit.agg_decrement('existing-key', '00000000-0000-0000-0000-000000000000')
19+
SELECT * FROM rate_limit.agg_decrement('existing-key', 'dedicated-test', '2023-01-01 09:00:00+0')
1920
$have$,
2021
'rate_limit.agg_decrement does not throw an error'
2122
);

db/tests/unit/agg_increment.test.sql

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
BEGIN;
22
-- Plan the tests.
3-
SELECT plan(3);
3+
SELECT plan(5);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'aggregated'::text AS type_
9+
'aggregated'::text AS type_,
10+
'2023-01-01 10:00:00+0'::timestamptz AS expires_at
1011
UNION
1112
SELECT
1213
'00000000-1111-1111-1111-000000000000'::uuid AS id,
1314
'dedicated-test-2'::text AS name_,
14-
'aggregated'::text AS type_;;
15+
'aggregated'::text AS type_,
16+
'2023-01-01 10:00:00+0' AS expires_at
17+
UNION
18+
SELECT
19+
'00000000-2222-2222-2222-000000000000'::uuid AS id,
20+
'dedicated-test-expired'::text AS name_,
21+
'aggregated'::text AS type_,
22+
'2023-01-01 08:00:00+0' AS expires_at;
1523

1624
INSERT INTO rate_limit.records_aggregated (key, session_id)
1725
SELECT
@@ -20,34 +28,59 @@ SELECT
2028

2129
SELECT results_eq(
2230
$have$
23-
SELECT agg_increment AS count FROM rate_limit.agg_increment('new-key', '00000000-0000-0000-0000-000000000000')
31+
SELECT * FROM rate_limit.agg_increment('new-key', 'dedicated-test', 1000, '2023-01-01 09:00:00+0') AS (count int, expires_at timestamptz);
2432
$have$,
2533
$want$
26-
SELECT 1::int AS count;
34+
SELECT 1::int AS count, '2023-01-01 10:00:00+0'::timestamptz as expires_at;
2735
$want$,
2836
'rate_limit.agg_increment returns correct count for new key'
2937
);
3038

3139
SELECT results_eq(
3240
$have$
33-
SELECT agg_increment AS count FROM rate_limit.agg_increment('new-key', '00000000-1111-1111-1111-000000000000')
41+
SELECT * FROM rate_limit.agg_increment('new-key', 'dedicated-test-2', 1000, '2023-01-01 09:00:00+0') AS (count int, expires_at timestamptz);
3442
$have$,
3543
$want$
36-
SELECT 1::int AS count;
44+
SELECT 1::int AS count, '2023-01-01 10:00:00+0'::timestamptz as expires_at;
3745
$want$,
3846
'rate_limit.agg_increment returns correct count for new key on different session'
3947
);
4048

4149
SELECT results_eq(
4250
$have$
43-
SELECT agg_increment AS count FROM rate_limit.agg_increment('existing-key', '00000000-0000-0000-0000-000000000000')
51+
SELECT * FROM rate_limit.agg_increment('existing-key', 'dedicated-test', 1000, '2023-01-01 09:00:00+0') AS (count int, expires_at timestamptz);
4452
$have$,
4553
$want$
46-
SELECT 2::int AS count;
54+
SELECT 2::int AS count, '2023-01-01 10:00:00+0'::timestamptz as expires_at;
4755
$want$,
4856
'rate_limit.agg_increment returns correct count for existing key'
4957
);
5058

59+
SELECT results_eq(
60+
$have$
61+
SELECT * FROM rate_limit.agg_increment('existing-key', 'dedicated-test-expired', 1000, '2023-01-01 09:00:00+0') AS (count int, expires_at timestamptz);
62+
$have$,
63+
$want$
64+
SELECT 1::int AS count, '2023-01-01 09:00:01+0'::timestamptz as expires_at;
65+
$want$,
66+
'rate_limit.agg_increment returns correct count for existing key for expired session'
67+
);
68+
69+
SELECT bag_hasnt(
70+
$have$
71+
SELECT id, name_, type_, expires_at FROM rate_limit.sessions
72+
$have$,
73+
$miss$
74+
SELECT
75+
'00000000-2222-2222-2222-000000000000'::uuid AS id,
76+
'dedicated-test-expired'::text AS name_,
77+
'aggregated'::text AS type_,
78+
'2023-01-01 08:00:00+0'::timestamptz as expires_at;
79+
$miss$,
80+
'expired session should not be reset after increment invoke'
81+
);
82+
83+
5184
-- Finish the tests and clean up.
5285
SELECT finish FROM finish();
5386
ROLLBACK;

db/tests/unit/agg_reset_key.test.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ BEGIN;
22
-- Plan the tests.
33
SELECT plan(2);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'aggregated'::text AS type_;
9+
'aggregated'::text AS type_,
10+
'2023-01-01 10:00+0'::timestamptz AS expires_at;
1011

1112
INSERT INTO rate_limit.records_aggregated (key, session_id)
1213
SELECT
@@ -15,7 +16,7 @@ SELECT
1516

1617
SELECT lives_ok(
1718
$have$
18-
SELECT * FROM rate_limit.agg_reset_key('existing-key', '00000000-0000-0000-0000-000000000000')
19+
SELECT * FROM rate_limit.agg_reset_key('existing-key', 'dedicated-test', '2023-01-01 09:00+0'::timestamptz )
1920
$have$,
2021
'rate_limit.agg_reset_key does not throw an error'
2122
);

db/tests/unit/agg_reset_session.test.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ BEGIN;
22
-- Plan the tests.
33
SELECT plan(2);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'aggregated'::text AS type_;
9+
'aggregated'::text AS type_,
10+
'2023-01-01 10:00+0'::timestamptz AS expires_at;
1011

1112
INSERT INTO rate_limit.records_aggregated (key, session_id)
1213
SELECT
@@ -19,7 +20,7 @@ SELECT
1920

2021
SELECT lives_ok(
2122
$have$
22-
SELECT * FROM rate_limit.agg_reset_session('00000000-0000-0000-0000-000000000000')
23+
SELECT * FROM rate_limit.agg_reset_session('dedicated-test', '2023-01-01 09:00+0')
2324
$have$,
2425
'rate_limit.agg_reset_session does not throw an error'
2526
);

test/stores/store_aggregated_ip.spec.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { assert } from 'chai'
44
import { Pool } from 'pg'
55
import sinon, { SinonMock, SinonStub } from 'sinon'
66
import { PostgresStore } from '../../source'
7-
import { Session } from '../../source/models/session'
8-
const session_handler = require('../../source/util/session_handler')
97
const migration_handler = require('../../source/util/migration_handler')
108

119
class ClientMock {
@@ -17,37 +15,21 @@ describe('Postgres Store Aggregated IP', () => {
1715
let query: SinonStub
1816
let connect: SinonStub
1917
let client: SinonMock
20-
let getSessionStub: SinonStub
2118
let applyMigrationsStub: SinonStub
22-
let isSessionValidSpy: SinonStub
23-
let newCreatedSession: Session
19+
let expirationDate = new Date()
2420

2521
beforeEach(() => {
2622
query = sinon.stub(Pool.prototype, 'query')
2723
connect = sinon.stub(Pool.prototype, 'connect')
2824
client = sinon.mock(ClientMock.prototype)
29-
getSessionStub = sinon.stub(session_handler, 'getSession')
30-
isSessionValidSpy = sinon.stub(session_handler, 'isSessionValid')
3125
applyMigrationsStub = sinon.stub(migration_handler, 'applyMigrations')
32-
const futureTime = new Date()
33-
const timeOffset = 5000
34-
futureTime.setMilliseconds(futureTime.getMilliseconds() + timeOffset)
35-
36-
newCreatedSession = {
37-
id: '1',
38-
name: 'test-name',
39-
type: 'test-type',
40-
expires_at: futureTime,
41-
}
4226
})
4327

4428
afterEach(() => {
4529
query.restore() // reset stub/mock
4630
connect.restore()
4731
client.restore()
48-
getSessionStub.restore()
4932
applyMigrationsStub.restore()
50-
isSessionValidSpy.restore()
5133
})
5234

5335
it('constructor should call correct functions and populate correct fields', async () => {
@@ -60,34 +42,25 @@ describe('Postgres Store Aggregated IP', () => {
6042
let pool = new Pool()
6143
let dbCount = 1
6244

63-
isSessionValidSpy.returns(true)
6445
query.onFirstCall().returns({
6546
rows: [
6647
{
6748
count: dbCount,
49+
expires_at: expirationDate,
6850
},
6951
],
7052
})
7153
let testStore = new PostgresStore({}, 'test')
7254
testStore.pool = pool
73-
testStore.session = newCreatedSession
74-
let recordInsertGetRecordsQuery = `
75-
SELECT agg_increment as count FROM rate_limit.agg_increment($1, $2);
76-
`
55+
let recordInsertGetRecordsQuery = `SELECT * FROM rate_limit.agg_increment($1, $2, $3) AS (count int, expires_at timestamptz);`
7756
let incrementCount = await testStore.increment('key')
78-
sinon.assert.callCount(isSessionValidSpy, 1)
7957
sinon.assert.calledWith(query, recordInsertGetRecordsQuery, [
8058
'key',
81-
newCreatedSession.id,
59+
testStore.prefix,
60+
undefined,
8261
])
8362
assert.equal(incrementCount.totalHits, dbCount)
84-
assert.isTrue(
85-
(incrementCount.resetTime?.getMilliseconds() ||
86-
new Date().getMilliseconds()) -
87-
(newCreatedSession.expires_at?.getMilliseconds() ||
88-
new Date().getMilliseconds()) <
89-
10,
90-
)
63+
assert.equal(incrementCount.resetTime, expirationDate)
9164
})
9265

9366
it('decrement function should follow expected business logic', async () => {
@@ -98,16 +71,12 @@ describe('Postgres Store Aggregated IP', () => {
9871

9972
let testStore = new PostgresStore({}, 'test')
10073
testStore.pool = pool
101-
testStore.session = newCreatedSession
10274

10375
await testStore.decrement('key')
10476
let decrementQuery = `
10577
SELECT * FROM rate_limit.agg_decrement($1, $2);
10678
`
107-
sinon.assert.calledWith(query, decrementQuery, [
108-
'key',
109-
newCreatedSession.id,
110-
])
79+
sinon.assert.calledWith(query, decrementQuery, ['key', testStore.prefix])
11180
})
11281

11382
it('resetKey function should follow expected business logic', async () => {
@@ -118,13 +87,12 @@ describe('Postgres Store Aggregated IP', () => {
11887

11988
let testStore = new PostgresStore({}, 'test')
12089
testStore.pool = pool
121-
testStore.session = newCreatedSession
12290

12391
await testStore.resetKey('key')
12492
let resetQuery = `
12593
SELECT * FROM rate_limit.agg_reset_key($1, $2)
12694
`
127-
sinon.assert.calledWith(query, resetQuery, ['key', newCreatedSession.id])
95+
sinon.assert.calledWith(query, resetQuery, ['key', testStore.prefix])
12896
})
12997

13098
it('resetAll function should follow expected business logic', async () => {
@@ -135,12 +103,11 @@ describe('Postgres Store Aggregated IP', () => {
135103

136104
let testStore = new PostgresStore({}, 'test')
137105
testStore.pool = pool
138-
testStore.session = newCreatedSession
139106

140107
await testStore.resetAll()
141108
let resetAllQuery = `
142109
SELECT * FROM rate_limit.agg_reset_session($1);
143110
`
144-
sinon.assert.calledWith(query, resetAllQuery, [newCreatedSession.id])
111+
sinon.assert.calledWith(query, resetAllQuery, [testStore.prefix])
145112
})
146113
})

0 commit comments

Comments
 (0)