Skip to content

Commit ceb2482

Browse files
author
Artem
committed
#RI-4489 UTests + endpoint for manually sync
1 parent 5a77fb4 commit ceb2482

File tree

3 files changed

+50
-34
lines changed

3 files changed

+50
-34
lines changed

redisinsight/api/src/modules/feature/feature.controller.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import {
22
Controller,
3-
Get,
3+
Get, HttpCode, Post,
44
UsePipes,
5-
ValidationPipe,
5+
ValidationPipe
66
} from '@nestjs/common';
77
import { ApiTags } from '@nestjs/swagger';
88
import { ApiEndpoint } from 'src/decorators/api-endpoint.decorator';
99
import { FeatureService } from 'src/modules/feature/feature.service';
10+
import { FeaturesConfigService } from 'src/modules/feature/features-config.service';
1011

1112
@ApiTags('Info')
1213
@Controller('features')
1314
@UsePipes(new ValidationPipe({ transform: true }))
1415
export class FeatureController {
1516
constructor(
1617
private featureService: FeatureService,
18+
private featuresConfigService: FeaturesConfigService,
1719
) {}
1820

1921
@Get('')
@@ -30,4 +32,10 @@ export class FeatureController {
3032
async list(): Promise<any> {
3133
return this.featureService.list();
3234
}
35+
36+
@Post('/sync')
37+
@HttpCode(200)
38+
async sync(): Promise<void> {
39+
return this.featuresConfigService.sync();
40+
}
3341
}

redisinsight/api/src/modules/feature/features-config.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class FeaturesConfigService {
5050
/**
5151
* Get latest config from remote and save it in the local database
5252
*/
53-
private async sync() {
53+
public async sync() {
5454
try {
5555
this.logger.log('Trying to sync features config...');
5656

redisinsight/api/src/modules/feature/repositories/local.features-config.repository.spec.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@ import { getRepositoryToken } from '@nestjs/typeorm';
33
import { Repository } from 'typeorm';
44
import {
55
mockAgreements,
6-
mockAgreementsEntity, mockFeaturesConfig, mockFeaturesConfigEntity, mockFeaturesConfigId, mockFeaturesConfigJson,
6+
mockAgreementsEntity,
7+
mockFeaturesConfig,
8+
mockFeaturesConfigData,
9+
mockFeaturesConfigEntity,
10+
mockFeaturesConfigId,
11+
mockFeaturesConfigJson,
712
mockRepository,
8-
MockType, mockUserId
13+
MockType,
14+
mockUserId
915
} from 'src/__mocks__';
1016
import { AgreementsEntity } from 'src/modules/settings/entities/agreements.entity';
1117
import { LocalFeaturesConfigRepository } from 'src/modules/feature/repositories/local.features-config.repository';
1218
import { FeaturesConfigEntity } from 'src/modules/feature/entities/features-config.entity';
1319
import { classToPlain, plainToClass } from 'class-transformer';
1420
import { FeaturesConfigData } from 'src/modules/feature/model/features-config';
21+
import * as defaultConfig from '../../../../config/features-config.json';
22+
1523
describe('LocalFeaturesConfigRepository', () => {
1624
let service: LocalFeaturesConfigRepository;
1725
let repository: MockType<Repository<FeaturesConfigEntity>>;
@@ -33,7 +41,7 @@ describe('LocalFeaturesConfigRepository', () => {
3341
service = await module.get(LocalFeaturesConfigRepository);
3442

3543
repository.findOneBy.mockResolvedValue(mockFeaturesConfigEntity);
36-
repository.update.mockResolvedValue(true); // no meter of response
44+
repository.update.mockResolvedValue(mockFeaturesConfigEntity);
3745
repository.save.mockResolvedValue(mockFeaturesConfigEntity);
3846
});
3947

@@ -73,34 +81,34 @@ describe('LocalFeaturesConfigRepository', () => {
7381
});
7482

7583
describe('getOrCreate', () => {
76-
it('ttt', async () => {
84+
it('should return existing config', async () => {
85+
const result = await service.getOrCreate();
86+
87+
expect(result).toEqual(mockFeaturesConfig);
7788
});
78-
// it('should return existing config', async () => {
79-
// const result = await service.getOrCreate();
80-
//
81-
// expect(result).toEqual(mockFeaturesConfig);
82-
// });
83-
// it('should create new config', async () => {
84-
// repository.findOneBy.mockResolvedValueOnce(null);
85-
//
86-
// const result = await service.getOrCreate();
87-
//
88-
// expect(result).toEqual({
89-
// ...mockAgreements,
90-
// version: undefined,
91-
// data: undefined,
92-
// });
93-
// });
94-
});
89+
it('should update existing config with newest default', async () => {
90+
repository.findOneBy.mockResolvedValueOnce(plainToClass(FeaturesConfigEntity, {
91+
...mockFeaturesConfig,
92+
data: {
93+
...mockFeaturesConfigData,
94+
version: defaultConfig.version - 0.1,
95+
},
96+
}));
97+
98+
const result = await service.getOrCreate();
9599

96-
// describe('update', () => {
97-
// it('should update agreements', async () => {
98-
// const result = await service.update(mockUserId, mockAgreements);
99-
//
100-
// expect(result).toEqual(mockAgreements);
101-
// expect(repository.update).toHaveBeenCalledWith({}, {
102-
// ...mockAgreementsEntity,
103-
// });
104-
// });
105-
// });
100+
expect(result).toEqual(mockFeaturesConfig);
101+
expect(repository.update).toHaveBeenCalledWith(
102+
{ id: service['id'] },
103+
plainToClass(FeaturesConfigEntity, { data: defaultConfig }),
104+
);
105+
});
106+
it('should create new config', async () => {
107+
repository.findOneBy.mockResolvedValueOnce(null);
108+
109+
const result = await service.getOrCreate();
110+
111+
expect(result).toEqual(mockFeaturesConfig);
112+
});
113+
});
106114
});

0 commit comments

Comments
 (0)