Skip to content

Commit f3bcefd

Browse files
committed
elastic infrastructure test
1 parent 2afd107 commit f3bcefd

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

src/Contexts/Backoffice/domain/BackofficeCourse.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ export class BackofficeCourse extends AggregateRoot {
4040
duration: this.duration.value
4141
};
4242
}
43+
44+
static sort(backofficeCourse1: BackofficeCourse, backofficeCourse2: BackofficeCourse): number {
45+
return backofficeCourse1?.id?.value.localeCompare(backofficeCourse2?.id?.value);
46+
}
4347
}
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { ElasticRepository } from '../../Shared/infrastructure/persistence/elasticsearch/ElasticRepository';
22
import { BackofficeCourse } from '../domain/BackofficeCourse';
33
import { BackofficeCourseRepository } from '../domain/BackofficeCourseRepository';
4-
import { Uuid } from '../../Shared/domain/value-object/Uuid';
5-
import { BackofficeCourseName } from '../domain/BackofficeCourseName';
6-
import { BackofficeCourseId } from '../domain/BackofficeCourseId';
7-
import { BackofficeCourseDuration } from '../domain/BackofficeCourseDuration';
84

95
type ElasticBackofficeCourseDocument = { _source: { id: string; duration: string; name: string } };
106

@@ -27,27 +23,12 @@ export class ElasticBackofficeCourseRepository
2723
}
2824
});
2925

30-
// console.log(response.body.hits.hits);
31-
3226
return response.body.hits.hits.map((hit: ElasticBackofficeCourseDocument) =>
3327
BackofficeCourse.fromPrimitives({ ...hit._source })
3428
);
3529
}
3630

37-
async save(course: BackofficeCourse) {
31+
async save(course: BackofficeCourse): Promise<void> {
3832
return this.persist(this.moduleName(), course);
3933
}
40-
41-
async delete() {
42-
const client = await this.client();
43-
44-
return client.deleteByQuery({
45-
index: this.moduleName(),
46-
body: {
47-
query: {
48-
match_all: {}
49-
}
50-
}
51-
});
52-
}
5334
}

src/Contexts/Shared/infrastructure/persistence/elasticsearch/ElasticRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export abstract class ElasticRepository<T extends AggregateRoot> {
1212

1313
protected async persist(index: string, aggregateRoot: T): Promise<void> {
1414
const document = { ...aggregateRoot.toPrimitives() };
15+
const client = await this.client();
1516

16-
console.log('persist', document);
17-
(await this.client()).index({ index: index, body: document });
17+
await client.index({ index: index, body: document, refresh: 'wait_for' }); // wait_for wait for a refresh to make this operation visible to search
1818
}
1919
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import container from '../../../../src/apps/backoffice/backend/config/dependency-injection';
21
import { ElasticBackofficeCourseRepository } from '../../../../src/Contexts/Backoffice/infrastructure/ElasticBackofficeCourseRepository';
32
import { ElasticClientFactory } from '../../../../src/Contexts/Shared/infrastructure/persistence/elasticsearch/ElasticClientFactory';
4-
import { EnvironmentArranger } from '../../Shared/infrastructure/arranger/EnvironmentArranger';
3+
import { ElasticEnvironmentArranger } from '../../Shared/infrastructure/elastic/ElasticEnvironmentArranger';
54
import { BackofficeCourseMother } from '../application/domain/BackofficeCourseMother';
5+
import { BackofficeCourse } from '../../../../src/Contexts/Backoffice/domain/BackofficeCourse';
66

77
const client = ElasticClientFactory.createClient('test');
88
const repository: ElasticBackofficeCourseRepository = new ElasticBackofficeCourseRepository(client);
9-
const environmentArranger: Promise<EnvironmentArranger> = container.get('Backoffice.Backend.EnvironmentArranger');
9+
const environmentArranger = new ElasticEnvironmentArranger(client);
1010

11-
beforeEach(async () => {
12-
await repository.delete();
13-
});
14-
15-
afterAll(async () => {
16-
// await repository.delete();
11+
afterEach(async () => {
12+
await environmentArranger.arrange();
1713
});
1814

1915
describe('Search all courses', () => {
2016
it('should return the existing courses', async () => {
2117
const courses = [BackofficeCourseMother.random(), BackofficeCourseMother.random()];
22-
await Promise.all(courses.map(course => repository.save(course)));
18+
19+
await Promise.all(courses.map(async course => repository.save(course)));
20+
2321
const expectedCourses = await repository.searchAll();
22+
2423
expect(courses.length).toEqual(expectedCourses.length);
25-
expect(courses.sort()).toEqual(expectedCourses.sort());
24+
expect(courses.sort(BackofficeCourse.sort)).toEqual(expectedCourses.sort(BackofficeCourse.sort));
2625
});
2726
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Client as ElasticClient } from '@elastic/elasticsearch';
2+
import { EnvironmentArranger } from '../arranger/EnvironmentArranger';
3+
4+
export class ElasticEnvironmentArranger extends EnvironmentArranger {
5+
constructor(private _client: Promise<ElasticClient>) {
6+
super();
7+
}
8+
9+
public async arrange(): Promise<void> {
10+
await this.dropAllIndex();
11+
}
12+
13+
protected async dropAllIndex(): Promise<void> {
14+
const client = await this.client();
15+
await client.indices.delete({
16+
index: '*'
17+
});
18+
}
19+
20+
protected client(): Promise<ElasticClient> {
21+
return this._client;
22+
}
23+
24+
public async close(): Promise<void> {}
25+
}

0 commit comments

Comments
 (0)