Skip to content

Commit

Permalink
Merge pull request #27 from pagopa/IOPLT-337-creating-system-tests-fo…
Browse files Browse the repository at this point in the history
…r-query-cosmos-enrichment

[IOPLT-337] Creating system tests for query cosmos enrichment
  • Loading branch information
drmarro authored Feb 2, 2024
2 parents 748bec4 + be8e469 commit 27c40b9
Show file tree
Hide file tree
Showing 9 changed files with 2,785 additions and 31 deletions.
41 changes: 22 additions & 19 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
module.exports = {
"env": {
"es6": true,
"node": true
env: {
es6: true,
node: true,
jasmine: true,
jest: true
},
"ignorePatterns": [
"node_modules",
"generated",
"**/__tests__/*",
"**/__mocks__/*",
"*.d.ts",
"*.js",
"Dangerfile.ts"
ignorePatterns: [
"node_modules",
"generated",
"**/__tests__/*",
"**/__mocks__/*",
"*.d.ts",
"*.js",
"Dangerfile.ts",
"**/__integrations__/utils",
"**/__integrations__/env.ts",
"index.ts"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json",
"sourceType": "module"
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
sourceType: "module"
},
"extends": [
"@pagopa/eslint-config/strong",
],
"rules": {}
extends: ["@pagopa/eslint-config/strong"],
rules: {}
};
16 changes: 15 additions & 1 deletion .github/workflows/code-review.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
timeout-minutes: 10 # temp fix
runs-on: ubuntu-latest

environment: IntegrationTests

steps:
- name: Check out code
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab #v3.5.2
Expand All @@ -43,4 +45,16 @@ jobs:
${{ runner.os }}-turbo-
- name: 'Code review'
run: yarn run code-review
run: yarn run code-review

- name: Set env
run: |
echo "NODE_TLS_REJECT_UNAUTHORIZED=0" >> $GITHUB_ENV
echo "COSMOSDB_NAME=${{github.run_id}}" >> $GITHUB_ENV
echo "COSMOSDB_CONNECTION_STRING=${{secrets.COSMOSDB_CONNECTION_STRING}}" >> $GITHUB_ENV
- name: System tests
run: |
cd __integrations__
yarn install --immutable
yarn test
163 changes: 163 additions & 0 deletions __integrations__/__tests__/system/cosmos/queryenrichment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { CosmosClient } from "@azure/cosmos";
import * as O from "fp-ts/Option";
import * as TE from "fp-ts/lib/TaskEither";
import { pipe } from "fp-ts/lib/function";
import { createCosmosQueryEnrichmentService } from "../../../../src/enrichment/query/cosmos/service";
import { COSMOSDB_CONNECTION_STRING, COSMOSDB_NAME } from "../../../env";
import {
COLLECTION_ID,
COLLECTION_ID_KEY,
COLLECTION_ID_VERSION,
COLLECTION_PARTITION_KEY,
COLLECTION_PARTITION_KEY_VALUE,
COSMOS_COLLECTION_NAME,
VERSION_FIELD_NAME,
VERSION_FIELD_VALUE,
WRONG_VERSION_FIELD_VALUE,
createCosmosDbAndCollections,
deleteDatabase,
} from "../../../utils/cosmos";

const client = new CosmosClient(COSMOSDB_CONNECTION_STRING);

beforeAll(async () => {
await pipe(
createCosmosDbAndCollections(client, COSMOSDB_NAME),
TE.getOrElse((e) => {
throw Error(
`Cannot initialize integration tests - ${JSON.stringify(e.message)}`,
);
}),
)();
}, 60000);

afterAll(async () => {
await pipe(
deleteDatabase(client, COSMOSDB_NAME),
TE.getOrElse((e) => {
throw Error(`Cannot delete db ${e.message}`);
}),
)();
});

describe("findByKey", () => {
it("should return Some(unknown) when the item is found", async () => {
await pipe(
createCosmosQueryEnrichmentService(COSMOSDB_CONNECTION_STRING),
TE.fromEither,
TE.chain((service) =>
service.findByKey(
COSMOSDB_NAME,
COSMOS_COLLECTION_NAME,
COLLECTION_ID_KEY,
COLLECTION_PARTITION_KEY,
),
),
TE.bimap(
(err) => {
new Error(
`it should not fail while finding an existing document - ${err.message}`,
);
},
O.fold(
() => {
throw new Error("it should be some");
},
(result) =>
expect(result).toMatchObject({
id: COLLECTION_ID_KEY,
key: COLLECTION_PARTITION_KEY,
}),
),
),
)();
});

it("should return None when the item is not found - find by id and partition key", async () => {
await pipe(
createCosmosQueryEnrichmentService(COSMOSDB_CONNECTION_STRING),
TE.fromEither,
TE.chain((service) =>
service.findByKey(
COSMOSDB_NAME,
COSMOS_COLLECTION_NAME,
COLLECTION_ID,
COLLECTION_PARTITION_KEY,
),
),
TE.bimap(
(err) => {
new Error(
`it should not fail while finding an existing document - ${err.message}`,
);
},
(result) => expect(O.isNone(result)).toBeTruthy(),
),
)();
});
});

describe("findByLastVersionKey", () => {
it("should return Some(unknown) when the item is found with the last version", async () => {
return await pipe(
createCosmosQueryEnrichmentService(COSMOSDB_CONNECTION_STRING),
TE.fromEither,
TE.chain((service) =>
service.findLastVersionByKey(
COSMOSDB_NAME,
COSMOS_COLLECTION_NAME,
VERSION_FIELD_NAME,
VERSION_FIELD_VALUE,
COLLECTION_ID_VERSION,
COLLECTION_PARTITION_KEY,
COLLECTION_PARTITION_KEY_VALUE,
),
),
TE.bimap(
(err) => {
new Error(
`it should not fail while finding an existing document - ${err.message}`,
);
},
O.fold(
() => {
throw new Error("it should be some");
},
(result) => {
expect(result).toMatchObject({
id: COLLECTION_ID_VERSION,
key: COLLECTION_PARTITION_KEY_VALUE,
version: VERSION_FIELD_VALUE,
});
},
),
),
)();
});

it("should return None(unknown) when the item is not found with the specific version", async () => {
await pipe(
createCosmosQueryEnrichmentService(COSMOSDB_CONNECTION_STRING),
TE.fromEither,
TE.chain((service) =>
service.findLastVersionByKey(
COSMOSDB_NAME,
COSMOS_COLLECTION_NAME,
VERSION_FIELD_NAME,
WRONG_VERSION_FIELD_VALUE,
COLLECTION_ID_KEY,
COLLECTION_PARTITION_KEY,
COLLECTION_PARTITION_KEY_VALUE,
),
),
TE.bimap(
(err) => {
new Error(
`it should not fail while finding an existing document - ${err.message}`,
);
},
(result) => expect(O.isNone(result)).toBeTruthy(),
),
)();
});
});
9 changes: 9 additions & 0 deletions __integrations__/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as dotenv from "dotenv";

dotenv.config({ path: "./environments/.env" });

export const COSMOSDB_URI = process.env.COSMOSDB_URI;
export const COSMOSDB_KEY = process.env.COSMOSDB_KEY;
export const COSMOSDB_CONNECTION_STRING =
process.env.COSMOSDB_CONNECTION_STRING ?? "COSMOSDB_CONNECTION_STRING";
export const COSMOSDB_NAME = process.env.COSMOSDB_NAME ?? "db";
37 changes: 37 additions & 0 deletions __integrations__/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@pagopa/change-data-capturer-integrations",
"version": "1.0.0",
"description": "Integration test suite for integration testing",
"license": "MIT",
"keywords": [],
"author": "",
"scripts": {
"start": "docker-compose up -d",
"stop": "docker-compose down",
"build": "tsc",
"test": "jest --runInBand"
},
"dependencies": {
"@azure/cosmos": "^4.0.0",
"dotenv": "^16.3.1",
"fp-ts": "^2.16.1"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"dotenv-cli": "^7.3.0",
"jest": "^29.7.0",
"prettier": "^3.1.1",
"ts-jest": "^29.1.1",
"typescript": "^5.3.3"
},
"jest": {
"preset": "ts-jest",
"collectCoverage": false,
"testEnvironment": "node",
"testPathIgnorePatterns": [
"dist",
"/node_modules"
]
}
}
27 changes: 27 additions & 0 deletions __integrations__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
},
"references": [
{
"path": "..",
}
],
"include": [
"src/**/*"
]
}
Loading

0 comments on commit 27c40b9

Please sign in to comment.