cast values on set
This commit is contained in:
parent
d20da85896
commit
d6d0b4ed11
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
);
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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>(
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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>(
|
||||
ConfigurationsManagerService,
|
||||
);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(configurationsManagerService).toBeDefined();
|
||||
});
|
||||
});
|
|
@ -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();
|
||||
// });
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue