Skip to content

Commit e4fcebf

Browse files
authored
chore(tests): add zod schema validation to more tests (#628)
* chore(tests): add zod schema validation to ressource-embedding * chore(tests): add zod validation to relatinships-join * chore(tests): add zod validation to rpc tests * chore(tests): add zod validation to relationships-aggregate-operations * chore(tests): add zod validation to relationships-spread-operations * chore(tests): add zod validation to max-affected
1 parent fa6c8e1 commit e4fcebf

7 files changed

+696
-389
lines changed

test/max-affected.test.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ import { Database } from './types.override'
33
import { Database as DatabasePostgrest13 } from './types.override-with-options-postgrest13'
44
import { expectType } from 'tsd'
55
import { InvalidMethodError } from '../src/PostgrestFilterBuilder'
6-
import { Json } from './types.generated'
6+
import { z } from 'zod'
7+
import { RequiredDeep } from 'type-fest'
8+
import { TypeEqual } from 'ts-expect'
79

810
const REST_URL_13 = 'http://localhost:3001'
911
const postgrest13 = new PostgrestClient<DatabasePostgrest13>(REST_URL_13)
1012
const postgrest12 = new PostgrestClient<Database>(REST_URL_13)
1113

14+
const MessageRowSchema = z.object({
15+
channel_id: z.number(),
16+
data: z.unknown().nullable(),
17+
id: z.number(),
18+
message: z.string().nullable(),
19+
username: z.string(),
20+
})
21+
1222
describe('maxAffected', () => {
1323
test('types: maxAffected should show type warning on postgrest 12 clients', async () => {
1424
const resUpdate = await postgrest12
@@ -95,16 +105,13 @@ describe('maxAffected', () => {
95105
.eq('message', 'test2')
96106
.maxAffected(2)
97107
.select()
98-
expectType<
99-
| {
100-
channel_id: number
101-
data: Json | null
102-
id: number
103-
message: string | null
104-
username: string
105-
}[]
106-
| null
107-
>(data)
108+
109+
let result: Exclude<typeof data, null>
110+
const ExpectedSchema = z.array(MessageRowSchema)
111+
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
112+
expectType<TypeEqual<typeof result, typeof expected>>(true)
113+
ExpectedSchema.parse(data)
114+
108115
expect(error).toBeNull()
109116
expect(data).toHaveLength(1)
110117
expect(data?.[0].message).toBe('updated')

test/relationships-aggregate-operations.test.ts

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PostgrestClient } from '../src/index'
22
import { Database } from './types.override'
33
import { expectType } from 'tsd'
44
import { TypeEqual } from 'ts-expect'
5+
import { z } from 'zod'
56

67
const REST_URL = 'http://localhost:3000'
78
export const postgrest = new PostgrestClient<Database>(REST_URL)
@@ -25,13 +26,17 @@ test('select with aggregate count function', async () => {
2526
}
2627
`)
2728
let result: Exclude<typeof res.data, null>
28-
let expected: {
29-
username: string
30-
messages: Array<{
31-
count: number
32-
}>
33-
}
29+
const ExpectedSchema = z.object({
30+
username: z.string(),
31+
messages: z.array(
32+
z.object({
33+
count: z.number(),
34+
})
35+
),
36+
})
37+
let expected: z.infer<typeof ExpectedSchema>
3438
expectType<TypeEqual<typeof result, typeof expected>>(true)
39+
ExpectedSchema.parse(res.data)
3540
})
3641

3742
test('select with aggregate count on a column function', async () => {
@@ -57,13 +62,17 @@ test('select with aggregate count on a column function', async () => {
5762
}
5863
`)
5964
let result: Exclude<typeof res.data, null>
60-
let expected: {
61-
username: string
62-
messages: Array<{
63-
count: number
64-
}>
65-
}
65+
const ExpectedSchema = z.object({
66+
username: z.string(),
67+
messages: z.array(
68+
z.object({
69+
count: z.number(),
70+
})
71+
),
72+
})
73+
let expected: z.infer<typeof ExpectedSchema>
6674
expectType<TypeEqual<typeof result, typeof expected>>(true)
75+
ExpectedSchema.parse(res.data)
6776
})
6877

6978
test('select with aggregate count function and alias', async () => {
@@ -89,13 +98,17 @@ test('select with aggregate count function and alias', async () => {
8998
}
9099
`)
91100
let result: Exclude<typeof res.data, null>
92-
let expected: {
93-
username: string
94-
messages: Array<{
95-
message_count: number
96-
}>
97-
}
101+
const ExpectedSchema = z.object({
102+
username: z.string(),
103+
messages: z.array(
104+
z.object({
105+
message_count: z.number(),
106+
})
107+
),
108+
})
109+
let expected: z.infer<typeof ExpectedSchema>
98110
expectType<TypeEqual<typeof result, typeof expected>>(true)
111+
ExpectedSchema.parse(res.data)
99112
})
100113

101114
test('select with aggregate nested count function', async () => {
@@ -133,15 +146,19 @@ test('select with aggregate nested count function', async () => {
133146
}
134147
`)
135148
let result: Exclude<typeof res.data, null>
136-
let expected: {
137-
username: string
138-
messages: Array<{
139-
channels: {
140-
count: number
141-
}
142-
}>
143-
}
149+
const ExpectedSchema = z.object({
150+
username: z.string(),
151+
messages: z.array(
152+
z.object({
153+
channels: z.object({
154+
count: z.number(),
155+
}),
156+
})
157+
),
158+
})
159+
let expected: z.infer<typeof ExpectedSchema>
144160
expectType<TypeEqual<typeof result, typeof expected>>(true)
161+
ExpectedSchema.parse(res.data)
145162
})
146163

147164
test('select with aggregate nested count function and alias', async () => {
@@ -179,15 +196,19 @@ test('select with aggregate nested count function and alias', async () => {
179196
}
180197
`)
181198
let result: Exclude<typeof res.data, null>
182-
let expected: {
183-
username: string
184-
messages: Array<{
185-
channels: {
186-
channel_count: number
187-
}
188-
}>
189-
}
199+
const ExpectedSchema = z.object({
200+
username: z.string(),
201+
messages: z.array(
202+
z.object({
203+
channels: z.object({
204+
channel_count: z.number(),
205+
}),
206+
})
207+
),
208+
})
209+
let expected: z.infer<typeof ExpectedSchema>
190210
expectType<TypeEqual<typeof result, typeof expected>>(true)
211+
ExpectedSchema.parse(res.data)
191212
})
192213

193214
test('select with aggregate sum function', async () => {
@@ -209,13 +230,17 @@ test('select with aggregate sum function', async () => {
209230
}
210231
`)
211232
let result: Exclude<typeof res.data, null>
212-
let expected: {
213-
username: string
214-
messages: Array<{
215-
sum: number
216-
}>
217-
}
233+
const ExpectedSchema = z.object({
234+
username: z.string(),
235+
messages: z.array(
236+
z.object({
237+
sum: z.number(),
238+
})
239+
),
240+
})
241+
let expected: z.infer<typeof ExpectedSchema>
218242
expectType<TypeEqual<typeof result, typeof expected>>(true)
243+
ExpectedSchema.parse(res.data)
219244
})
220245

221246
test('select with aggregate aliased sum function', async () => {
@@ -241,13 +266,17 @@ test('select with aggregate aliased sum function', async () => {
241266
}
242267
`)
243268
let result: Exclude<typeof res.data, null>
244-
let expected: {
245-
username: string
246-
messages: Array<{
247-
sum_id: number
248-
}>
249-
}
269+
const ExpectedSchema = z.object({
270+
username: z.string(),
271+
messages: z.array(
272+
z.object({
273+
sum_id: z.number(),
274+
})
275+
),
276+
})
277+
let expected: z.infer<typeof ExpectedSchema>
250278
expectType<TypeEqual<typeof result, typeof expected>>(true)
279+
ExpectedSchema.parse(res.data)
251280
})
252281

253282
test('select with aggregate sum function on nested relation', async () => {
@@ -285,13 +314,17 @@ test('select with aggregate sum function on nested relation', async () => {
285314
}
286315
`)
287316
let result: Exclude<typeof res.data, null>
288-
let expected: {
289-
username: string
290-
messages: Array<{
291-
channels: {
292-
sum: number
293-
}
294-
}>
295-
}
317+
const ExpectedSchema = z.object({
318+
username: z.string(),
319+
messages: z.array(
320+
z.object({
321+
channels: z.object({
322+
sum: z.number(),
323+
}),
324+
})
325+
),
326+
})
327+
let expected: z.infer<typeof ExpectedSchema>
296328
expectType<TypeEqual<typeof result, typeof expected>>(true)
329+
ExpectedSchema.parse(res.data)
297330
})

0 commit comments

Comments
 (0)