Skip to content

Commit afbf967

Browse files
committed
Improved tests consistency by removing some duplicating scenarios
1 parent f3703d9 commit afbf967

5 files changed

Lines changed: 152 additions & 67 deletions

File tree

src/packages/dumbo/src/storage/sqlite/core/pool/dualPool.int.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ describe('SQLite Dual Connection Pool', () => {
307307
return error;
308308
} finally {
309309
await pool.close();
310+
cleanupDb(parallelFileName);
310311
}
311312
});
312313

src/packages/pongo/src/core/cache/collectionCache.int.spec.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,43 @@ describe('pongoCollection cache integration', () => {
5656
expect(spies.get).toHaveBeenCalled();
5757
});
5858

59+
it('db-level cache flows to collection', async () => {
60+
const { cache, spies } = spyCache('db');
61+
client = pongoClient({
62+
driver: sqlite3Driver,
63+
connectionString: memoryConnectionString(),
64+
});
65+
await client.connect();
66+
67+
const col = client.db('db', { cache }).collection<User>('users');
68+
const { insertedId } = await col.insertOne({ name: 'Alice' });
69+
await col.findOne({ _id: insertedId! });
70+
71+
expect(spies.set).toHaveBeenCalled();
72+
expect(spies.get).toHaveBeenCalled();
73+
});
74+
75+
it('db-level cache overrides client-level', async () => {
76+
const { cache: clientCache, spies: clientSpies } = spyCache('client');
77+
const { cache: dbCache, spies: dbSpies } = spyCache('db');
78+
79+
client = pongoClient({
80+
driver: sqlite3Driver,
81+
connectionString: memoryConnectionString(),
82+
cache: clientCache,
83+
});
84+
await client.connect();
85+
86+
const col = client.db('db', { cache: dbCache }).collection<User>('users');
87+
const { insertedId } = await col.insertOne({ name: 'Carol' });
88+
await col.findOne({ _id: insertedId! });
89+
90+
expect(dbSpies.set).toHaveBeenCalled();
91+
expect(dbSpies.get).toHaveBeenCalled();
92+
expect(clientSpies.set).not.toHaveBeenCalled();
93+
expect(clientSpies.get).not.toHaveBeenCalled();
94+
});
95+
5996
it('collection-level cache overrides client-level', async () => {
6097
const { cache: clientCache, spies: clientSpies } = spyCache('client');
6198
const { cache: colCache, spies: colSpies } = spyCache('collection');
@@ -79,6 +116,28 @@ describe('pongoCollection cache integration', () => {
79116
expect(clientSpies.get).not.toHaveBeenCalled();
80117
});
81118

119+
it('collection-level cache overrides db-level', async () => {
120+
const { cache: dbCache, spies: dbSpies } = spyCache('db');
121+
const { cache: colCache, spies: colSpies } = spyCache('collection');
122+
123+
client = pongoClient({
124+
driver: sqlite3Driver,
125+
connectionString: memoryConnectionString(),
126+
});
127+
await client.connect();
128+
129+
const col = client
130+
.db('db', { cache: dbCache })
131+
.collection<User>('users', { cache: colCache });
132+
const { insertedId } = await col.insertOne({ name: 'Bob' });
133+
await col.findOne({ _id: insertedId! });
134+
135+
expect(colSpies.set).toHaveBeenCalled();
136+
expect(colSpies.get).toHaveBeenCalled();
137+
expect(dbSpies.set).not.toHaveBeenCalled();
138+
expect(dbSpies.get).not.toHaveBeenCalled();
139+
});
140+
82141
it("'disabled' at client, enabled at collection", async () => {
83142
const { cache: colCache, spies: colSpies } = spyCache('collection');
84143

@@ -494,5 +553,74 @@ describe('pongoCollection cache integration', () => {
494553

495554
expect(spies.set).toHaveBeenCalled();
496555
});
556+
557+
it('commit flushes to inherited client-level cache', async () => {
558+
const { cache, spies } = spyCache('client');
559+
await client.close();
560+
client = pongoClient({
561+
driver: sqlite3Driver,
562+
connectionString: memoryConnectionString(),
563+
cache,
564+
});
565+
await client.connect();
566+
567+
const col = client.db('db').collection<User>('users');
568+
569+
const session = client.startSession();
570+
session.startTransaction();
571+
await col.insertOne({ name: 'Ivy' }, { session });
572+
573+
expect(spies.set).not.toHaveBeenCalled();
574+
575+
await session.commitTransaction();
576+
await session.endSession();
577+
578+
expect(spies.set).toHaveBeenCalled();
579+
});
580+
581+
it('rollback does not populate inherited client-level cache', async () => {
582+
const { cache, spies } = spyCache('client');
583+
await client.close();
584+
client = pongoClient({
585+
driver: sqlite3Driver,
586+
connectionString: memoryConnectionString(),
587+
cache,
588+
});
589+
await client.connect();
590+
591+
const col = client.db('db').collection<User>('users');
592+
593+
const session = client.startSession();
594+
session.startTransaction();
595+
await col.insertOne({ name: 'Jack' }, { session });
596+
await session.abortTransaction();
597+
await session.endSession();
598+
599+
expect(spies.set).not.toHaveBeenCalled();
600+
});
601+
602+
it('commit flushes to db-level cache, not client-level', async () => {
603+
const { cache: clientCache, spies: clientSpies } = spyCache('client');
604+
const { cache: dbCache, spies: dbSpies } = spyCache('db');
605+
606+
await client.close();
607+
client = pongoClient({
608+
driver: sqlite3Driver,
609+
connectionString: memoryConnectionString(),
610+
cache: clientCache,
611+
});
612+
await client.connect();
613+
614+
const col = client.db('db', { cache: dbCache }).collection<User>('users');
615+
616+
const session = client.startSession();
617+
session.startTransaction();
618+
await col.insertOne({ name: 'Kim' }, { session });
619+
await session.commitTransaction();
620+
await session.endSession();
621+
622+
expect(dbSpies.set).toHaveBeenCalled();
623+
expect(clientSpies.set).not.toHaveBeenCalled();
624+
});
497625
});
498626
});

src/packages/pongo/src/core/cache/pongoCache.unit.spec.ts

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,29 @@ import { pongoCache } from './pongoCache';
33
import { identityMapCache } from './providers';
44

55
describe('pongoCache factory', () => {
6-
describe("'identity-map' type", () => {
7-
it('creates an identity-map cache when type is identity-map', async () => {
8-
const cache = pongoCache({ type: 'identity-map' });
9-
10-
expect(cache.cacheType).toBe('pongo:cache:identity-map');
11-
12-
await cache.set('db:col:1', { _id: '1', name: 'Alice' });
13-
const result = await cache.get('db:col:1');
14-
expect(result).toEqual({ _id: '1', name: 'Alice' });
15-
});
16-
17-
it('identity-map cache has no max size eviction', async () => {
18-
const cache = pongoCache({ type: 'identity-map' });
19-
const count = 2000;
20-
21-
for (let i = 0; i < count; i++) {
22-
await cache.set(`db:col:${i}`, { _id: String(i), n: i });
23-
}
24-
25-
for (let i = 0; i < count; i++) {
26-
const doc = await cache.get(`db:col:${i}`);
27-
expect(doc).not.toBeNull();
28-
}
29-
});
6+
it("creates an identity-map cache when type is 'identity-map'", () => {
7+
const cache = pongoCache({ type: 'identity-map' });
8+
expect(cache.cacheType).toBe('pongo:cache:identity-map');
309
});
3110

32-
describe("'in-memory' type", () => {
33-
it('creates an LRU cache when type is in-memory', () => {
34-
const cache = pongoCache({ type: 'in-memory', max: 100 });
35-
expect(cache.cacheType).toBe('pongo:cache:lru');
36-
});
37-
38-
it('defaults to disabled when no config provided', async () => {
39-
const cache = pongoCache();
40-
41-
await cache.set('db:col:1', { _id: '1', name: 'Bob' });
42-
const result = await cache.get('db:col:1');
43-
expect(result).toBeUndefined();
44-
});
45-
46-
it('in-memory LRU evicts when over max', async () => {
47-
const cache = pongoCache({ type: 'in-memory', max: 3 });
48-
49-
for (let i = 0; i < 4; i++) {
50-
await cache.set(`db:col:${i}`, { _id: String(i) });
51-
}
52-
53-
// Oldest entry should be evicted
54-
const first = await cache.get('db:col:0');
55-
expect(first).toBeUndefined();
56-
});
11+
it("creates an LRU cache when type is 'in-memory'", () => {
12+
const cache = pongoCache({ type: 'in-memory', max: 100 });
13+
expect(cache.cacheType).toBe('pongo:cache:lru');
5714
});
5815

59-
describe("'disabled'", () => {
60-
it('returns noop cache when disabled', async () => {
61-
const cache = pongoCache('disabled');
16+
it('returns noop cache when no config provided', () => {
17+
const cache = pongoCache();
18+
expect(cache.cacheType).toBe('pongo:cache:no-op');
19+
});
6220

63-
await cache.set('db:col:1', { _id: '1', name: 'Carol' });
64-
const result = await cache.get('db:col:1');
65-
expect(result).toBeUndefined();
66-
});
21+
it("returns noop cache when 'disabled'", () => {
22+
const cache = pongoCache('disabled');
23+
expect(cache.cacheType).toBe('pongo:cache:no-op');
6724
});
6825

69-
describe('pre-built PongoCache passthrough', () => {
70-
it('passes through a pre-built PongoCache instance', () => {
71-
const raw = identityMapCache();
72-
const cache = pongoCache(raw);
73-
expect(cache).toBe(raw);
74-
});
26+
it('passes through a pre-built PongoCache instance', () => {
27+
const raw = identityMapCache();
28+
const cache = pongoCache(raw);
29+
expect(cache).toBe(raw);
7530
});
7631
});

src/packages/pongo/src/core/collection/pongoCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export const pongoCollection = <
189189
const resolveFromCache = async (
190190
key: PongoDocumentCacheKey,
191191
options: CollectionOperationOptions | undefined,
192-
): Promise<PongoDocument | undefined> => {
192+
): Promise<T | undefined> => {
193193
const txCache = txCacheFor(options);
194194
if (txCache) {
195195
const cached = await txCache.get<T>(key);
@@ -201,7 +201,7 @@ export const pongoCollection = <
201201
const findManyFromCache = async (
202202
keys: PongoDocumentCacheKey[],
203203
options: CollectionOperationOptions | undefined,
204-
): Promise<PongoDocument[]> => {
204+
): Promise<T[]> => {
205205
const txCache = txCacheFor(options);
206206

207207
if (!txCache) {

src/packages/pongo/src/core/database/pongoDb.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ export const PongoDatabase = <
127127
schema: { ...options.schema, ...collectionOptions?.schema },
128128
serializer,
129129
errors: { ...options.errors, ...collectionOptions?.errors },
130-
...(collectionOptions?.cache !== undefined
131-
? { cache: collectionOptions?.cache ?? cache }
132-
: {}),
130+
cache:
131+
collectionOptions?.cache !== undefined
132+
? collectionOptions.cache
133+
: cache,
133134
}),
134135
transaction: () => pool.transaction(),
135136
withTransaction: (handle) => pool.withTransaction(handle),

0 commit comments

Comments
 (0)