Skip to content

Commit

Permalink
adds convertFormat, tests and refactor inputFieldName
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoFrontini committed Jan 24, 2024
1 parent 5ff18ed commit ebf3f61
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/formatter/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const dateStringFromTimestampFormat = (
)
);

const OutputFormat = t.union([
export const OutputFormat = t.union([
t.literal("yyyy-MM-dd"),
t.literal("yyyy-MM-dd HH:mm"),
t.literal("yyyy-MM-dd HH:mm:ss")
Expand Down
2 changes: 0 additions & 2 deletions src/types/__tests__/boolean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BooleanMapping } from "../boolean";
describe("BooleanToStringMapping", () => {
it("should validate with correct values", () => {
const validMapping = {
inputFieldName: "foo",
falseString: "false",
mapper: "BOOLEAN_TO_STRING",
trueString: "true"
Expand All @@ -17,7 +16,6 @@ describe("BooleanToStringMapping", () => {

it("should not validate with incorrect values", () => {
const invalidMapping = {
inputFieldName: "foo",
falseString: 123,
mapper: "BOOLEAN_TO_STRING",
trueString: "true"
Expand Down
2 changes: 0 additions & 2 deletions src/types/__tests__/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ describe("SwitchCaseMapping", () => {
case1: "value1",
case2: "value2"
},
inputFieldName: "foo",
defaultValue: "default"
};
const result = SwitchCaseMapping.decode(validMapping);
expect(E.isRight(result)).toBeTruthy();
});
it("should not decode if cases is not an object", () => {
const invalidMapping = {
inputFieldName: "foo",
cases: "invalidCases",
defaultValue: "default"
};
Expand Down
57 changes: 40 additions & 17 deletions src/types/__tests__/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,60 @@ import * as E from "fp-ts/Either";
import { DateMapping } from "../date";

describe("DateMapping", () => {
it("should decode a correct dateMapping type properly", () => {
it("should decode a correct DATE_TO_ISO type properly", () => {
const validData = {
inputFieldName: "foo",
dateString: "2023-12-19",
mapper: "DATE_TO_ISO"
};
const res = DateMapping.decode(validData);
expect(E.isRight(res)).toBeTruthy();
});
it("should not decode an invalid dateMapping type properly", () => {
const invalidData = {
inputFieldName: "foo",
dateString: "2023-12-19",
mapper: "INVALID MAPPER"
it("should decode a correct DATE_TO_UTC type properly", () => {
const validData = {
mapper: "DATE_TO_UTC"
};
const res = DateMapping.decode(invalidData);
expect(E.isLeft(res)).toBeTruthy();
const res = DateMapping.decode(validData);
expect(E.isRight(res)).toBeTruthy();
});
it("should decode a correct ISO_TO_UTC type properly", () => {
const validData = {
mapper: "ISO_TO_UTC"
};
const res = DateMapping.decode(validData);
expect(E.isRight(res)).toBeTruthy();
});
it("should decode a correct DATE_TO_TIMESTAMP type properly", () => {
const validData = {
mapper: "DATE_TO_TIMESTAMP"
};
const res = DateMapping.decode(validData);
expect(E.isRight(res)).toBeTruthy();
});
it("should decode a correct DATE_FROM_TIMESTAMP type properly", () => {
const validData = {
mapper: "DATE_FROM_TIMESTAMP"
};
const res = DateMapping.decode(validData);
expect(E.isRight(res)).toBeTruthy();
});
it("should decode a correct CONVERT_FORMAT type properly", () => {
const validData = {
mapper: "CONVERT_FORMAT",
output: "yyyy-MM-dd"
};
const res = DateMapping.decode(validData);
expect(E.isRight(res)).toBeTruthy();
});
it("should not decode if dateString is not a string", () => {
it("should not decode an invalid CONVERT_FORMAT type properly", () => {
const invalidData = {
inputFieldName: "foo",
dateString: 2023 - 12 - 19,
mapper: "DATE_TO_ISO"
mapper: "CONVERT_FORMAT",
output: "INVALID FORMAT"
};
const res = DateMapping.decode(invalidData);
expect(E.isLeft(res)).toBeTruthy();
});
it("should not decode if inpuFieldName is missing", () => {
it("should not decode an invalid dateMapping type properly", () => {
const invalidData = {
dateString: "2023-12-19",
mapper: "DATE_TO_ISO"
mapper: "INVALID MAPPER"
};
const res = DateMapping.decode(invalidData);
expect(E.isLeft(res)).toBeTruthy();
Expand Down
11 changes: 0 additions & 11 deletions src/types/__tests__/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe("NumberMapping", () => {
describe("MULTIPLY_NUMBER", () => {
it('should validate when multiplier is a number and mapper is "MULTIPLY_NUMBER"', () => {
const validData = {
inputFieldName: "foo",
multiplier: 5,
mapper: "MULTIPLY_NUMBER"
};
Expand All @@ -14,7 +13,6 @@ describe("NumberMapping", () => {
});
it("should not validate when multiplier is not a number", () => {
const invalidData = {
inputFieldName: "foo",
multiplier: "invalid",
mapper: "MULTIPLY_NUMBER"
};
Expand All @@ -23,7 +21,6 @@ describe("NumberMapping", () => {
});
it("should not validate when mapper is not 'MULTIPLY_NUMBER'", () => {
const invalidData = {
inputFieldName: "foo",
multiplier: 5,
mapper: "INVALID_MAPPER"
};
Expand All @@ -34,7 +31,6 @@ describe("NumberMapping", () => {
describe("DIVIDE_NUMBER", () => {
it('should validate when divider is a number and mapper is "DIVIDE_NUMBER"', () => {
const validData = {
inputFieldName: "foo",
divider: 5,
mapper: "DIVIDE_NUMBER"
};
Expand All @@ -46,7 +42,6 @@ describe("NumberMapping", () => {

it("should not validate when divider is not a number", () => {
const invalidData = {
inputFieldName: "foo",
divider: "invalid",
mapper: "DIVIDE_NUMBER"
};
Expand All @@ -58,7 +53,6 @@ describe("NumberMapping", () => {

it('should not validate when mapper is not "DIVIDE_NUMBER"', () => {
const invalidData = {
inputFieldName: "foo",
divider: 5,
mapper: "INVALID_MAPPER"
};
Expand All @@ -70,7 +64,6 @@ describe("NumberMapping", () => {

it('should not validate when mapper is "DIVIDE_NUMBER" and decimals attribute is evaluated', () => {
const invalidData = {
inputFieldName: "foo",
decimals: 2,
mapper: "DIVIDE_NUMBER"
};
Expand All @@ -84,7 +77,6 @@ describe("NumberMapping", () => {
describe("ROUND_NUMBER", () => {
it('should validate when decimals is a number and mapper is "ROUND_NUMBER"', () => {
const validData = {
inputFieldName: "foo",
decimals: 5,
mapper: "ROUND_NUMBER"
};
Expand All @@ -96,7 +88,6 @@ describe("NumberMapping", () => {

it("should not validate when decimals is not a number", () => {
const invalidData = {
inputFieldName: "foo",
decimals: "5",
mapper: "ROUND_NUMBER"
};
Expand All @@ -108,7 +99,6 @@ describe("NumberMapping", () => {

it('should not validate when mapper is not "ROUND_NUMBER"', () => {
const invalidData = {
inputFieldName: "foo",
decimals: 5,
mapper: "INVALID"
};
Expand All @@ -120,7 +110,6 @@ describe("NumberMapping", () => {

it('should not validate when mapper is "ROUND_NUMBER" and divider attribute is evaluated', () => {
const invalidData = {
inputFieldName: "foo",
divider: 2,
mapper: "ROUND_NUMBER"
};
Expand Down
6 changes: 1 addition & 5 deletions src/types/__tests__/renameField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { RenameFieldMapping, RenameMapping } from "../renameField";
describe("RenameFieldMapping", () => {
it("should decode if config is a valid RenameFieldMapping", () => {
const validData = {
inputFieldName: "foo",
newFieldName: "fooo",
mapper: "RENAME_FIELD"
};
Expand All @@ -13,16 +12,14 @@ describe("RenameFieldMapping", () => {
});
it("should not validate if config is not a valid input", () => {
const invalidData = {
invalidInputField: "foo",
newFieldName: "fooo",
newFieldName: "",
mapper: "RENAME_FIELD"
};
const result = RenameFieldMapping.decode(invalidData);
expect(E.isRight(result)).toBeFalsy();
});
it("should not validate if mapper is not 'RENAME_FIELD'", () => {
const invalidData = {
inputFieldName: "foo",
newFieldName: "fooo",
mapper: "INVALID_MAPPER"
};
Expand All @@ -48,7 +45,6 @@ describe("RenameMapping", () => {

it("should decode if config is a valid RenameMapping instance of RenameField mapper", () => {
const validData = {
inputFieldName: "foo",
newFieldName: "fooo",
mapper: "RENAME_FIELD"
};
Expand Down
34 changes: 33 additions & 1 deletion src/types/__tests__/singleInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const invalidNumberCaseMapping = {
outputFieldName: "bar"
};

const anotherInvalidNumberCaseMapping = {
type: "SINGLE_INPUT",
divider: 10,
mapper: "DIVIDE_NUMBER",
outputFieldName: "bar"
};

const stringCaseMapping = {
type: "SINGLE_INPUT",
inputFieldName: "foo",
Expand Down Expand Up @@ -82,10 +89,23 @@ const invalidRenameFieldMapping = {
const dateMapping = {
type: "SINGLE_INPUT",
inputFieldName: "foo",
dateString: "2023-12-19",
mapper: "DATE_TO_ISO"
};

const convertFormatMapping = {
type: "SINGLE_INPUT",
inputFieldName: "foo",
mapper: "CONVERT_FORMAT",
output: "yyyy-MM-dd"
};

const invalidConvertFormatMapping = {
type: "SINGLE_INPUT",
inputFieldName: "foo",
mapper: "CONVERT_FORMAT",
output: "invalid output string"
};

describe("SingleInputMapping", () => {
it("should decode a correct numberCaseMapping type properly", () => {
const res = SingleInputMapping.decode(numberCaseMapping);
Expand All @@ -111,6 +131,10 @@ describe("SingleInputMapping", () => {
const res = SingleInputMapping.decode(invalidNumberCaseMapping);
expect(E.isLeft(res)).toBeTruthy();
});
it("should not decode another invalid numberCaseMapping properly", () => {
const res = SingleInputMapping.decode(anotherInvalidNumberCaseMapping);
expect(E.isLeft(res)).toBeTruthy();
});
it("should not decode an invalid stringCaseMapping type properly", () => {
const res = SingleInputMapping.decode(invalidStringCaseMapping);
expect(E.isLeft(res)).toBeTruthy();
Expand All @@ -131,4 +155,12 @@ describe("SingleInputMapping", () => {
const res = SingleInputMapping.decode(dateMapping);
expect(E.isRight(res)).toBeTruthy();
});
it("should decode a correct convertFormatMapping type properly", () => {
const res = SingleInputMapping.decode(convertFormatMapping);
expect(E.isRight(res)).toBeTruthy();
});
it("should not decode an invalid convertFormatMapping type properly", () => {
const res = SingleInputMapping.decode(invalidConvertFormatMapping);
expect(E.isLeft(res)).toBeTruthy();
});
});
7 changes: 0 additions & 7 deletions src/types/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,34 @@ import * as E from "fp-ts/Either";
import { StringMapping } from "../string";

const upperCaseMapping = {
inputFieldName: "foo",
mapper: "UPPER_CASE"
};

const lowerCaseMapping = {
inputFieldName: "foo",
mapper: "LOWER_CASE"
};

const capitalizeMapping = {
inputFieldName: "foo",
mapper: "CAPITALIZE"
};

const trimMapping = {
inputFieldName: "foo",
mapper: "TRIM"
};

const replaceMapping = {
inputFieldName: "foo",
mapper: "REPLACE",
placeholder: "a",
toBeReplaced: "b"
};

const replaceAllMapping = {
inputFieldName: "foo",
mapper: "REPLACE",
placeholder: "a",
toBeReplaced: "b"
};

const invalidMapping = {
inputFieldName: "foo",
mapper: "INVALID"
};

Expand Down
2 changes: 0 additions & 2 deletions src/types/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import * as t from "io-ts";

const BooleanToStringMapping = t.type({
falseString: t.string,
inputFieldName: NonEmptyString,
mapper: t.literal("BOOLEAN_TO_STRING"),
trueString: t.string
});
Expand Down
4 changes: 1 addition & 3 deletions src/types/case.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import * as t from "io-ts";

export const SwitchCaseMapping = t.type({
cases: t.record(t.string, t.unknown),
defaultValue: t.unknown,
inputFieldName: NonEmptyString
defaultValue: t.unknown
});

export type SwitchCaseMapping = t.TypeOf<typeof SwitchCaseMapping>;
Loading

0 comments on commit ebf3f61

Please sign in to comment.