Skip to content

Commit 5852d49

Browse files
committed
test(driver-mongodb): one native Collection fake serves both write paths again
Claude-Session: https://claude.ai/code/session_01Utp6CXoPqLaG33jM98mL1R Co-authored-by: Claude <noreply@anthropic.com>
1 parent f273c9f commit 5852d49

1 file changed

Lines changed: 13 additions & 30 deletions

File tree

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ type Doc = Record<string, unknown>;
3030

3131
/**
3232
* 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`.
33+
* collection that declares ONLY the native `Collection` members the paths under
34+
* test call. The single-row doors (`create`, `update`, `findOne`) reach
35+
* `insertOne` / `updateOne` / `findOne`; the batch door (`bulkCreate`) reaches
36+
* `insertMany`, and its next read is a `find`.
3737
*/
38-
function fakeStore() {
38+
function fakeDb() {
3939
const docs: Doc[] = [];
4040
const project = (doc: Doc, projection: Record<string, number> | undefined): Doc => {
4141
const keep = Object.keys(projection ?? {}).filter((k) => projection![k] === 1);
@@ -62,41 +62,25 @@ function fakeStore() {
6262
});
6363
const push = (doc: Doc) => { docs.push({ _id: `oid_${docs.length}`, ...doc }); };
6464
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();
7365
const collection = {
74-
insertOne: async (doc: Doc) => { store.push(doc); return {}; },
66+
insertOne: async (doc: Doc) => { push(doc); return {}; },
67+
insertMany: async (many: Doc[]) => { for (const doc of many) push(doc); return {}; },
7568
updateOne: async (filter: Doc, update: { $set?: Doc }) => {
7669
const updateOps = Object.keys(update);
7770
if (updateOps.length !== 1 || updateOps[0] !== '$set') throw new Error(`fake Db: update ${updateOps.join(',')} is not implemented`);
78-
const d = store.pick(filter);
71+
const d = pick(filter);
7972
if (d) Object.assign(d, update.$set);
8073
return { matchedCount: d ? 1 : 0 };
8174
},
8275
findOne: async (filter: Doc, options: { projection?: Record<string, number> }) => {
83-
const d = store.pick(filter);
84-
return d ? store.project(d, options?.projection) : null;
76+
const d = pick(filter);
77+
return d ? project(d, options?.projection) : null;
8578
},
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 {}; },
9579
find: (filter: Doc, options: { projection?: Record<string, number> }) => ({
96-
toArray: async () => store.pickAll(filter, options),
80+
toArray: async () => docs.filter((x) => matches(x, filter)).map((d) => project(d, options?.projection)),
9781
}),
9882
};
99-
return { db: { collection: () => collection }, docs: store.docs };
83+
return { db: { collection: () => collection }, docs };
10084
}
10185

10286
const OBJECT = 'nk_mongo_write_item';
@@ -129,7 +113,7 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
129113
} as never);
130114
};
131115

132-
beforeEach(async () => { await boot(singleRowDb()); });
116+
beforeEach(async () => { await boot(fakeDb()); });
133117

134118
const expectAnswerIsNextRead = async (answer: Doc, next?: Doc) => {
135119
expect(answer).toStrictEqual(next ?? await engine.findOne(OBJECT, { where: { id: answer.id } }));
@@ -148,7 +132,6 @@ describe('[#29538] driver-mongodb: a write answers the record the next read answ
148132
});
149133

150134
it('batch insert (bulkCreate): every answered row deep-equals its next read', async () => {
151-
await boot(batchDb());
152135
const answers = await engine.insert(OBJECT, [{ name: 'Ada' }, { name: 'Bo' }]) as Doc[];
153136
expect(answers).toHaveLength(2);
154137
expect(own(docs[0], 'note')).toBe(false);

0 commit comments

Comments
 (0)