Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#IP-324] Change fp-ts version to 2.x and removed old italia-commons uses #154

Merged
merged 8 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions CreateMessage/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {

import { none, some } from "fp-ts/lib/Option";

import { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";

import {
canDefaultAddresses,
canPaymentAmount,
Expand Down Expand Up @@ -41,7 +44,7 @@ import {
anIncompleteService,
anotherFiscalCode
} from "../../__mocks__/mocks";
import { initAppInsights } from "italia-ts-commons/lib/appinsights";
import { initAppInsights } from "@pagopa/ts-commons/lib/appinsights";
import { mockOrchestratorContext } from "../../__mocks__/durable-functions";
import { ApiNewMessageWithDefaults } from "../types";

Expand All @@ -64,9 +67,9 @@ describe("canWriteMessage", () => {
), // any authorized recipient, possibly also the current one
recipient // one random recipient
);
expect(response.isLeft()).toBeTruthy();
if (response.isLeft()) {
expect(response.value.kind).toEqual(
expect(E.isLeft(response)).toBeTruthy();
if (E.isLeft(response)) {
expect(response.left.kind).toEqual(
"IResponseErrorForbiddenNotAuthorizedForProduction"
);
}
Expand All @@ -86,9 +89,9 @@ describe("canWriteMessage", () => {
new Set(authorizedRecipients.filter(_ => _ !== recipient)), // current recipient is not authorized
recipient
);
expect(response.isLeft()).toBeTruthy();
if (response.isLeft()) {
expect(response.value.kind).toEqual(
expect(E.isLeft(response)).toBeTruthy();
if (E.isLeft(response)) {
expect(response.left.kind).toEqual(
"IResponseErrorForbiddenNotAuthorizedForRecipient"
);
}
Expand All @@ -108,7 +111,7 @@ describe("canWriteMessage", () => {
new Set([...authorizedRecipients, recipient]), // current recipient always authorized
recipient
);
expect(response.isRight()).toBeTruthy();
expect(E.isRight(response)).toBeTruthy();
}
)
);
Expand All @@ -125,7 +128,7 @@ describe("canWriteMessage", () => {
authorizedRecipients,
recipient
);
expect(response.isRight()).toBeTruthy();
expect(E.isRight(response)).toBeTruthy();
}
)
);
Expand All @@ -137,7 +140,7 @@ describe("canDefaultAddresses", () => {
fc.assert(
fc.property(newMessageWithDefaultEmailArb, m => {
const response = canDefaultAddresses(m);
expect(response.isLeft()).toBeTruthy();
expect(E.isLeft(response)).toBeTruthy();
})
);
});
Expand All @@ -150,11 +153,13 @@ describe("canPaymentAmount", () => {
newMessageWithPaymentDataArb,
maxAmountArb,
(message, maxAmount) => {
const p = message.content.payment_data;

const response = canPaymentAmount(message.content, maxAmount);
if (message.content.payment_data.amount <= maxAmount) {
expect(response.isRight()).toBeTruthy();
expect(E.isRight(response)).toBeTruthy();
} else {
expect(response.isLeft()).toBeTruthy();
expect(E.isLeft(response)).toBeTruthy();
}
}
)
Expand Down Expand Up @@ -188,11 +193,11 @@ describe("createMessageDocument", () => {
senderServiceId
);

const response = await responseTask.run();
const response = await responseTask();

expect(mockMessageModel.create).toHaveBeenCalledTimes(1);
expect(response.isRight()).toBeTruthy();
expect(response.getOrElse(undefined)).toMatchObject({
expect(E.isRight(response)).toBeTruthy();
expect(pipe(response, E.getOrElse(undefined))).toMatchObject({
fiscalCode,
id: messageId,
indexedId: messageId,
Expand Down Expand Up @@ -233,8 +238,8 @@ describe("forkOrchestrator", () => {
service,
newMessageWithoutContent,
serviceUserEmail
).run();
expect(response.isRight()).toBeTruthy();
)();
expect(E.isRight(response)).toBeTruthy();
expect(getDfClient).toHaveBeenCalledTimes(1);
expect(mockDfClient.startNew).toHaveBeenCalledTimes(1);
expect(mockDfClient.startNew).toHaveBeenCalledWith(
Expand All @@ -255,7 +260,9 @@ describe("forkOrchestrator", () => {
serviceVersion: service.version
})
);
expect(response.getOrElse(undefined)).toEqual("orchestratorId");
expect(pipe(response, E.getOrElse(undefined))).toEqual(
"orchestratorId"
);
}
)
);
Expand Down
78 changes: 49 additions & 29 deletions CreateMessage/__tests__/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { WithinRangeString } from "@pagopa/ts-commons/lib/strings";
import * as t from "io-ts";
import { toString } from "fp-ts/lib/function";
import { readableReport } from "italia-ts-commons/lib/reporters";
import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import { ApiNewMessageWithContentOf } from "../types";
import { PaymentData } from "@pagopa/io-functions-commons/dist/generated/definitions/PaymentData";
import { json } from "express";

import { pipe } from "fp-ts/lib/function";
import * as E from "fp-ts/lib/Either";

const toString = (x: any) => JSON.stringify(x);

const aMessageContent = { subject: "a".repeat(10), markdown: "a".repeat(80) };

Expand All @@ -23,19 +28,25 @@ describe("ApiNewMessageWithContentOf", () => {
const codec = ApiNewMessageWithContentOf(pattern);

// positive scenario: we expect a match
codec.decode(aMessageWithSuchSubject).fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.subject).toBe(aSubject);
}
pipe(
codec.decode(aMessageWithSuchSubject),
E.fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.subject).toBe(aSubject);
}
)
);

// negative scenario: we expect a no-match
codec.decode(aMessageWithDifferentSubject).fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
pipe(
codec.decode(aMessageWithDifferentSubject),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
});

Expand Down Expand Up @@ -67,27 +78,36 @@ describe("ApiNewMessageWithContentOf", () => {
const codec = ApiNewMessageWithContentOf(pattern);

// positive scenario: we expect a match
codec.decode(aMessageWithSuchPaymentData).fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.payment_data).toEqual(
expect.objectContaining(aPaymentData)
);
}
pipe(
codec.decode(aMessageWithSuchPaymentData),
E.fold(
e => fail(`Should have decoded the value: ${readableReport(e)}`),
e => {
expect(e.content.payment_data).toEqual(
expect.objectContaining(aPaymentData)
);
}
)
);

// negative scenario: we expect a no-match
codec.decode(aMessageWithNoPaymentData).fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
pipe(
codec.decode(aMessageWithNoPaymentData),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
codec.decode(aMessageWithAnotherPaymentData).fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
pipe(
codec.decode(aMessageWithAnotherPaymentData),
E.fold(
_ => {
expect(true).toBe(true);
},
e => fail(`Should have not decoded the value: ${toString(e)}`)
)
);
});
});
Loading