|
1 |
| -import { Collection } from "./deps.ts" |
| 1 | +import { Collection, Filter, CountOptions, AggregatePipeline, AggregateOptions, DeleteOptions, DropOptions, DropIndexOptions, DistinctOptions, FindOptions, IndexOptions, InsertOptions, UpdateOptions, ConnectOptions, CollationOptions, CreateUserOptions, CreateIndexOptions, FindAndModifyOptions, InsertDocument, Document, Bson, FindCursor, AggregateCursor, ListIndexesCursor, UpdateFilter} from "./deps.ts" |
2 | 2 | import { spy } from "./deps.ts"
|
3 | 3 |
|
4 | 4 |
|
5 |
| -/** |
6 |
| - * |
7 |
| - * @param additional additional properties that should be set explicitly |
8 |
| - * @returns a spy-mock for mongo_deno Collections |
9 |
| - */ |
10 | 5 |
|
11 |
| -export function getMockCollection<T>(additional: Partial<Collection<T>>): Collection<T> { |
12 |
| - let props: Partial<Collection<T>> = { |
13 |
| - name: "" |
| 6 | + |
| 7 | +export class MockCollection<T> extends Collection<T> { |
| 8 | + static instance: Collection<any> |
| 9 | + |
| 10 | + static getInstance(): Collection<any> { |
| 11 | + return MockCollection.instance; |
| 12 | + } |
| 13 | + static initMock<T>(additional: Partial<Collection<T>>) { |
| 14 | + MockCollection.instance = MockCollection.getMockCollection<T>(additional) |
14 | 15 | }
|
15 | 16 |
|
16 |
| - let methods: (keyof Omit<Collection<T>, "name">)[] = [ |
17 |
| - "aggregate", |
18 |
| - "count", |
19 |
| - "countDocuments", |
20 |
| - "createIndexes", |
21 |
| - "delete", |
22 |
| - "deleteMany", |
23 |
| - "deleteOne", |
24 |
| - "distinct", |
25 |
| - "drop", |
26 |
| - "dropIndexes", |
27 |
| - "estimatedDocumentCount", |
28 |
| - "find", |
29 |
| - "findAndModify", |
30 |
| - "findOne", |
31 |
| - "insert", |
32 |
| - "insertMany", |
33 |
| - "insertOne", |
34 |
| - "listIndexes", |
35 |
| - "replaceOne", |
36 |
| - "updateMany", |
37 |
| - "updateOne" |
38 |
| - ] |
39 |
| - |
40 |
| - |
41 |
| - |
42 |
| - let mockCollection = { |
43 |
| - ...props, |
44 |
| - ...additional |
| 17 | + static getMockWithProxy<T>() { |
| 18 | + return MockCollection.getMockCollection({ |
| 19 | + aggregate: (pipeline: AggregatePipeline<any>[], options?: AggregateOptions | undefined): AggregateCursor<any> => MockCollection.instance.aggregate(pipeline, options), |
| 20 | + countDocuments: (filter?: Filter<unknown> | undefined, options?: CountOptions | undefined): Promise<number> => MockCollection.instance.countDocuments(filter, options), |
| 21 | + createIndexes: (options: CreateIndexOptions): Promise<{ |
| 22 | + ok: number; |
| 23 | + createdCollectionAutomatically: boolean; |
| 24 | + numIndexesBefore: number; |
| 25 | + numIndexesAfter: number; |
| 26 | + }> => MockCollection.instance.createIndexes(options), |
| 27 | + delete: (filter: Filter<unknown>, options?: DeleteOptions | undefined): Promise<number> => MockCollection.instance.delete(filter, options), |
| 28 | + deleteMany: (filter: Filter<unknown>, options?: DeleteOptions | undefined): Promise<number> => MockCollection.instance.deleteMany(filter, options), |
| 29 | + deleteOne: (filter: Filter<unknown>, options?: DeleteOptions | undefined): Promise<number> => MockCollection.instance.deleteOne(filter, options), |
| 30 | + distinct: (key: string, query?: Filter<unknown> | undefined, options?: DistinctOptions | undefined): Promise<any> => MockCollection.instance.distinct(key, query, options), |
| 31 | + drop: (options?: DropOptions | undefined): Promise<void> => MockCollection.instance.drop(options), |
| 32 | + dropIndexes: (options: DropIndexOptions): Promise<{ |
| 33 | + ok: number; |
| 34 | + nIndexesWas: number; |
| 35 | + }> => MockCollection.instance.dropIndexes(options), |
| 36 | + estimatedDocumentCount: (): Promise<number> => MockCollection.instance.estimatedDocumentCount(), |
| 37 | + find: (filter?: Filter<unknown> | undefined, options?: FindOptions | undefined): FindCursor<unknown> => MockCollection.instance.find(filter, options), |
| 38 | + findAndModify: (filter?: Filter<unknown> | undefined, options?: FindAndModifyOptions<unknown> | undefined): Promise<unknown> => MockCollection.instance.findAndModify(filter, options), |
| 39 | + findOne: (filter?: Filter<unknown> | undefined, options?: FindOptions | undefined): Promise<unknown> => MockCollection.instance.findOne(filter, options), |
| 40 | + insertMany: (docs: InsertDocument<T>[], options?: InsertOptions | undefined): Promise<{ |
| 41 | + insertedIds: unknown[]; |
| 42 | + insertedCount: number; |
| 43 | + }> => MockCollection.instance.insertMany(docs, options), |
| 44 | + insertOne: (doc: InsertDocument<T>, options?: InsertOptions | undefined): Promise<unknown> => MockCollection.instance.insertOne(doc, options), |
| 45 | + listIndexes: (): ListIndexesCursor<{ |
| 46 | + v: number; |
| 47 | + key: Document; |
| 48 | + name: string; |
| 49 | + ns?: string | undefined; |
| 50 | + }> => MockCollection.instance.listIndexes(), |
| 51 | + replaceOne: (filter: Filter<unknown>, replacement: InsertDocument<T>, options?: UpdateOptions | undefined): Promise<{ |
| 52 | + upsertedId: Bson.ObjectId; |
| 53 | + upsertedCount: number; |
| 54 | + matchedCount: number; |
| 55 | + modifiedCount: number; |
| 56 | + }> => MockCollection.instance.replaceOne(filter, replacement, options), |
| 57 | + updateMany: (filter: Filter<unknown>, doc: UpdateFilter<unknown>, options?: UpdateOptions | undefined): Promise<{ |
| 58 | + upsertedIds: Bson.ObjectId[] | undefined; |
| 59 | + upsertedCount: number; |
| 60 | + modifiedCount: number; |
| 61 | + matchedCount: number; |
| 62 | + }> => MockCollection.instance.updateMany(filter, doc, options), |
| 63 | + updateOne: (filter: Filter<unknown>, update: UpdateFilter<unknown>, options?: UpdateOptions | undefined): Promise<{ |
| 64 | + upsertedId: Bson.ObjectId; |
| 65 | + upsertedCount: number; |
| 66 | + matchedCount: number; |
| 67 | + modifiedCount: number; |
| 68 | + }> => MockCollection.instance.updateOne(filter, update, options) |
| 69 | + }) |
45 | 70 | }
|
46 | 71 |
|
| 72 | + private static getMockCollection<T>(additional: Partial<Collection<T>>): Collection<T> { |
| 73 | + const props: Partial<Collection<T>> = { |
| 74 | + name: "", |
| 75 | + } |
| 76 | + |
| 77 | + const methods: (keyof Omit<Collection<T>, "name">)[] = [ |
| 78 | + "aggregate", |
| 79 | + "count", |
| 80 | + "countDocuments", |
| 81 | + "createIndexes", |
| 82 | + "delete", |
| 83 | + "deleteMany", |
| 84 | + "deleteOne", |
| 85 | + "distinct", |
| 86 | + "drop", |
| 87 | + "dropIndexes", |
| 88 | + "estimatedDocumentCount", |
| 89 | + "find", |
| 90 | + "findAndModify", |
| 91 | + "findOne", |
| 92 | + "insert", |
| 93 | + "insertMany", |
| 94 | + "insertOne", |
| 95 | + "listIndexes", |
| 96 | + "replaceOne", |
| 97 | + "updateMany", |
| 98 | + "updateOne" |
| 99 | + ] |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + const mockCollection = { |
| 104 | + ...props, |
| 105 | + ...additional |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + methods.forEach((method) => { |
| 110 | + mockCollection[method] = additional ? _wrapSpy(mockCollection[method]) : _wrapSpy(); |
| 111 | + }); |
| 112 | + |
| 113 | + |
| 114 | + return mockCollection as Collection<T> |
| 115 | + } |
47 | 116 |
|
48 |
| - methods.forEach((method) => { |
49 |
| - mockCollection[method] = additional ? _wrapSpy(mockCollection[method]) : _wrapSpy(); |
50 |
| - }); |
51 | 117 |
|
52 | 118 |
|
53 |
| - return mockCollection as Collection<T> |
54 | 119 | }
|
55 | 120 |
|
56 | 121 | export function _wrapSpy(func?: Function) {
|
|
0 commit comments