Skip to content

Commit 24089de

Browse files
committed
update
1 parent ae407ac commit 24089de

File tree

2 files changed

+84
-75
lines changed

2 files changed

+84
-75
lines changed

packages/orm/src/client/client-impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class ClientImpl {
135135

136136
if (options.diagnostics) {
137137
const diagnosticsSchema = z.object({
138-
slowQueryThresholdMs: z.int().nonnegative().optional(),
138+
slowQueryThresholdMs: z.number().nonnegative().optional(),
139139
slowQueryMaxRecords: z.int().nonnegative().optional(),
140140
});
141141
const parseResult = diagnosticsSchema.safeParse(options.diagnostics);

tests/e2e/orm/client-api/diagnostics.test.ts

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,128 @@
1-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2-
import type { ClientContract } from '@zenstackhq/orm';
1+
import { describe, expect, it } from 'vitest';
32
import { schema } from '../schemas/basic';
43
import { createTestClient } from '@zenstackhq/testtools';
54

65
describe('Client $diagnostics tests', () => {
76
describe('without diagnostics option', () => {
8-
let client: ClientContract<typeof schema>;
9-
10-
beforeEach(async () => {
11-
client = await createTestClient(schema);
12-
});
13-
14-
afterEach(async () => {
15-
await client?.$disconnect();
16-
});
17-
187
it('returns zod cache stats', async () => {
19-
const diagnostics = await client.$diagnostics();
20-
expect(diagnostics.zodCache).toEqual({ size: 0, keys: [] });
8+
const client = await createTestClient(schema);
9+
try {
10+
const diagnostics = await client.$diagnostics();
11+
expect(diagnostics.zodCache).toEqual({ size: 0, keys: [] });
12+
} finally {
13+
await client.$disconnect();
14+
}
2115
});
2216

2317
it('returns zod cache stats after queries', async () => {
24-
await client.user.create({ data: { email: 'u1@test.com' } });
25-
const diagnostics = await client.$diagnostics();
26-
expect(diagnostics.zodCache.size).toBeGreaterThan(0);
27-
expect(diagnostics.zodCache.keys.length).toBe(diagnostics.zodCache.size);
18+
const client = await createTestClient(schema);
19+
try {
20+
await client.user.create({ data: { email: 'u1@test.com' } });
21+
const diagnostics = await client.$diagnostics();
22+
expect(diagnostics.zodCache.size).toBeGreaterThan(0);
23+
expect(diagnostics.zodCache.keys.length).toBe(diagnostics.zodCache.size);
24+
} finally {
25+
await client.$disconnect();
26+
}
2827
});
2928

3029
it('returns empty slow queries when diagnostics option is not set', async () => {
31-
await client.user.create({ data: { email: 'u1@test.com' } });
32-
await client.user.findMany();
33-
const diagnostics = await client.$diagnostics();
34-
expect(diagnostics.slowQueries).toEqual([]);
30+
const client = await createTestClient(schema);
31+
try {
32+
await client.user.create({ data: { email: 'u1@test.com' } });
33+
await client.user.findMany();
34+
const diagnostics = await client.$diagnostics();
35+
expect(diagnostics.slowQueries).toEqual([]);
36+
} finally {
37+
await client.$disconnect();
38+
}
3539
});
3640
});
3741

3842
describe('with diagnostics option', () => {
39-
let client: ClientContract<typeof schema>;
40-
41-
beforeEach(async () => {
42-
client = await createTestClient(schema, {
43-
diagnostics: {
44-
// threshold of 0ms ensures all queries are captured as "slow"
45-
slowQueryThresholdMs: 0,
46-
},
43+
it('records slow queries when threshold is exceeded', async () => {
44+
const client = await createTestClient(schema, {
45+
diagnostics: { slowQueryThresholdMs: 0 },
4746
});
48-
});
49-
50-
afterEach(async () => {
51-
await client?.$disconnect();
52-
});
47+
try {
48+
await client.user.create({ data: { email: 'u1@test.com' } });
49+
await client.user.findMany();
5350

54-
it('records slow queries when threshold is exceeded', async () => {
55-
await client.user.create({ data: { email: 'u1@test.com' } });
56-
await client.user.findMany();
57-
58-
const diagnostics = await client.$diagnostics();
59-
expect(diagnostics.slowQueries.length).toBeGreaterThan(0);
60-
for (const query of diagnostics.slowQueries) {
61-
expect(query.durationMs).toBeGreaterThanOrEqual(0);
62-
expect(query.sql).toBeTruthy();
51+
const diagnostics = await client.$diagnostics();
52+
expect(diagnostics.slowQueries.length).toBeGreaterThan(0);
53+
for (const query of diagnostics.slowQueries) {
54+
expect(query.durationMs).toBeGreaterThanOrEqual(0);
55+
expect(query.sql).toBeTruthy();
56+
}
57+
} finally {
58+
await client.$disconnect();
6359
}
6460
});
6561

6662
it('does not record queries below threshold', async () => {
67-
const fastClient = await createTestClient(schema, {
68-
diagnostics: {
69-
// very high threshold to ensure no queries are captured
70-
slowQueryThresholdMs: 999999,
71-
},
63+
const client = await createTestClient(schema, {
64+
diagnostics: { slowQueryThresholdMs: 999999 },
7265
});
73-
7466
try {
75-
await fastClient.user.create({ data: { email: 'u1@test.com' } });
76-
await fastClient.user.findMany();
67+
await client.user.create({ data: { email: 'u1@test.com' } });
68+
await client.user.findMany();
7769

78-
const diagnostics = await fastClient.$diagnostics();
70+
const diagnostics = await client.$diagnostics();
7971
expect(diagnostics.slowQueries).toEqual([]);
8072
} finally {
81-
await fastClient.$disconnect();
73+
await client.$disconnect();
8274
}
8375
});
8476

8577
it('returns a copy of slow queries', async () => {
86-
await client.user.create({ data: { email: 'u1@test.com' } });
78+
const client = await createTestClient(schema, {
79+
diagnostics: { slowQueryThresholdMs: 0 },
80+
});
81+
try {
82+
await client.user.create({ data: { email: 'u1@test.com' } });
8783

88-
const diagnostics1 = await client.$diagnostics();
89-
const diagnostics2 = await client.$diagnostics();
90-
expect(diagnostics1.slowQueries).not.toBe(diagnostics2.slowQueries);
91-
expect(diagnostics1.slowQueries).toEqual(diagnostics2.slowQueries);
84+
const diagnostics1 = await client.$diagnostics();
85+
const diagnostics2 = await client.$diagnostics();
86+
expect(diagnostics1.slowQueries).not.toBe(diagnostics2.slowQueries);
87+
expect(diagnostics1.slowQueries).toEqual(diagnostics2.slowQueries);
88+
} finally {
89+
await client.$disconnect();
90+
}
9291
});
9392

9493
it('shares slow queries across derived clients', async () => {
95-
await client.user.create({ data: { email: 'u1@test.com' } });
94+
const client = await createTestClient(schema, {
95+
diagnostics: { slowQueryThresholdMs: 0 },
96+
});
97+
try {
98+
await client.user.create({ data: { email: 'u1@test.com' } });
9699

97-
const derivedClient = client.$setAuth({ id: '1' });
98-
await derivedClient.user.findMany();
100+
const derivedClient = client.$setAuth({ id: '1' });
101+
await derivedClient.user.findMany();
99102

100-
// both clients should see the same slow queries
101-
const parentDiag = await client.$diagnostics();
102-
const derivedDiag = await derivedClient.$diagnostics();
103-
expect(parentDiag.slowQueries).toEqual(derivedDiag.slowQueries);
103+
// both clients should see the same slow queries
104+
const parentDiag = await client.$diagnostics();
105+
const derivedDiag = await derivedClient.$diagnostics();
106+
expect(parentDiag.slowQueries).toEqual(derivedDiag.slowQueries);
107+
} finally {
108+
await client.$disconnect();
109+
}
104110
});
105111

106112
it('shares slow queries across transaction clients', async () => {
107-
await client.$transaction(async (tx) => {
108-
await tx.user.create({ data: { email: 'u1@test.com' } });
113+
const client = await createTestClient(schema, {
114+
diagnostics: { slowQueryThresholdMs: 0 },
109115
});
116+
try {
117+
await client.$transaction(async (tx) => {
118+
await tx.user.create({ data: { email: 'u1@test.com' } });
119+
});
110120

111-
const diagnostics = await client.$diagnostics();
112-
expect(diagnostics.slowQueries.length).toBeGreaterThan(0);
121+
const diagnostics = await client.$diagnostics();
122+
expect(diagnostics.slowQueries.length).toBeGreaterThan(0);
123+
} finally {
124+
await client.$disconnect();
125+
}
113126
});
114127
});
115128

@@ -122,9 +135,7 @@ describe('Client $diagnostics tests', () => {
122135
slowQueryMaxRecords: maxRecords,
123136
},
124137
});
125-
126138
try {
127-
// create enough queries to exceed the limit
128139
for (let i = 0; i < 10; i++) {
129140
await client.user.create({ data: { email: `u${i}@test.com` } });
130141
}
@@ -144,15 +155,13 @@ describe('Client $diagnostics tests', () => {
144155
slowQueryMaxRecords: maxRecords,
145156
},
146157
});
147-
148158
try {
149159
for (let i = 0; i < 5; i++) {
150160
await client.user.create({ data: { email: `u${i}@test.com` } });
151161
}
152162

153163
const diagnostics = await client.$diagnostics();
154164
expect(diagnostics.slowQueries.length).toBeLessThanOrEqual(maxRecords);
155-
// all entries should have valid data
156165
for (const query of diagnostics.slowQueries) {
157166
expect(query.durationMs).toBeGreaterThanOrEqual(0);
158167
expect(query.sql).toBeTruthy();

0 commit comments

Comments
 (0)