From bdd3bad9d099b8d598a7750b77a4b15647378ec6 Mon Sep 17 00:00:00 2001 From: AleDore Date: Fri, 8 Mar 2024 17:42:11 +0100 Subject: [PATCH] fix tests --- src/enrichment/__tests__/blob.test.ts | 10 ++++----- src/enrichment/__tests__/table.test.ts | 13 ++++++----- src/enrichment/blob.ts | 24 ++++++++++----------- src/enrichment/table.ts | 24 ++++++++++----------- src/formatter/__test__/apply.test.ts | 13 ++++++----- src/formatter/__test__/selectFields.test.ts | 7 +++--- src/formatter/__test__/string.test.ts | 2 +- src/formatter/string.ts | 4 ++-- 8 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/enrichment/__tests__/blob.test.ts b/src/enrichment/__tests__/blob.test.ts index dbbc629..babe9f5 100644 --- a/src/enrichment/__tests__/blob.test.ts +++ b/src/enrichment/__tests__/blob.test.ts @@ -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(); @@ -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(); @@ -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) @@ -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 => @@ -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 => diff --git a/src/enrichment/__tests__/table.test.ts b/src/enrichment/__tests__/table.test.ts index 0f15b44..fa31059 100644 --- a/src/enrichment/__tests__/table.test.ts +++ b/src/enrichment/__tests__/table.test.ts @@ -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(); @@ -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(); @@ -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) @@ -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" }) @@ -78,12 +78,11 @@ describe("tableEnrich", () => { ); await pipe( tableEnrich( - input, tableClientMock, "partitionKey", "rowKey", "enrichedField" - ), + )(input), TE.bimap( () => fail("it should not fail"), result => @@ -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" }) diff --git a/src/enrichment/blob.ts b/src/enrichment/blob.ts index dd34372..0b607b5 100644 --- a/src/enrichment/blob.ts +++ b/src/enrichment/blob.ts @@ -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 = ( - input: T, +export const blobEnrich = >( blobContainerClient: BS.ContainerClient, blobNameField: keyof T, - outputFieldName?: NotInKeys -): TE.TaskEither => + outputFieldName?: string +) => (input: T): TE.TaskEither => pipe( input[blobNameField], NonEmptyString.decode, @@ -36,20 +34,20 @@ export const blobEnrich = ( 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) ) ) diff --git a/src/enrichment/table.ts b/src/enrichment/table.ts index d8e6763..7ccc968 100644 --- a/src/enrichment/table.ts +++ b/src/enrichment/table.ts @@ -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, @@ -17,13 +16,12 @@ export const InputKeyFields = t.type({ export type InputKeyFields = t.TypeOf; -export const tableEnrich = ( - input: T, +export const tableEnrich = >( tableClient: DT.TableClient, partitionKeyField: keyof T, rowKeyField: keyof T, - outputFieldName?: NotInKeys -): TE.TaskEither => + outputFieldName?: string +) => (input: T): TE.TaskEither => pipe( { partitionKey: input[partitionKeyField], rowKey: input[rowKeyField] }, InputKeyFields.decode, @@ -44,22 +42,22 @@ export const tableEnrich = ( 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) ) ) diff --git a/src/formatter/__test__/apply.test.ts b/src/formatter/__test__/apply.test.ts index 00f9aee..f651121 100644 --- a/src/formatter/__test__/apply.test.ts +++ b/src/formatter/__test__/apply.test.ts @@ -1,3 +1,4 @@ +import * as E from "fp-ts/Either"; import { applySingleInput } from "../apply"; import { upperCaseFormat } from "../string"; @@ -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 + }) + ); }); }); diff --git a/src/formatter/__test__/selectFields.test.ts b/src/formatter/__test__/selectFields.test.ts index 4461ba2..82e9bef 100644 --- a/src/formatter/__test__/selectFields.test.ts +++ b/src/formatter/__test__/selectFields.test.ts @@ -1,3 +1,4 @@ +import * as E from "fp-ts/Either"; import { selectFields } from "../selectFields"; const inputData = { @@ -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({})); }); }); diff --git a/src/formatter/__test__/string.test.ts b/src/formatter/__test__/string.test.ts index 389b7f4..eb6cc11 100644 --- a/src/formatter/__test__/string.test.ts +++ b/src/formatter/__test__/string.test.ts @@ -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)}`) ); }); diff --git a/src/formatter/string.ts b/src/formatter/string.ts index 663213d..93fcf2e 100644 --- a/src/formatter/string.ts +++ b/src/formatter/string.ts @@ -18,9 +18,9 @@ export const capitalizeFormat = (s: string): E.Either => 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 ) )