-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcache.test.ts
More file actions
114 lines (78 loc) · 3.53 KB
/
cache.test.ts
File metadata and controls
114 lines (78 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { afterAll, afterEach, beforeAll, beforeEach, expect, describe } from 'vitest';
import { BaseClientOptions, SimpleCache } from '../../packages/client/src';
import { XataClient } from '../../packages/codegen/example/xata';
import { setUpTestEnvironment, TestEnvironmentResult } from '../utils/setup';
import { test } from '../utils/tracing';
const cache = new SimpleCache();
let xata: XataClient;
let clientOptions: BaseClientOptions;
let hooks: TestEnvironmentResult['hooks'];
beforeAll(async (ctx) => {
const result = await setUpTestEnvironment('cache', { cache });
xata = result.client;
clientOptions = result.clientOptions;
hooks = result.hooks;
await hooks.beforeAll(ctx);
});
afterAll(async (ctx) => {
await hooks.afterAll(ctx);
});
beforeEach(async (ctx) => {
await hooks.beforeEach(ctx);
});
afterEach(async (ctx) => {
await cache.clear();
await hooks.afterEach(ctx);
});
describe('cache', () => {
test('query with ttl', async () => {
const user = await xata.db.users.create({ full_name: 'John Doe' });
await cache.clear();
await xata.db.users.filter({ id: user.id }).getFirst();
const cacheItems = Object.entries(await cache.getAll());
expect(Object.keys(cacheItems)).toHaveLength(1);
const [cacheKey, value] = cacheItems[0] as any;
const cacheItem = await cache.get<any>(cacheKey);
expect(cacheItem).not.toBeNull();
expect(cacheItem?.records[0]?.full_name).toBe('John Doe');
await cache.set(cacheKey, { ...value, records: [{ ...user, full_name: 'Jane Doe' }] });
const query = await xata.db.users.filter({ id: user.id }).getFirst({ cache: 120000 });
expect(query?.full_name).toBe('Jane Doe');
});
test('query with expired ttl', async () => {
const user = await xata.db.users.create({ full_name: 'John Doe' });
await cache.clear();
await xata.db.users.filter({ id: user.id }).getFirst();
const cacheItems = Object.entries(await cache.getAll());
expect(cacheItems).toHaveLength(1);
const [key, value] = cacheItems[0] as any;
await cache.set(key, { ...value, records: [{ ...user, full_name: 'Jane Doe' }] });
await new Promise((resolve) => setTimeout(resolve, 2000));
const query = await xata.db.users.filter({ id: user.id }).getFirst({ cache: 500 });
expect(query?.full_name).toBe('John Doe');
});
test("query with negative ttl doesn't cache", async () => {
const user = await xata.db.users.create({ full_name: 'John Doe' });
await cache.clear();
await xata.db.users.filter({ id: user.id }).getFirst();
const cacheItems = Object.entries(await cache.getAll());
expect(cacheItems).toHaveLength(1);
const [key, value] = cacheItems[0] as any;
await cache.set(key, { ...value, records: [{ ...user, full_name: 'Jane Doe' }] });
const query = await xata.db.users.filter({ id: user.id }).getFirst({ cache: -1 });
expect(query?.full_name).toBe('John Doe');
});
test('no cache', async () => {
const client1 = new XataClient({ ...clientOptions, cache: undefined });
const client2 = new XataClient({ ...clientOptions, cache: undefined });
const teamsA1 = await client1.db.teams.getAll();
const teamsA2 = await client2.db.teams.getAll();
expect(teamsA1).toHaveLength(teamsA2.length);
await client2.db.teams.create({});
const teamsB1 = await client1.db.teams.getAll();
const teamsB2 = await client2.db.teams.getAll();
expect(teamsB1).toHaveLength(teamsB2.length);
expect(teamsB1).toHaveLength(teamsA1.length + 1);
expect(teamsB2).toHaveLength(teamsA2.length + 1);
});
});