Skip to content

Add unit test suites for jobplugins, dataset and strategus-analysis plugin functions - #2989

Open
ohdsi-trex wants to merge 9 commits into
developfrom
ohdsi-trex/plugin-function-unit-tests
Open

Add unit test suites for jobplugins, dataset and strategus-analysis plugin functions#2989
ohdsi-trex wants to merge 9 commits into
developfrom
ohdsi-trex/plugin-function-unit-tests

Conversation

@ohdsi-trex

Copy link
Copy Markdown
Collaborator

Summary

Adds deno test unit suites to three plugin functions that previously had none: jobplugins, dataset, and strategus-analysis. 42 tests, all passing.

A shared harness under plugins/functions/_shared/testing/ supplies Express request/response doubles, a Trex ambient-global shim, router lookup helpers, and an express-validator runner. Tests call route handlers directly with fake req/res and stub collaborators (API clients, TypeORM repositories) via @std/testing/mock. No live HTTP, no live database.

Production change — one file pair, testability only

dataset/index.ts declared export class DatasetRouter and called app.listen(8000) at module scope, so importing the class started a server and hung the test process. The class is extracted verbatim into dataset/router.ts; index.ts is now bootstrap-only.

This is a pure move: the class body and import block are byte-identical to the original, and the registered route table was dumped before and after and matches exactly (8 layers, same order, generateDatasetSchema middleware intact). No handler logic, status code, or response string changed.

Two lazy-initialization changes considered during design were dropped as unnecessary — module-scope Deno.env reads are handled by importing a _setup.ts first (ES modules evaluate in import order), and TypeORM's new DataSource() does not connect until .initialize().

Coverage

Plugin Tests Covers
strategus-analysis 14 study validation, analysis service logic, result enrichment, route error propagation
jobplugins 21 DQD request validators, controller validation, error handling, missing-auth path
dataset 7 request parsing, input validation, happy path, error handling, missing-token path

Each test file was verified able to fail (assertion mutated, run, confirmed FAILED, restored).

Known limitations

  • --no-check on strategus-analysis and jobplugins. Both inherit pre-existing type errors that any importing test picks up: datasource.ts passes PG__PORT (a string) where TypeORM wants a number, and PortalServerAPI.ts:280 references an out-of-scope path. Fixing them is a production change, kept out of scope. dataset type-checks cleanly.
  • Coverage is a proven pattern, not a sweep — 1 of 17 jobplugins controllers and 2 of 8 dataset routes. Extending is mechanical repetition of the established shape.
  • These are characterization tests. They pin current behaviour, including several questionable behaviours documented explicitly in test names/comments rather than silently fixed (see below).
  • No live edge-runtime verification. Platform functions are pooled and do not hot-reload, so exercising the refactor against a served route needs an alp-trex restart.

Pre-existing issues found while writing these tests (not fixed here)

  1. jobplugins/src/api/PortalServerAPI.ts:280const path is declared inside try but referenced in catch; any failure throws ReferenceError and destroys the original error.
  2. jobplugins/src/middlewares/DqdRequestValidatorMiddlewares.ts:25.isUUID().notEmpty().withMessage(...) attaches the message only to notEmpty, so a malformed UUID returns the unhelpful default "Invalid value".
  3. dataset/router.ts handles a missing Authorization header two different ways on the same router: /cdm-schema/snapshot/metadata constructs its API client outside the try (unhandled rejection, no response), while /cohorts constructs it inside (500). Neither returns 401.
  4. strategus-analysis validateStudyId returns true when no token is supplied, so unauthenticated callers bypass study-existence validation.

Each is characterized by a named test asserting actual behaviour. Fixing any is a behaviour change deserving its own decision.

Not included

The CI workflow that runs these suites (.github/workflows/plugin-function-tests.yml) is written and committed locally but not in this PR — the available token lacks the workflow OAuth scope. It needs to be pushed by someone with that scope, or added in a follow-up.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Deno unit test coverage for several Trex plugin functions (jobplugins, dataset, strategus-analysis) using a shared in-repo test harness, and refactors the dataset plugin to make the router importable without pulling in the server bootstrap.

Changes:

  • Added new Deno test suites for strategus-analysis, jobplugins, and dataset that directly invoke Express route handlers with request/response doubles and stubs.
  • Introduced shared test utilities under plugins/functions/_shared/testing/ (HTTP doubles, router lookup helpers, Trex global shim, express-validator runner).
  • Extracted DatasetRouter into plugins/functions/dataset/router.ts and simplified dataset/index.ts to bootstrap wiring.

Reviewed changes

Copilot reviewed 19 out of 22 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
plugins/functions/strategus-analysis/tests/utils.test.ts Adds unit test for getDummyDataset.
plugins/functions/strategus-analysis/tests/study-validation.test.ts Adds middleware/service-level study validation characterization tests.
plugins/functions/strategus-analysis/tests/analysis-service.test.ts Adds unit tests for analysis service repository interactions and enrichment behavior.
plugins/functions/strategus-analysis/tests/analysis-routes.test.ts Adds route-handler tests for analysis endpoints and error mapping.
plugins/functions/strategus-analysis/tests/_setup.ts Test setup: env priming + Trex global shim.
plugins/functions/strategus-analysis/deno.json Adds deno task test and std imports for test assertions/mocking.
plugins/functions/strategus-analysis/deno.lock Locks new std dependencies used by tests.
plugins/functions/jobplugins/tests/dqd-validators.test.ts Adds express-validator chain tests for DQD DTO/query validation.
plugins/functions/jobplugins/tests/dqd-controller.test.ts Adds controller route-chain tests (validators + handler) for key DQD endpoints.
plugins/functions/jobplugins/tests/_setup.ts Test setup: SERVICE_ROUTES + DB env + Trex global shim.
plugins/functions/jobplugins/deno.json Adds deno task test and std imports for test assertions/mocking.
plugins/functions/jobplugins/deno.lock Locks new std dependencies used by tests.
plugins/functions/dataset/tests/dataset-router.test.ts Adds dataset router tests for query parsing, happy path, and error paths.
plugins/functions/dataset/tests/_setup.ts Test setup: SERVICE_ROUTES + DB env + Trex global shim.
plugins/functions/dataset/router.ts Extracted DatasetRouter implementation into its own module for testability.
plugins/functions/dataset/index.ts Bootstrap-only wiring for express app + router mount.
plugins/functions/dataset/deno.json Adds deno task test and std imports for test assertions/mocking.
plugins/functions/dataset/deno.lock Locks new std dependencies used by tests.
plugins/functions/_shared/testing/validator-helpers.ts Shared helper to execute express-validator chains on mock requests.
plugins/functions/_shared/testing/trex-global.ts Shared Trex ambient-global shim for tests.
plugins/functions/_shared/testing/router-helpers.ts Shared router introspection helpers to locate handlers/chains.
plugins/functions/_shared/testing/http-doubles.ts Shared Express-style request/response doubles for tests.

recursive: true,
});
await fs.writeFile(localFilePath, buffer);
} catch (e) {}
@hengxian-jiang hengxian-jiang linked an issue Jul 27, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create unit test suites for initial three functions

2 participants