diff --git a/src/modules/configuration/core/application/services/configurations-manager.service.ts b/src/modules/configuration/core/application/services/configurations-manager.service.ts index 5dd7990..01d9b6b 100644 --- a/src/modules/configuration/core/application/services/configurations-manager.service.ts +++ b/src/modules/configuration/core/application/services/configurations-manager.service.ts @@ -87,7 +87,6 @@ export class ConfigurationsManagerService { for (key in configuration) { if (key !== 'domain' && key === property) return configuration[key]; } - throw new NotFoundException('Configuration item not found'); }; private _isInt = (number: number): boolean => number % 1 === 0; diff --git a/src/modules/configuration/interface/grpc-controllers/dtos/is-configuration-type.decorator.ts b/src/modules/configuration/interface/grpc-controllers/dtos/is-configuration-type.decorator.ts deleted file mode 100644 index 71e9b1c..0000000 --- a/src/modules/configuration/interface/grpc-controllers/dtos/is-configuration-type.decorator.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator'; - -export const IsConfigurationType = ( - validationOptions?: ValidationOptions, -): PropertyDecorator => - ValidateBy( - { - name: '', - constraints: [], - validator: { - validate: (value: any): boolean => - typeof value === 'number' || - typeof value === 'boolean' || - typeof value === 'string', - defaultMessage: buildMessage( - () => `Value must be a number, boolean or string`, - validationOptions, - ), - }, - }, - validationOptions, - ); diff --git a/src/modules/configuration/interface/grpc-controllers/dtos/set-configuration.request.dto.ts b/src/modules/configuration/interface/grpc-controllers/dtos/set-configuration.request.dto.ts index 12285b7..2c494ad 100644 --- a/src/modules/configuration/interface/grpc-controllers/dtos/set-configuration.request.dto.ts +++ b/src/modules/configuration/interface/grpc-controllers/dtos/set-configuration.request.dto.ts @@ -1,6 +1,5 @@ import { ConfigurationDomain } from '@mobicoop/configuration-module'; import { IsEnum, IsNotEmpty, IsString } from 'class-validator'; -import { IsConfigurationType } from './is-configuration-type.decorator'; export class SetConfigurationRequestDto { @IsEnum(ConfigurationDomain) @@ -11,7 +10,6 @@ export class SetConfigurationRequestDto { @IsNotEmpty() key: string; - @IsConfigurationType() @IsNotEmpty() value: string | boolean | number; } diff --git a/src/modules/configuration/tests/unit/core/configurations-manager.service.spec.ts b/src/modules/configuration/tests/unit/core/configurations-manager.service.spec.ts new file mode 100644 index 0000000..f5c4cbe --- /dev/null +++ b/src/modules/configuration/tests/unit/core/configurations-manager.service.spec.ts @@ -0,0 +1,125 @@ +import { + ConfigurationDomain, + ConfigurationIdentifier, + ConfigurationType, +} from '@mobicoop/configuration-module'; +import { NotFoundException } from '@mobicoop/ddd-library'; +import { ConfigurationsManagerService } from '@modules/configuration/core/application/services/configurations-manager.service'; +import { ConfigService } from '@nestjs/config'; +import { Test, TestingModule } from '@nestjs/testing'; +import { Config } from '@src/config/config'; + +const mockConfigService = { + get: jest.fn().mockImplementation((domain: string) => { + switch (domain) { + case 'carpool': + return { + departureTimeMargin: 900, + role: 'passenger', + seatsProposed: 3, + seatsRequested: 1, + strictFrequency: false, + }; + case 'pagination': + return { + perPage: 10, + }; + } + }), +}; + +describe('Configurations Manager Service', () => { + let configurationsManagerService: ConfigurationsManagerService; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: ConfigService, + useValue: mockConfigService, + }, + ConfigurationsManagerService, + ], + }).compile(); + + configurationsManagerService = module.get( + ConfigurationsManagerService, + ); + }); + + it('should be defined', () => { + expect(configurationsManagerService).toBeDefined(); + }); + + it('should return the list of configuration elements', () => { + const list: Config[] = configurationsManagerService.list(); + expect(list).toHaveLength(2); + }); + + describe('identifierType', () => { + it('should return the type of a configuration item for a given identifier', () => { + const configurationIdentifier: ConfigurationIdentifier = { + domain: ConfigurationDomain.CARPOOL, + key: 'seatsProposed', + }; + const configurationType: ConfigurationType = + configurationsManagerService.identifierType(configurationIdentifier); + expect(configurationType).toBe(ConfigurationType.INT); + }); + it('should throw if configuration item is not found', () => { + const configurationIdentifier: ConfigurationIdentifier = { + domain: ConfigurationDomain.CARPOOL, + key: 'minAge', + }; + expect(() => { + configurationsManagerService.identifierType(configurationIdentifier); + }).toThrow(NotFoundException); + }); + }); + + describe('configurationType', () => { + it('should return the configuration type of an int', () => { + expect(configurationsManagerService.configurationType(3)).toBe( + ConfigurationType.INT, + ); + }); + it('should return the configuration type of a float', () => { + expect(configurationsManagerService.configurationType(3.5)).toBe( + ConfigurationType.FLOAT, + ); + }); + it('should return the configuration type of a boolean', () => { + expect(configurationsManagerService.configurationType(true)).toBe( + ConfigurationType.BOOLEAN, + ); + }); + it('should return the configuration type of a string', () => { + expect(configurationsManagerService.configurationType('role')).toBe( + ConfigurationType.STRING, + ); + }); + }); + + describe('cast', () => { + it('should cast a string to int', () => { + expect( + configurationsManagerService.cast('1', ConfigurationType.INT), + ).toBe(1); + }); + it('should cast a string to float', () => { + expect( + configurationsManagerService.cast('1.5', ConfigurationType.FLOAT), + ).toBe(1.5); + }); + it('should cast a string to boolean', () => { + expect( + configurationsManagerService.cast('true', ConfigurationType.BOOLEAN), + ).toBeTruthy(); + }); + it('should not cast a string and return it as is', () => { + expect( + configurationsManagerService.cast('role', ConfigurationType.STRING), + ).toBe('role'); + }); + }); +}); diff --git a/src/modules/configuration/tests/unit/core/configurations-manager.spec.ts b/src/modules/configuration/tests/unit/core/configurations-manager.spec.ts deleted file mode 100644 index c5974dd..0000000 --- a/src/modules/configuration/tests/unit/core/configurations-manager.spec.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { ConfigurationsManagerService } from '@modules/configuration/core/application/services/configurations-manager.service'; -import { ConfigService } from '@nestjs/config'; -import { Test, TestingModule } from '@nestjs/testing'; - -const mockConfigService = { - get: jest.fn().mockImplementation((domain: string) => { - switch (domain) { - case 'carpool': - return { - departureTimeMargin: 900, - role: 'passenger', - seatsProposed: 3, - seatsRequested: 1, - strictFrequency: false, - }; - case 'pagination': - return { - perPage: 10, - }; - } - }), -}; - -describe('Configurations Manager Service', () => { - let configurationsManagerService: ConfigurationsManagerService; - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - { - provide: ConfigService, - useValue: mockConfigService, - }, - ConfigurationsManagerService, - ], - }).compile(); - - configurationsManagerService = module.get( - ConfigurationsManagerService, - ); - }); - - it('should be defined', () => { - expect(configurationsManagerService).toBeDefined(); - }); -}); diff --git a/src/modules/configuration/tests/unit/core/populate.service.spec.ts b/src/modules/configuration/tests/unit/core/populate.service.spec.ts index c31ab4d..63d4cc7 100644 --- a/src/modules/configuration/tests/unit/core/populate.service.spec.ts +++ b/src/modules/configuration/tests/unit/core/populate.service.spec.ts @@ -59,11 +59,4 @@ describe('Populate Service', () => { it('should be defined', () => { expect(populateService).toBeDefined(); }); - - // it('should populate database with default values', () => { - // jest.spyOn(mockConfigurationRepository, 'get'); - // jest.spyOn(mockConfigurationRepository, 'set'); - // expect(mockConfigurationRepository.get).toHaveBeenCalled(); - // expect(mockConfigurationRepository.set).toHaveBeenCalled(); - // }); });