Skip to content

Refactor test parameter validation into shared helpers#1583

Merged
huss merged 2 commits intoOpenEnergyDashboard:developmentfrom
KaulanSerzhanuly:kaulan_test_helpers
Mar 30, 2026
Merged

Refactor test parameter validation into shared helpers#1583
huss merged 2 commits intoOpenEnergyDashboard:developmentfrom
KaulanSerzhanuly:kaulan_test_helpers

Conversation

@KaulanSerzhanuly
Copy link
Copy Markdown
Contributor

@KaulanSerzhanuly KaulanSerzhanuly commented Mar 13, 2026

Description

  • Refactors multiple ParamsTest.js files to use shared helpers from src/server/test/util/validationHelpers.js instead of duplicating validation logic.
  • Adds/extends helpers for common patterns: string/int/bool validation with configurable expected status, numeric path IDs, required query parameters, and extra-field (parameter injection) checks.
  • Keeps test behavior consistent while reducing duplication and making it easier to add new tests.

Details:

  • Updates validationHelpers.js:
    • Extends validateString, validateInt, and validateBool to accept an expectedStatus parameter.
    • Adds validateNumericIdInPath, expectValidNumericIdInPath, and validateRequiredQueryParams helpers for common ID and query validation patterns.
  • Refactors the following tests to use the helpers:
    compareReadingsParamsTest.js
    conversionsParamsTest.js
    groupsParamsTest.js
    mapsParamsTest.js
    readingsParamsTest.js
    unitAddParamsTest.js

Fixes #1572

Author: Kaulan Serzhanuly

(In general, OED likes to have at least one issue associated with each pull request. Replace [issue] with the OED GitHub issue number. In the preview you will see an issue description if you hover over that number. You can create one yourself before doing this pull request. This is where details are normally given on what is being addressed. Note you should not use the word "Fixes" if it does not completely address the issue since the issue would automatically be closed on merging the pull request. In that case use "Partly Addresses #[issue].)

Type of change

(Check the ones that apply by placing an "x" instead of the space in the [ ] so it becomes [x])

  • Note merging this changes the database configuration.
  • This change requires a documentation update

Checklist

(Note what you have done by placing an "x" instead of the space in the [ ] so it becomes [x]. It is hoped you do all of them.)

  • I have followed the OED pull request ideas
  • I have removed text in ( ) from the issue request
  • You acknowledge that every person contributing to this work has signed the OED Contributing License Agreement and each author is listed in the Description section.

Limitations

(Describe any issues that remain or work that should still be done.)

@huss
Copy link
Copy Markdown
Member

huss commented Mar 13, 2026

@KaulanSerzhanuly I see you checked the box for having completed the CLA. However, I did not find it in the OED records. If you have under a different GitHub ID or think the OED records are off then let me know. Otherwise, could you please fill it out.

@KaulanSerzhanuly
Copy link
Copy Markdown
Contributor Author

Hello I did sign everything tho, it was under this accounts ID. This accounts email is kaulan.2004@gmail.com and name to this account is KaulanSerzhanuly. Just in case i have resubmited it

@KaulanSerzhanuly
Copy link
Copy Markdown
Contributor Author

I forgot to tag you sir @huss

@KaulanSerzhanuly
Copy link
Copy Markdown
Contributor Author

@huss Hello again i resubmited OED record. Could you please check it again?

Copy link
Copy Markdown
Member

@huss huss left a comment

Choose a reason for hiding this comment

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

@KaulanSerzhanuly First, I'm sorry this took a few days while my efforts needed to be elsewhere. Second, thank you for this work. Overall, I like these changes. It caused me to think in a deeper way about possible centralization and I've made a number of comments about this. I want to note that I did not carefully think them through so it may be the case that it does not apply across all routes. It is also possible my modest comments are not clear enough. Please feel free to put in your thoughts and ask me about anything. I'm always here to help.

Comment thread src/server/test/util/validationHelpers.js Outdated
Comment thread src/server/test/util/validationHelpers.js Outdated
Comment thread src/server/test/util/validationHelpers.js
Comment thread src/server/test/util/validationHelpers.js
Comment thread src/server/test/routes/compareReadingsParamsTest.js Outdated
.send(payloadWithExtra);

expect(res.status).to.equal(HTTP_CODE.FORBIDDEN);
await validateNoExtraFields({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not going to mark every one but here is one where I wonder about a wrapper function to set these values and then call the validate function as one or more params are the same across tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reasonable. I didn’t add that wrapper here to keep the diff focused; happy to add a local helper (or a tiny test util) in a follow-up if we standardize on the same forbidden + base payload shape in several tests.

Comment thread src/server/test/routes/readingsParamsTest.js Outdated
Comment thread src/server/test/routes/readingsParamsTest.js Outdated
Comment thread src/server/test/routes/unitAddParamsTest.js
Comment thread src/server/test/routes/unitAddParamsTest.js Outdated
@KaulanSerzhanuly
Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and for flagging that some ideas might not fit every route—I’ll call those out where I left them for later.
I pushed updates that: document defaults as HTTP_CODE.BAD_REQUEST, merge the path-ID helpers via a shared implementation + clarify expectedStatus as number | number[], extend validateInt / validateBool (including the edge cases you mentioned), centralize the duplicated group_id tests and the readings timeInterval, treat unit enum-like fields with enumValues in validateString, and drop redundant expectedStatus where it matched the default. Happy to discuss anything you’d still like generalized.

@KaulanSerzhanuly
Copy link
Copy Markdown
Contributor Author

@huss Please check out the lastest changes I pushed

@KaulanSerzhanuly KaulanSerzhanuly requested a review from huss March 26, 2026 03:07
@huss
Copy link
Copy Markdown
Member

huss commented Mar 29, 2026

@huss Please check out the lastest changes I pushed

Oops! I lost track of this. I plan to look at it in the near future.

Copy link
Copy Markdown
Member

@huss huss left a comment

Choose a reason for hiding this comment

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

@KaulanSerzhanuly Thank you for carefully and thoroughly addressing my previous comments. There are a couple unresolved but I view them as future work. I don't want perfection to be the impediment to progress. You fixed all the important cases (and then more) and this addresses creating general test code for routes. If you want to do anything else on this or something else then you are welcome to contact me. Congratulations on your first accepted pull request to OED.

@huss huss merged commit b4a4db6 into OpenEnergyDashboard:development Mar 30, 2026
3 checks passed
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.

Test Generalization and Helper Functions

2 participants