Skip to content

Commit bc53552

Browse files
committed
Refactor EnvironmentArranger
1 parent d03412b commit bc53552

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

src/apps/mooc_backend/config/dependency-injection/application_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ imports:
33

44
services:
55
Mooc.EnvironmentArranger:
6-
class: ../../../../../tests/Contexts/Shared/infrastructure/arranger/MongoEnvironmentArranger
6+
class: ../../../../../tests/Contexts/Shared/infrastructure/mongo/MongoEnvironmentArranger
77
arguments: ['@Mooc.ConnectionManager']

tests/Contexts/Mooc/Courses/infrastructure/persistence/CourseRepository.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const repository: CourseRepository = container.get('Mooc.courses.CourseRepositor
77
const environmentArranger: Promise<EnvironmentArranger> = container.get('Mooc.EnvironmentArranger');
88

99
beforeEach(async () => {
10-
await (await environmentArranger).arrange('courses');
10+
await (await environmentArranger).arrange();
1111
});
1212

1313
afterAll(async () => {
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
export abstract class EnvironmentArranger {
2-
public async arrange(name: string): Promise<void> {
3-
await this.clean(name);
4-
}
5-
6-
protected abstract clean(name: string): Promise<void>;
2+
public abstract arrange(): Promise<void>;
73

84
public abstract close(): Promise<void>;
95
}

tests/Contexts/Shared/infrastructure/arranger/MongoEnvironmentArranger.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { MongoClient } from 'mongodb';
2+
import { EnvironmentArranger } from '../arranger/EnvironmentArranger';
3+
4+
export class MongoEnvironmentArranger extends EnvironmentArranger {
5+
constructor(private _client: Promise<MongoClient>) {
6+
super();
7+
}
8+
9+
public async arrange(): Promise<void> {
10+
await this.cleanDatabase();
11+
}
12+
13+
protected async cleanDatabase(): Promise<void> {
14+
const collections = await this.collections();
15+
16+
for (const collection of collections) {
17+
await (await this.client()).db().dropCollection(collection);
18+
}
19+
}
20+
21+
private async collections(): Promise<string[]> {
22+
const client = await this.client();
23+
const collections = await client
24+
.db()
25+
.listCollections(undefined, { nameOnly: true })
26+
.toArray();
27+
28+
return collections.map(collection => collection.name);
29+
}
30+
31+
protected client(): Promise<MongoClient> {
32+
return this._client;
33+
}
34+
35+
public async close(): Promise<void> {
36+
return (await this.client()).close();
37+
}
38+
}

0 commit comments

Comments
 (0)