|
| 1 | +import { runUnifiedSuite } from './unified-spec-runner/runner'; |
| 2 | +import { |
| 3 | + type CollectionData, |
| 4 | + type EntityDescription, |
| 5 | + type ExpectedEventsForClient, |
| 6 | + type OperationDescription, |
| 7 | + type RunOnRequirement, |
| 8 | + type Test, |
| 9 | + type UnifiedSuite |
| 10 | +} from './unified-spec-runner/schema'; |
| 11 | + |
| 12 | +export class TestBuilder { |
| 13 | + private _description: string; |
| 14 | + private runOnRequirements: RunOnRequirement[] = []; |
| 15 | + private _skipReason?: string; |
| 16 | + private _operations: OperationDescription[] = []; |
| 17 | + private _expectEvents?: ExpectedEventsForClient[] = []; |
| 18 | + private _outcome?: CollectionData[] = []; |
| 19 | + |
| 20 | + static it(title: string) { |
| 21 | + return new TestBuilder(title); |
| 22 | + } |
| 23 | + |
| 24 | + constructor(description: string) { |
| 25 | + this._description = description; |
| 26 | + } |
| 27 | + |
| 28 | + operation(operation: OperationDescription): this { |
| 29 | + this._operations.push({ |
| 30 | + object: 'collection0', |
| 31 | + arguments: {}, |
| 32 | + ...operation |
| 33 | + }); |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + runOnRequirement(requirement: RunOnRequirement): this { |
| 38 | + this.runOnRequirements.push(requirement); |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + expectEvents(event: ExpectedEventsForClient): this { |
| 43 | + this._expectEvents.push(event); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + toJSON(): Test { |
| 48 | + const test: Test = { |
| 49 | + description: this._description, |
| 50 | + runOnRequirements: this.runOnRequirements, |
| 51 | + operations: this._operations, |
| 52 | + expectEvents: this._expectEvents, |
| 53 | + outcome: this._outcome |
| 54 | + }; |
| 55 | + |
| 56 | + if (this._skipReason != null) { |
| 57 | + test.skipReason = this._skipReason; |
| 58 | + } |
| 59 | + |
| 60 | + return test; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export class UnifiedTestSuiteBuilder { |
| 65 | + private _description = 'Default Description'; |
| 66 | + private _schemaVersion = '1.0'; |
| 67 | + private _createEntities: EntityDescription[]; |
| 68 | + private _runOnRequirement: RunOnRequirement[] = []; |
| 69 | + private _initialData: CollectionData[] = []; |
| 70 | + private _tests: Test[] = []; |
| 71 | + |
| 72 | + static describe(title: string) { |
| 73 | + return new UnifiedTestSuiteBuilder(title); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Establish common defaults |
| 78 | + * - id and name = client0, listens for commandStartedEvent |
| 79 | + * - id and name = database0 |
| 80 | + * - id and name = collection0 |
| 81 | + */ |
| 82 | + static get defaultEntities(): EntityDescription[] { |
| 83 | + return [ |
| 84 | + { |
| 85 | + client: { |
| 86 | + id: 'client0', |
| 87 | + useMultipleMongoses: true, |
| 88 | + observeEvents: ['commandStartedEvent'] |
| 89 | + } |
| 90 | + }, |
| 91 | + { |
| 92 | + database: { |
| 93 | + id: 'database0', |
| 94 | + client: 'client0', |
| 95 | + databaseName: 'database0' |
| 96 | + } |
| 97 | + }, |
| 98 | + { |
| 99 | + collection: { |
| 100 | + id: 'collection0', |
| 101 | + database: 'database0', |
| 102 | + collectionName: 'collection0' |
| 103 | + } |
| 104 | + } |
| 105 | + ]; |
| 106 | + } |
| 107 | + |
| 108 | + constructor(description: string) { |
| 109 | + this._description = description; |
| 110 | + this._createEntities = []; |
| 111 | + } |
| 112 | + |
| 113 | + description(description: string): this { |
| 114 | + this._description = description; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + test(test: Test): this; |
| 119 | + test(test: Test[]): this; |
| 120 | + test(test: Test | Test[]): this { |
| 121 | + if (Array.isArray(test)) { |
| 122 | + this._tests.push(...test); |
| 123 | + } else { |
| 124 | + this._tests.push(test); |
| 125 | + } |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + createEntities(entity: EntityDescription): this; |
| 130 | + createEntities(entity: EntityDescription[]): this; |
| 131 | + createEntities(entity: EntityDescription | EntityDescription[]): this { |
| 132 | + if (Array.isArray(entity)) { |
| 133 | + this._createEntities.push(...entity); |
| 134 | + } else { |
| 135 | + this._createEntities.push(entity); |
| 136 | + } |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + initialData(data: CollectionData): this; |
| 141 | + initialData(data: CollectionData[]): this; |
| 142 | + initialData(data: CollectionData | CollectionData[]): this { |
| 143 | + if (Array.isArray(data)) { |
| 144 | + this._initialData.push(...data); |
| 145 | + } else { |
| 146 | + this._initialData.push(data); |
| 147 | + } |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + runOnRequirement(requirement: RunOnRequirement): this; |
| 152 | + runOnRequirement(requirement: RunOnRequirement[]): this; |
| 153 | + runOnRequirement(requirement: RunOnRequirement | RunOnRequirement[]): this { |
| 154 | + Array.isArray(requirement) |
| 155 | + ? this._runOnRequirement.push(...requirement) |
| 156 | + : this._runOnRequirement.push(requirement); |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + schemaVersion(version: string): this { |
| 161 | + this._schemaVersion = version; |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + toJSON(): UnifiedSuite { |
| 166 | + return { |
| 167 | + description: this._description, |
| 168 | + schemaVersion: this._schemaVersion, |
| 169 | + runOnRequirements: this._runOnRequirement, |
| 170 | + createEntities: this._createEntities, |
| 171 | + initialData: this._initialData, |
| 172 | + tests: this._tests |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + run(): void { |
| 177 | + return runUnifiedSuite([this.toJSON()]); |
| 178 | + } |
| 179 | + |
| 180 | + toMocha() { |
| 181 | + return describe(this._description, () => runUnifiedSuite([this.toJSON()])); |
| 182 | + } |
| 183 | + |
| 184 | + clone(): UnifiedSuite { |
| 185 | + return JSON.parse(JSON.stringify(this)); |
| 186 | + } |
| 187 | +} |
0 commit comments