|
1 | 1 | import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { createDatabase, createSchema, defineSchemas } from "../../src"; |
3 | | -import { array, boolean, date, mixed, number, string } from "../../src/types"; |
| 3 | +import { array, boolean, date, literal, mixed, number, object, string, tuple } from "../../src/types"; |
4 | 4 | import { createMockDatabase, mockUsers } from "../mock"; |
5 | 5 |
|
6 | 6 | describe("Update Operations", async () => { |
@@ -799,6 +799,7 @@ describe("Update Operations", async () => { |
799 | 799 | count: number(), |
800 | 800 | tags: array(string()), |
801 | 801 | alias: string().optional(), |
| 802 | + pair: tuple([string().default("X"), number().default(99)]).optional(), |
802 | 803 | }); |
803 | 804 |
|
804 | 805 | const db = createDatabase(client.db(), defineSchemas({ UpsertSchema })); |
@@ -831,13 +832,22 @@ describe("Update Operations", async () => { |
831 | 832 | expect(result?.count).toBe(0); |
832 | 833 | }); |
833 | 834 |
|
834 | | - it("$setOnInsert fields fails validation when upsert is true", async () => { |
835 | | - await expect( |
836 | | - db.collections.docs |
837 | | - // @ts-expect-error |
838 | | - .findOneAndUpdate({ name: "ghost" }, { $set: { name: "alice" }, $setOnInsert: { count: 1 } }) |
839 | | - .options({ upsert: true }), |
840 | | - ).rejects.toThrow("$setOnInsert.name: expected 'string' received 'undefined'"); |
| 835 | + it("$setOnInsert allows partial fields and generates defaults when upsert is true", async () => { |
| 836 | + const result = await db.collections.docs |
| 837 | + .findOneAndUpdate({ name: "ghost" }, { $set: { name: "alice" }, $setOnInsert: { count: 1 } }) |
| 838 | + .options({ upsert: true, returnDocument: "after" }); |
| 839 | + |
| 840 | + expect(result?.name).toBe("alice"); |
| 841 | + expect(result?.count).toBe(1); |
| 842 | + }); |
| 843 | + |
| 844 | + it("$setOnInsert safely generates default structures for tuples when upserting", async () => { |
| 845 | + const result = await db.collections.docs |
| 846 | + .findOneAndUpdate({ name: "ghost" }, { $set: { name: "alice" }, $setOnInsert: { count: 1 } }) |
| 847 | + .options({ upsert: true, returnDocument: "after" }); |
| 848 | + |
| 849 | + expect(result?.name).toBe("alice"); |
| 850 | + expect(result?.pair).toEqual(["X", 99]); |
841 | 851 | }); |
842 | 852 | }); |
843 | 853 | }); |
0 commit comments