feat: add test cases (#1463)
This commit is contained in:
parent
9535ec57d0
commit
a0e85cda22
14
packages/core/database/src/__tests__/percent2float.test.ts
Normal file
14
packages/core/database/src/__tests__/percent2float.test.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { percent2float } from '../utils';
|
||||
|
||||
describe('percent2float', () => {
|
||||
it('should be NaN', () => {
|
||||
expect(percent2float('123')).toBe(NaN);
|
||||
expect(percent2float('3a')).toBe(NaN);
|
||||
expect(percent2float('3a%')).toBe(NaN);
|
||||
});
|
||||
|
||||
it('should be a floating point number', () => {
|
||||
expect(percent2float('123%')).toBe(1.23);
|
||||
expect(percent2float('22.5507%')).toBe(0.225507); // not 0.22550699999999999
|
||||
});
|
||||
});
|
@ -0,0 +1,46 @@
|
||||
import { NumberValueParser } from '../../value-parsers';
|
||||
|
||||
describe('number value parser', () => {
|
||||
let parser: NumberValueParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new NumberValueParser({}, {});
|
||||
});
|
||||
|
||||
const expectValue = (value) => {
|
||||
parser = new NumberValueParser({}, {});
|
||||
parser.setValue(value);
|
||||
return expect(parser.getValue());
|
||||
};
|
||||
|
||||
it('should be number', () => {
|
||||
expectValue(123).toBe(123);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
expectValue('123').toBe(123);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
expectValue('123%').toBe(1.23);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
expectValue('22.5507%').toBe(0.225507);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should be null', () => {
|
||||
expectValue('').toBe(null);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
expectValue('n/a').toBe(null);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
expectValue('-').toBe(null);
|
||||
expect(parser.errors.length === 0).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should be errors', () => {
|
||||
expectValue({}).toBe(null);
|
||||
expect(parser.errors.length > 0).toBeTruthy();
|
||||
expectValue('123a').toBe(null);
|
||||
expect(parser.errors.length > 0).toBeTruthy();
|
||||
expectValue('123a%').toBe(null);
|
||||
expect(parser.errors.length > 0).toBeTruthy();
|
||||
expectValue('aaa').toBe(null);
|
||||
expect(parser.errors.length > 0).toBeTruthy();
|
||||
});
|
||||
});
|
@ -117,6 +117,13 @@ export function patchSequelizeQueryInterface(db: Database) {
|
||||
}
|
||||
|
||||
export function percent2float(value: string) {
|
||||
if (!value.endsWith('%')) {
|
||||
return NaN;
|
||||
}
|
||||
let val = value.substring(0, value.length - 1);
|
||||
if (isNaN(+val)) {
|
||||
return NaN;
|
||||
}
|
||||
const index = value.indexOf('.');
|
||||
if (index === -1) {
|
||||
return parseFloat(value) / 100;
|
||||
|
@ -7,6 +7,7 @@ export class BaseValueParser {
|
||||
constructor(field: any, ctx: any) {
|
||||
this.field = field;
|
||||
this.ctx = ctx;
|
||||
this.value = null;
|
||||
}
|
||||
|
||||
toString() {
|
||||
|
@ -5,19 +5,25 @@ export class NumberValueParser extends BaseValueParser {
|
||||
async setValue(value: any) {
|
||||
if (value === null || value === undefined || typeof value === 'number') {
|
||||
this.value = value;
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
} else if (typeof value === 'string') {
|
||||
if (!value) {
|
||||
this.value = null;
|
||||
} else if (['n/a', '-'].includes(value.toLowerCase())) {
|
||||
this.value = null;
|
||||
} else if (value.endsWith('%')) {
|
||||
this.value = percent2float(value);
|
||||
console.log(value, this.value);
|
||||
} else {
|
||||
const val = +value;
|
||||
this.value = isNaN(val) ? null : val;
|
||||
}
|
||||
if (value.endsWith('%')) {
|
||||
value = percent2float(value);
|
||||
} else {
|
||||
value = +value;
|
||||
}
|
||||
if (isNaN(value)) {
|
||||
this.errors.push(`Value invalid - "${value}"`);
|
||||
} else {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.errors.push(`Value invalid - ${JSON.stringify(value)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user