Skip to content

Commit 94b0457

Browse files
committed
test(objectql, driver-mongodb): give each mongo write arm a fake of only the native members its path calls; type the stored-row reads (#29538)
The single-row arms' Collection fake declares insertOne / updateOne / findOne, the batch arm's declares insertMany / find (its next read is a find). The three stored-row reads in the forced objectql test edits name their object in the AST instead of erasing the options with `as any`. Claude-Session: https://claude.ai/code/session_01JQwxbXRug3R6WZAjtFuAoS Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f847848 commit 94b0457

4 files changed

Lines changed: 49 additions & 19 deletions

File tree

‎packages/drivers/driver-mongodb/src/mongodb-declared-column-null-on-write.test.ts‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ import { MongoDBDriver } from './mongodb-driver.js';
2828

2929
type Doc = Record<string, unknown>;
3030

31-
/** A fake `Db`: one collection, stored documents kept as inserted. */
32-
function fakeDb() {
31+
/**
32+
* A fake `Db`: one store of documents kept as inserted, served through a
33+
* collection that declares ONLY the native `Collection` members the path under
34+
* test calls. Two shapes, one per path: the single-row doors (`create`,
35+
* `update`, `findOne`) reach `insertOne` / `updateOne` / `findOne`; the batch
36+
* door (`bulkCreate`) reaches `insertMany`, and its next read is a `find`.
37+
*/
38+
function fakeStore() {
3339
const docs: Doc[] = [];
3440
const project = (doc: Doc, projection: Record<string, number> | undefined): Doc => {
3541
const keep = Object.keys(projection ?? {}).filter((k) => projection![k] === 1);
@@ -54,25 +60,43 @@ function fakeDb() {
5460
}
5561
return doc[k] === v;
5662
});
63+
const push = (doc: Doc) => { docs.push({ _id: `oid_${docs.length}`, ...doc }); };
64+
const pick = (filter: Doc) => docs.find((x) => matches(x, filter));
65+
const pickAll = (filter: Doc, options?: { projection?: Record<string, number> }) =>
66+
docs.filter((x) => matches(x, filter)).map((d) => project(d, options?.projection));
67+
return { docs, push, pick, pickAll, project };
68+
}
69+
70+
/** The single-row doors: `create`, `update` and `findOne`. */
71+
function singleRowDb() {
72+
const store = fakeStore();
5773
const collection = {
58-
insertOne: async (doc: Doc) => { docs.push({ _id: `oid_${docs.length}`, ...doc }); return {}; },
59-
insertMany: async (many: Doc[]) => { for (const doc of many) docs.push({ _id: `oid_${docs.length}`, ...doc }); return {}; },
74+
insertOne: async (doc: Doc) => { store.push(doc); return {}; },
6075
updateOne: async (filter: Doc, update: { $set?: Doc }) => {
6176
const updateOps = Object.keys(update);
6277
if (updateOps.length !== 1 || updateOps[0] !== '$set') throw new Error(`fake Db: update ${updateOps.join(',')} is not implemented`);
63-
const d = docs.find((x) => matches(x, filter));
78+
const d = store.pick(filter);
6479
if (d) Object.assign(d, update.$set);
6580
return { matchedCount: d ? 1 : 0 };
6681
},
6782
findOne: async (filter: Doc, options: { projection?: Record<string, number> }) => {
68-
const d = docs.find((x) => matches(x, filter));
69-
return d ? project(d, options?.projection) : null;
83+
const d = store.pick(filter);
84+
return d ? store.project(d, options?.projection) : null;
7085
},
86+
};
87+
return { db: { collection: () => collection }, docs: store.docs };
88+
}
89+
90+
/** The batch door: `bulkCreate`, and `find` for the next read. */
91+
function batchDb() {
92+
const store = fakeStore();
93+
const collection = {
94+
insertMany: async (many: Doc[]) => { for (const doc of many) store.push(doc); return {}; },
7195
find: (filter: Doc, options: { projection?: Record<string, number> }) => ({
72-
toArray: async () => docs.filter((x) => matches(x, filter)).map((d) => project(d, options?.projection)),
96+
toArray: async () => store.pickAll(filter, options),
7397
}),
7498
};
75-
return { db: { collection: () => collection }, docs };
99+
return { db: { collection: () => collection }, docs: store.docs };
76100
}
77101

78102
const OBJECT = 'nk_mongo_write_item';
@@ -84,9 +108,8 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
84108
let engine: ObjectQL;
85109
let docs: Doc[];
86110

87-
beforeEach(async () => {
111+
const boot = async (fake: { db: unknown; docs: Doc[] }) => {
88112
const driver = new MongoDBDriver({ url: 'mongodb://127.0.0.1:1/unused' });
89-
const fake = fakeDb();
90113
docs = fake.docs;
91114
(driver as any).db = fake.db;
92115
(driver as any).connect = async () => {};
@@ -104,10 +127,12 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
104127
doubled: { type: 'formula', expression: { dialect: 'cel', source: 'record.qty != null ? record.qty * 2 : -1' } },
105128
},
106129
} as never);
107-
});
130+
};
108131

109-
const expectAnswerIsNextRead = async (answer: Doc) => {
110-
expect(answer).toStrictEqual(await engine.findOne(OBJECT, { where: { id: answer.id } }));
132+
beforeEach(async () => { await boot(singleRowDb()); });
133+
134+
const expectAnswerIsNextRead = async (answer: Doc, next?: Doc) => {
135+
expect(answer).toStrictEqual(next ?? await engine.findOne(OBJECT, { where: { id: answer.id } }));
111136
expect(own(answer, 'note')).toBe(true);
112137
expect(answer.note).toBeNull();
113138
expect(own(answer, 'qty')).toBe(true);
@@ -122,10 +147,15 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
122147
await expectAnswerIsNextRead(answer);
123148
});
124149

125-
it('batch insert (bulkCreate): every answered row deep-equals its next findOne', async () => {
150+
it('batch insert (bulkCreate): every answered row deep-equals its next read', async () => {
151+
await boot(batchDb());
126152
const answers = await engine.insert(OBJECT, [{ name: 'Ada' }, { name: 'Bo' }]) as Doc[];
127153
expect(answers).toHaveLength(2);
128-
for (const answer of answers) await expectAnswerIsNextRead(answer);
154+
expect(own(docs[0], 'note')).toBe(false);
155+
for (const answer of answers) {
156+
const [next] = await engine.find(OBJECT, { where: { id: answer.id } }) as Doc[];
157+
await expectAnswerIsNextRead(answer, next);
158+
}
129159
});
130160

131161
it('update by id: the answer deep-equals the next findOne', async () => {

‎packages/objectql/src/engine-default-value-tokens.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('[#4560] the `current_user` defaultValue token is engine-owned', () =>
106106
// [#29538] The answer is the next read's record: a column nothing was
107107
// stored in reads `null`. "Unset" is the STORED row's own: no key at all.
108108
expect(row.owner).toBeNull();
109-
const stored = await engine.getDriverForObject('tok_doc')!.findOne('tok_doc', { where: { id: row.id } } as any);
109+
const stored = await engine.getDriverForObject('tok_doc')!.findOne('tok_doc', { object: 'tok_doc', where: { id: row.id } });
110110
expect(stored).not.toHaveProperty('owner');
111111
});
112112

‎packages/objectql/src/engine-lookup-referential-integrity.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ describe('[#4441] a lookup id that resolves to nothing is refused', () => {
373373
// [#29538] The answer reads the unstored column `null`, as the next read
374374
// does; the strip is the STORED row's own: no key at all.
375375
expect(row.recorded_by, 'stripped before the reference check, not refused by it').toBeNull();
376-
const stored = await engine.getDriverForObject('ref_audit_note')!.findOne('ref_audit_note', { where: { id: row.id } } as any);
376+
const stored = await engine.getDriverForObject('ref_audit_note')!.findOne('ref_audit_note', { object: 'ref_audit_note', where: { id: row.id } });
377377
expect(stored, 'stripped before the reference check, not refused by it').not.toHaveProperty('recorded_by');
378378
});
379379

‎packages/objectql/src/engine-select-option-default.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const sys = { context: { isSystem: true } } as any;
7474

7575
/** The row as the stub driver stored it — what the write persisted, before any engine shaping. */
7676
const storedRow = (engine: ObjectQL, object: string, id: unknown) =>
77-
engine.getDriverForObject(object)!.findOne(object, { where: { id } } as any);
77+
engine.getDriverForObject(object)!.findOne(object, { object, where: { id } });
7878

7979
describe('[#7246] the option marked `default: true` is the field default', () => {
8080
let engine: ObjectQL;

0 commit comments

Comments
 (0)