-
Notifications
You must be signed in to change notification settings - Fork 2
feat: improving test speed and maintainability #1560
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c928884
feat: trying to improve running speed of the retry code in e2e tests
SamRobinson75684 daa5fb9
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 792df7a
fix: removing unwanted changes
SamRobinson75684 10807fc
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 27dd36a
chore: addressing comments on PR
SamRobinson75684 d292ea8
chore: adding new config items to env example
SamRobinson75684 856494e
fix: trying to make sure retry is actually working
SamRobinson75684 f507d9f
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 2b1c594
fix: trying to make sure retry is actually working
SamRobinson75684 9c7ec6d
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 c5e5ce2
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 9790d59
fix: making sure tests are not flaky
SamRobinson75684 497b4e4
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 0522a0d
fix: trying to make sure that both all tests run the same. (this comm…
SamRobinson75684 f67a4f0
fix: trying to get local debugging working
SamRobinson75684 4a625f1
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 009671a
fix: code clean up
SamRobinson75684 49ef0c4
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 f6fb844
Merge branch 'main' into feat/DTOSS-10703-improving-test-speed
SamRobinson75684 e7fa30e
fix: adding tests back in
SamRobinson75684 0b69ff4
fix: creating pick test types function
SamRobinson75684 fe6b061
fix: removing unwanted code
SamRobinson75684 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { APIResponse, expect } from "@playwright/test"; | ||
| import { config } from "../../config/env"; | ||
| import { fetchApiResponse, findMatchingObject, validateFields } from "../apiHelper"; | ||
| import { ApiResponse } from "../core/types"; | ||
|
|
||
| const NHS_NUMBER_KEY = config.nhsNumberKey; | ||
| const NHS_NUMBER_KEY_EXCEPTION_DEMOGRAPHIC = config.nhsNumberKeyExceptionDemographic; | ||
|
|
||
| export async function validateApiResponse(validationJson: any, request: any): Promise<{ status: boolean; errorTrace?: any }> { | ||
| let status = false; | ||
| let endpoint = ""; | ||
| let errorTrace: any = undefined; | ||
|
|
||
| try { | ||
| for (const apiValidation of validationJson) { | ||
| endpoint = apiValidation.validations.apiEndpoint; | ||
| var response = await pollAPI(endpoint, apiValidation, request); | ||
| console.info(response); | ||
| switch(response.status()) { | ||
| case 204: | ||
| console.info("now handling no content response"); | ||
| const expectedCount = apiValidation.validations.expectedCount; | ||
| status = await HandleNoContentResponse(expectedCount, apiValidation, endpoint); | ||
| break; | ||
| case 200: | ||
| console.info("now handling OK response"); | ||
| status = await handleOKResponse(apiValidation, endpoint, response); | ||
| default: | ||
| console.error("there was an error when handling response from "); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| catch (error) { | ||
| const errorMsg = `Endpoint: ${endpoint}, Error: ${error instanceof Error ? error.stack || error.message : error}`; | ||
| errorTrace = errorMsg; | ||
| console.error(errorMsg); | ||
| } | ||
| return { status, errorTrace }; | ||
| } | ||
|
|
||
| async function handleOKResponse(apiValidation: any, endpoint: string, response: any ) : Promise<boolean>{ | ||
| const responseBody = await response.json(); | ||
| expect(Array.isArray(responseBody)).toBeTruthy(); | ||
|
|
||
| const { matchingObject, nhsNumber, matchingObjects } = await findMatchingObject(endpoint, responseBody, apiValidation); | ||
|
|
||
| console.info(`Validating fields using 🅰️\t🅿️\tℹ️\t ${endpoint}`); | ||
| console.info(`From Response ${JSON.stringify(matchingObject, null, 2)}`); | ||
| return await validateFields(apiValidation, matchingObject, nhsNumber, matchingObjects); | ||
| } | ||
|
|
||
|
|
||
| async function HandleNoContentResponse(expectedCount: number, apiValidation: any, endpoint: string): Promise<boolean> { | ||
| if (expectedCount !== undefined && Number(expectedCount) === 0) { | ||
| console.info(`✅ Status 204: Expected 0 records for endpoint ${endpoint}`); | ||
|
|
||
| // Get NHS number for validation | ||
| const nhsNumber = apiValidation.validations.NHSNumber || | ||
| apiValidation.validations.NhsNumber || | ||
| apiValidation.validations[NHS_NUMBER_KEY] || | ||
| apiValidation.validations[NHS_NUMBER_KEY_EXCEPTION_DEMOGRAPHIC]; | ||
|
|
||
| console.info(`Validating fields using 🅰️\t🅿️\tℹ️\t ${endpoint}`); | ||
| console.info(`From Response: null (204 No Content - 0 records as expected)`); | ||
| return await validateFields(apiValidation, null, nhsNumber, []); | ||
| } else { | ||
| // 204 is unexpected, throw error to trigger retry | ||
| throw new Error(`Status 204: No data found in the table using endpoint ${endpoint}`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| async function pollAPI(endpoint: string, apiValidation: any, request: any): Promise<APIResponse> { | ||
| let apiResponse: APIResponse | null = null; | ||
| let i = 0; | ||
|
|
||
| let maxNumberOfRetries = config.maxNumberOfRetries; | ||
| let maxTimeBetweenRequests = config.maxTimeBetweenRequests; | ||
|
|
||
| console.info(`now trying request for ${maxNumberOfRetries} retries`); | ||
| while (i < maxNumberOfRetries) { | ||
| try { | ||
| apiResponse = await fetchApiResponse(endpoint, request); | ||
| if (apiResponse.status() == 200) { | ||
| console.info("200 response found") | ||
| break; | ||
| } | ||
| } | ||
| catch(exception) { | ||
| console.error("Error reading request body:", exception); | ||
| } | ||
| i++; | ||
|
|
||
| console.info(`http response completed ${i}/${maxNumberOfRetries} of number of reties`); | ||
| await new Promise(res => setTimeout(res, maxTimeBetweenRequests)); | ||
| } | ||
|
|
||
| if (!apiResponse) { | ||
| throw new Error("apiResponse was never assigned"); | ||
| } | ||
|
|
||
| return apiResponse; | ||
| } | ||
|
|
||
|
|
||
| export async function pollApiForOKResponse(httpRequest: () => Promise<ApiResponse>): Promise<ApiResponse>{ | ||
| let apiResponse: ApiResponse | null = null; | ||
| let i = 0; | ||
| let maxNumberOfRetries = config.maxNumberOfRetries; | ||
| let maxTimeBetweenRequests = config.maxTimeBetweenRequests; | ||
|
|
||
| console.info(`now trying request for ${maxNumberOfRetries} retries`); | ||
| while (i < maxNumberOfRetries) { | ||
| try { | ||
| apiResponse = await httpRequest(); | ||
| if (apiResponse.status == 200) { | ||
| console.info("200 response found") | ||
| break; | ||
| } | ||
| } | ||
| catch(exception) { | ||
| console.error("Error reading request body:", exception); | ||
| } | ||
| i++; | ||
|
|
||
| console.info(`http response completed ${i}/${maxNumberOfRetries} of number of reties`); | ||
|
SamRobinson75684 marked this conversation as resolved.
Outdated
|
||
| await new Promise(res => setTimeout(res, maxTimeBetweenRequests)); | ||
| } | ||
|
|
||
| if (!apiResponse) { | ||
| throw new Error("apiResponse was never assigned"); | ||
| } | ||
| return apiResponse; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.