Skip to content

Commit

Permalink
Feat: zod collection tests (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeprinz-1 authored Jul 30, 2024
1 parent ec5631a commit f7b68ae
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/zod-collection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { z } from 'zod'
import { convertToZodCollection } from '../src'
import { closeDb, mkTsTestCollection } from './util'

const ZExample = z.object({
a: z.number(),
b: z.string(),
numbers: z.number().array(),
})

const mkZodCollection = async () => {
const col = await mkTsTestCollection()
// use any here so we can insert wrong values without upsetting typescript
const zodCol = convertToZodCollection<any>(col, ZExample)
return zodCol
}

test('Documents should be parsed correctly on insert', async () => {
const zodCol = await mkZodCollection()

expect(() =>
zodCol.insertOne({ a: 'string', b: 'string', numbers: [9] })
).toThrow()
await expect(
zodCol.insertOne({ a: 0, b: 'string', numbers: [0] })
).resolves.toBeTruthy()

expect(() => zodCol.insertMany([{ a: 0, b: 0 }])).toThrow()
await expect(
zodCol.insertMany([{ a: 0, b: 'string', numbers: [7] }])
).resolves.toBeTruthy()
})

test('Documents should be parsed correctly on update', async () => {
const zodCol = await mkZodCollection()

expect(() => zodCol.updateOne({}, { $set: { a: 'string' as any } })).toThrow()
await expect(
zodCol.updateOne({}, { $set: { a: 0 as any } })
).resolves.toBeTruthy()

expect(() => zodCol.updateOne({}, { $pull: { a: 0 as any } })).toThrow()
await expect(
zodCol.updateOne({}, { $pull: { numbers: 0 as any } })
).resolves.toBeTruthy()

expect(() => zodCol.updateOne({}, { $push: { a: 0 as any } })).toThrow()
await expect(
zodCol.updateOne({}, { $push: { numbers: 0 as any } })
).resolves.toBeTruthy()

expect(() => zodCol.updateOne({}, { $addToSet: { a: 0 as any } })).toThrow()
await expect(
zodCol.updateOne({}, { $addToSet: { numbers: 0 as any } })
).resolves.toBeTruthy()

expect(() =>
zodCol.updateOne({}, { $setOnInsert: { a: 'string' as any } })
).toThrow()
await expect(
zodCol.updateOne({}, { $setOnInsert: { a: 0 as any } })
).resolves.toBeTruthy()
})

afterAll(() => closeDb())

0 comments on commit f7b68ae

Please sign in to comment.