Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AleDore committed Mar 8, 2024
1 parent 5bb9dee commit bdd3bad
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 49 deletions.
10 changes: 5 additions & 5 deletions src/enrichment/__tests__/blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const containerClientMock = {} as any;
describe("blobEnrich", () => {
it("should raise an error if blobName Field is not strings", async () => {
await pipe(
blobEnrich(input, containerClientMock, "bar"),
blobEnrich(containerClientMock, "bar")(input),
TE.bimap(
err => {
expect(err).toBeDefined();
Expand All @@ -35,7 +35,7 @@ describe("blobEnrich", () => {
TE.left(Error("Cannot read Blob"))
);
await pipe(
blobEnrich(input, containerClientMock, "foo"),
blobEnrich(containerClientMock, "foo")(input),
TE.bimap(
err => {
expect(err).toBeDefined();
Expand All @@ -51,7 +51,7 @@ describe("blobEnrich", () => {
it("should return unmodified input if Blob Document is missing", async () => {
getBlobDocumentMock.mockImplementationOnce(() => TE.right(O.none));
await pipe(
blobEnrich(input, containerClientMock, "foo"),
blobEnrich(containerClientMock, "foo")(input),
TE.bimap(
() => fail("it should not fail"),
result => expect(result).toEqual(input)
Expand All @@ -64,7 +64,7 @@ describe("blobEnrich", () => {
TE.right(O.some({ baz: "baz" }))
);
await pipe(
blobEnrich(input, containerClientMock, "blobName", "enrichedFieldName"),
blobEnrich(containerClientMock, "blobName", "enrichedFieldName")(input),
TE.bimap(
() => fail("it should not fail"),
result =>
Expand All @@ -81,7 +81,7 @@ describe("blobEnrich", () => {
TE.right(O.some({ baz: "baz" }))
);
await pipe(
blobEnrich(input, containerClientMock, "blobName"),
blobEnrich(containerClientMock, "blobName")(input),
TE.bimap(
() => fail("it should not fail"),
result =>
Expand Down
13 changes: 6 additions & 7 deletions src/enrichment/__tests__/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const tableClientMock = {} as any;
describe("tableEnrich", () => {
it("should raise an error if input Key Fields are not strings", async () => {
await pipe(
tableEnrich(input, tableClientMock, "foo", "bar"),
tableEnrich(tableClientMock, "foo", "bar")(input),
TE.bimap(
err => {
expect(err).toBeDefined();
Expand All @@ -37,7 +37,7 @@ describe("tableEnrich", () => {
TE.left(Error("Table unreachable"))
);
await pipe(
tableEnrich(input, tableClientMock, "partitionKey", "partitionKey"),
tableEnrich(tableClientMock, "partitionKey", "partitionKey")(input),
TE.bimap(
err => {
expect(err).toBeDefined();
Expand All @@ -51,7 +51,7 @@ describe("tableEnrich", () => {
it("should return unmodified input if table document is missing", async () => {
getTableDocumentMock.mockImplementationOnce(() => TE.right(O.none));
await pipe(
tableEnrich(input, tableClientMock, "partitionKey", "rowKey"),
tableEnrich(tableClientMock, "partitionKey", "rowKey")(input),
TE.bimap(
() => fail("it should not fail"),
result => expect(result).toEqual(input)
Expand All @@ -64,7 +64,7 @@ describe("tableEnrich", () => {
TE.right(O.some({ baz: "baz" }))
);
await pipe(
tableEnrich(input, tableClientMock, "partitionKey", "rowKey"),
tableEnrich(tableClientMock, "partitionKey", "rowKey")(input),
TE.bimap(
() => fail("it should not fail"),
result => expect(result).toEqual({ ...input, baz: "baz" })
Expand All @@ -78,12 +78,11 @@ describe("tableEnrich", () => {
);
await pipe(
tableEnrich(
input,
tableClientMock,
"partitionKey",
"rowKey",
"enrichedField"
),
)(input),
TE.bimap(
() => fail("it should not fail"),
result =>
Expand All @@ -97,7 +96,7 @@ describe("tableEnrich", () => {
TE.right(O.some({ baz: "baz" }))
);
await pipe(
tableEnrich(input, tableClientMock, "partitionKey", "rowKey"),
tableEnrich(tableClientMock, "partitionKey", "rowKey")(input),
TE.bimap(
() => fail("it should not fail"),
result => expect(result).toEqual({ ...input, baz: "baz" })
Expand Down
24 changes: 11 additions & 13 deletions src/enrichment/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import { errorsToReadableMessages } from "@pagopa/ts-commons/lib/reporters";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { flattenField } from "../formatter/flatten";
import { getBlobDocument } from "../utils/blobStorage";
import { NotInKeys } from "../utils/types";
import { toJsonObject } from "../utils/data";

export const blobEnrich = <T, K extends string>(
input: T,
export const blobEnrich = <T extends Record<string, unknown>>(
blobContainerClient: BS.ContainerClient,
blobNameField: keyof T,
outputFieldName?: NotInKeys<T, K>
): TE.TaskEither<Error, T> =>
outputFieldName?: string
) => (input: T): TE.TaskEither<Error, T> =>
pipe(
input[blobNameField],
NonEmptyString.decode,
Expand All @@ -36,20 +34,20 @@ export const blobEnrich = <T, K extends string>(
pipe(
outputFieldName,
O.fromNullable,
O.map(fieldName => ({ ...input, [fieldName]: blobDocumentObject })),
O.map(fieldName =>
E.right({ ...input, [fieldName]: blobDocumentObject })
),
O.getOrElseW(() =>
pipe(`${String(blobNameField)}_enrich`, flattenFieldName =>
flattenField(
{
...input,
[flattenFieldName]: blobDocumentObject
},
flattenFieldName
)
flattenField(flattenFieldName)({
...input,
[flattenFieldName]: blobDocumentObject
})
)
)
)
),
O.chain(O.fromEither),
O.getOrElse(() => input)
)
)
Expand Down
24 changes: 11 additions & 13 deletions src/enrichment/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { errorsToReadableMessages } from "@pagopa/ts-commons/lib/reporters";
import { flattenField } from "../formatter/flatten";
import { getTableDocument } from "../utils/tableStorage";
import { NotInKeys } from "../utils/types";

export const InputKeyFields = t.type({
partitionKey: NonEmptyString,
Expand All @@ -17,13 +16,12 @@ export const InputKeyFields = t.type({

export type InputKeyFields = t.TypeOf<typeof InputKeyFields>;

export const tableEnrich = <T, K extends string>(
input: T,
export const tableEnrich = <T extends Record<string, unknown>>(
tableClient: DT.TableClient,
partitionKeyField: keyof T,
rowKeyField: keyof T,
outputFieldName?: NotInKeys<T, K>
): TE.TaskEither<Error, T> =>
outputFieldName?: string
) => (input: T): TE.TaskEither<Error, T> =>
pipe(
{ partitionKey: input[partitionKeyField], rowKey: input[rowKeyField] },
InputKeyFields.decode,
Expand All @@ -44,22 +42,22 @@ export const tableEnrich = <T, K extends string>(
pipe(
outputFieldName,
O.fromNullable,
O.map(fieldName => ({ ...input, [fieldName]: tableDocument })),
O.map(fieldName =>
E.right({ ...input, [fieldName]: tableDocument })
),
O.getOrElse(() =>
pipe(
`${String(partitionKeyField)}_${String(rowKeyField)}_enrich`,
flattenFieldName =>
flattenField(
{
...input,
[flattenFieldName]: tableDocument
},
flattenFieldName
)
flattenField(flattenFieldName)({
...input,
[flattenFieldName]: tableDocument
})
)
)
)
),
O.chain(O.fromEither),
O.getOrElse(() => input)
)
)
Expand Down
13 changes: 8 additions & 5 deletions src/formatter/__test__/apply.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as E from "fp-ts/Either";
import { applySingleInput } from "../apply";
import { upperCaseFormat } from "../string";

Expand All @@ -8,16 +9,18 @@ const dataFlow = {
describe("applySingleInput", () => {
it("should apply a formatter function without adding new field", () => {
const formattedFlow = applySingleInput("foo")(upperCaseFormat)(dataFlow);
expect(formattedFlow).toEqual({ foo: upperCaseFormat(dataFlow.foo) });
expect(formattedFlow).toEqual(E.right({ foo: dataFlow.foo.toUpperCase() }));
});

it("should apply a formatter function with adding new field", () => {
const formattedFlow = applySingleInput("foo", "bar")(upperCaseFormat)(
dataFlow
);
expect(formattedFlow).toEqual({
bar: upperCaseFormat(dataFlow.foo),
foo: dataFlow.foo
});
expect(formattedFlow).toEqual(
E.right({
bar: dataFlow.foo.toUpperCase(),
foo: dataFlow.foo
})
);
});
});
7 changes: 4 additions & 3 deletions src/formatter/__test__/selectFields.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as E from "fp-ts/Either";
import { selectFields } from "../selectFields";

const inputData = {
Expand All @@ -9,14 +10,14 @@ const inputData = {
describe("selectFields", () => {
it("should select fields from an object", () => {
const res = selectFields(["foo", "baz"])(inputData);
expect(res).toEqual({ baz: "hello", foo: "Foo" });
expect(res).toEqual(E.right({ baz: "hello", foo: "Foo" }));
});
it("should return an empty object if no fields are provided", () => {
const res = selectFields([])(inputData);
expect(res).toEqual({});
expect(res).toEqual(E.right({}));
});
it("should return an empty object if a missing field is provided", () => {
const res = selectFields(["fooooo" as keyof typeof inputData])(inputData);
expect(res).toEqual({});
expect(res).toEqual(E.right({}));
});
});
2 changes: 1 addition & 1 deletion src/formatter/__test__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("CapitalizeFormat", () => {
it("should capitalize a string", () => {
const res = capitalizeFormat(aString);
expect(res).toEqual(
E.right(`${aString[0].toUpperCase() + aString.slice(1)}`)
E.right(`${aString.charAt(0).toUpperCase()}${aString.slice(1)}`)
);
});

Expand Down
4 changes: 2 additions & 2 deletions src/formatter/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const capitalizeFormat = (s: string): E.Either<Error, string> =>
pipe(
strArr,
AR.head,
O.map(upperCaseFormat),
O.map(str => str.toUpperCase()),
O.map(firstLetter => `${firstLetter}${strArr.join("").slice(1)}`),
O.getOrElse(() => ""),
O.getOrElseW(() => ""),
E.right
)
)
Expand Down

0 comments on commit bdd3bad

Please sign in to comment.