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

Add testWorkflow to WorkflowResourceService #70

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
run: yarn install
- name: Run the linter
run: yarn lint

unit-tests:
runs-on: ubuntu-latest
steps:
Expand All @@ -27,9 +28,16 @@ jobs:
node-version: "18"
- name: Install package
run: yarn install
- name: Run Unit test
run: yarn test
- name: Run Tests
run: yarn test --ci --reporters=default --reporters=jest-junit
env:
KEY_ID: ${{ secrets.KEY_ID }}
KEY_SECRET: ${{ secrets.KEY_SECRET }}
SERVER_URL: ${{ secrets.SERVER_URL }}
- name: Publish Test Results
if: always()
uses: dorny/test-reporter@v1
with:
name: Test report
path: reports/jest-*.xml
reporter: jest-junit
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"dotenv": "^16.0.1",
"eslint": "^6.1.0",
"jest": "^29.4.3",
"jest-junit": "^16.0.0",
"ts-jest": "^29.0.5",
"ts-node": "^10.7.0",
"tsup": "^7.1.0",
Expand All @@ -83,6 +84,14 @@
"engines": {
"node": ">=18"
},
"dependencies": {
"dependencies": {},
"jest-junit": {
"outputDirectory": "reports",
"outputName": "jest-junit.xml",
"ancestorSeparator": " › ",
"uniqueOutputName": "false",
"suiteNameTemplate": "{filepath}",
"classNameTemplate": "{classname}",
"titleTemplate": "{title}"
}
}
40 changes: 40 additions & 0 deletions src/common/open-api/__test__/WorkflowResourceService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect, describe, test, jest } from "@jest/globals";
import { MetadataClient } from "../../../core";
import { simpleTask, workflow } from "../../../core/sdk";
import { orkesConductorClient } from "../../../orkes";
import { TaskDefTypes } from "../../types";

const config = {
keyId: `${process.env.KEY_ID}`,
keySecret: `${process.env.KEY_SECRET}`,
serverUrl: `${process.env.SERVER_URL}`,
};

describe("WorkflowResourceService", () => {
jest.setTimeout(120000);

test("Should test a workflow", async () => {
const client = await orkesConductorClient(config);
const metadataClient = new MetadataClient(client);
const tasks: TaskDefTypes[] = [
simpleTask("simple_ref", "le_simple_task", {}),
];

const wfDef = workflow("unit_test_wf", tasks);
wfDef.outputParameters = { message: "${simple_ref.output.message}" };
metadataClient.registerWorkflowDef(wfDef, true);

const status = "COMPLETED";
const output = { message: "Mocked message" };

const wf = await client.workflowResource.testWorkflow({
name: wfDef.name,
taskRefToMockOutput: {
"simple_ref": [{ status, output }],
},
});

expect(wf.status).toEqual(status);
expect(wf.output).toEqual(output);
});
});
13 changes: 13 additions & 0 deletions src/common/open-api/models/TaskMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type TaskMock = {
executionTime?: number;
output: Record<string, any>;
queueWaitTime?: number;
status:
| "IN_PROGRESS"
| "FAILED"
| "FAILED_WITH_TERMINAL_ERROR"
| "COMPLETED";
};
20 changes: 20 additions & 0 deletions src/common/open-api/models/WorkflowTestRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { TaskMock } from "./TaskMock";
import type { WorkflowDef } from "./WorkflowDef";
export type WorkflowTestRequest = {
correlationId?: string;
createdBy?: string;
externalInputPayloadStoragePath?: string;
idempotencyKey?: string;
idempotencyStrategy?: "FAIL" | "RETURN_EXISTING";
input?: Record<string, Record<string, any>>;
name: string;
priority?: number;
subWorkflowTestRequest?: Record<string, WorkflowTestRequest>;
taskRefToMockOutput?: Record<string, Array<TaskMock>>;
taskToDomain?: Record<string, string>;
version?: number;
workflowDef?: WorkflowDef;
};
18 changes: 18 additions & 0 deletions src/common/open-api/services/WorkflowResourceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { StartWorkflowRequest } from '../models/StartWorkflowRequest';
import type { Workflow } from '../models/Workflow';
import type { WorkflowRun } from '../models/WorkflowRun';
import type { WorkflowStatus } from '../models/WorkflowStatus';
import type { WorkflowTestRequest } from '../models/WorkflowTestRequest';


import type { CancelablePromise } from '../core/CancelablePromise';
import type { BaseHttpRequest } from '../core/BaseHttpRequest';
Expand Down Expand Up @@ -605,4 +607,20 @@ export class WorkflowResourceService {
});
}

/**
* Test workflow execution using mock data
* @param requestBody
* @returns Workflow OK
* @throws ApiError
*/
public testWorkflow(
requestBody: WorkflowTestRequest
): CancelablePromise<Workflow> {
return this.httpRequest.request({
method: 'POST',
url: '/workflow/test',
body: requestBody,
mediaType: 'application/json',
});
}
}
22 changes: 18 additions & 4 deletions src/core/metadataClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
ConductorClient,
TaskDef,
} from "../common";
import { ConductorClient, TaskDef } from "../common";
import { WorkflowDef } from "../common/open-api";
import { tryCatchReThrow } from "./helpers";

export class MetadataClient {
Expand Down Expand Up @@ -46,4 +44,20 @@ export class MetadataClient {
this._client.metadataResource.updateTaskDef(taskDef)
);
}

/**
* Creates or updates (overwrite: true) a workflow definition
*
* @param workflowDef
* @param overwrite
* @returns
*/
public registerWorkflowDef(
workflowDef: WorkflowDef,
overwrite: boolean = false
) {
return tryCatchReThrow(() =>
this._client.metadataResource.create(workflowDef, overwrite)
);
}
}
4 changes: 2 additions & 2 deletions src/task/__tests__/TaskManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("TaskManager", () => {
const executor = new WorkflowExecutor(client);

const worker: ConductorWorker = {
taskDefName: "taskmanager-error-test",
taskDefName: "taskmanager-error-test2",
execute: async () => {
throw Error("This is a forced error");
},
Expand All @@ -71,7 +71,7 @@ describe("TaskManager", () => {
manager.startPolling();

await executor.startWorkflow({
name: "TaskManagerTestE",
name: "TaskManagerTestE2",
input: {},
version: 1,
});
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,16 @@ jest-haste-map@^29.5.0:
optionalDependencies:
fsevents "^2.3.2"

jest-junit@^16.0.0:
version "16.0.0"
resolved "https://registry.yarnpkg.com/jest-junit/-/jest-junit-16.0.0.tgz#d838e8c561cf9fdd7eb54f63020777eee4136785"
integrity sha512-A94mmw6NfJab4Fg/BlvVOUXzXgF0XIH6EmTgJ5NDPp4xoKq0Kr7sErb+4Xs9nZvu58pJojz5RFGpqnZYJTrRfQ==
dependencies:
mkdirp "^1.0.4"
strip-ansi "^6.0.1"
uuid "^8.3.2"
xml "^1.0.1"

jest-leak-detector@^29.5.0:
version "29.5.0"
resolved "https://registry.yarnpkg.com/jest-leak-detector/-/jest-leak-detector-29.5.0.tgz#cf4bdea9615c72bac4a3a7ba7e7930f9c0610c8c"
Expand Down Expand Up @@ -2823,6 +2833,11 @@ mkdirp@^0.5.1:
dependencies:
minimist "^1.2.6"

mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

[email protected]:
version "2.1.2"
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
Expand Down Expand Up @@ -3578,6 +3593,11 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

uuid@^9.0.0:
version "9.0.0"
resolved "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz"
Expand Down Expand Up @@ -3686,6 +3706,11 @@ [email protected]:
dependencies:
mkdirp "^0.5.1"

xml@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
integrity sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==

y18n@^5.0.5:
version "5.0.8"
resolved "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz"
Expand Down
Loading