Skip to content

Commit

Permalink
chore: add tests for SavePlayoutModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Mar 5, 2024
1 parent 1a0fbca commit d2f12b6
Show file tree
Hide file tree
Showing 2 changed files with 399 additions and 10 deletions.
14 changes: 4 additions & 10 deletions packages/job-worker/src/__mocks__/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
async remove(selector: MongoQuery<TDoc> | TDoc['_id']): Promise<number> {
this.#ops.push({ type: 'remove', args: [selector] })

return this.removeInner(selector)
}
private async removeInner(selector: MongoQuery<TDoc> | TDoc['_id']): Promise<number> {
const docs: Pick<TDoc, '_id'>[] = await this.findFetchInner(selector, { projection: { _id: 1 } })
for (const doc of docs) {
this.#documents.delete(doc._id)
Expand All @@ -186,15 +183,15 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
return docs.length
}
async update(selector: MongoQuery<TDoc> | TDoc['_id'], modifier: MongoModifier<TDoc>): Promise<number> {
this.#ops.push({ type: 'update', args: [selector, modifier] })

return this.updateInner(selector, modifier, false)
}
private async updateInner(
selector: MongoQuery<TDoc> | TDoc['_id'],
modifier: MongoModifier<TDoc>,
single: boolean
) {
this.#ops.push({ type: 'update', args: [selector, modifier] })

const docs = await this.findFetchInner(selector)

for (const doc of docs) {
Expand All @@ -210,9 +207,6 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
async replace(doc: TDoc | ReadonlyDeep<TDoc>): Promise<boolean> {
this.#ops.push({ type: 'replace', args: [doc._id] })

return this.replaceInner(doc)
}
private async replaceInner(doc: TDoc | ReadonlyDeep<TDoc>): Promise<boolean> {
if (!doc._id) throw new Error(`replace requires document to have an _id`)

const exists = this.#documents.has(doc._id)
Expand All @@ -228,9 +222,9 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
} else if ('updateOne' in op) {
await this.updateInner(op.updateOne.filter, op.updateOne.update, true)
} else if ('replaceOne' in op) {
await this.replaceInner(op.replaceOne.replacement as any)
await this.replace(op.replaceOne.replacement as any)
} else if ('deleteMany' in op) {
await this.removeInner(op.deleteMany.filter)
await this.remove(op.deleteMany.filter)
} else {
// Note: implement more as we start using them
throw new Error(`Unknown mongo Bulk Operation: ${JSON.stringify(op)}`)
Expand Down
Loading

0 comments on commit d2f12b6

Please sign in to comment.