-
-
Notifications
You must be signed in to change notification settings - Fork 0
OBPIH-7459 Create API URL file in e2e project #88
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
644dda1
OBPIH-7459 Refactor API URL usage across multiple components and test…
alannadolny 172a78a
OBPIH-7459 Fix fetching products
alannadolny d318b5f
OBPIH-7459 Add generating file with product codes before running tests
alannadolny 496fbe7
OBPIH-7459 Fixes after review
alannadolny f2926a2
OBPIH-7459 Pull products name from csv
alannadolny e3b118a
OBPIH-7459 Add validation for minimal array length
alannadolny 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,6 @@ node_modules/ | |
| /localFiles | ||
|
|
||
| .idea/ | ||
|
|
||
| # generated | ||
| src/generated/ | ||
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
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,82 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const CSV_PATH = path.join(__dirname, '..', 'src', 'setup', 'dataImport', 'products.csv'); | ||
| const OUTPUT_PATH = path.join(__dirname, '..', 'src', 'generated', 'ProductCodes.generated.ts'); | ||
|
|
||
| function parseCsv(csv) { | ||
| const lines = csv.trim().split('\n'); | ||
| if (lines.length < 2) { | ||
| return []; | ||
| } | ||
| const headers = lines[0].split(',').map((h) => h.trim()); | ||
| const codeIndex = headers.indexOf('ProductCode'); | ||
| const nameIndex = headers.indexOf('Name'); | ||
| if (codeIndex === -1) { | ||
| throw new Error(`ProductCode column not found in ${CSV_PATH}`); | ||
| } | ||
| if (nameIndex === -1) { | ||
| throw new Error(`Name column not found in ${CSV_PATH}`); | ||
| } | ||
| return lines | ||
| .slice(1) | ||
| .map((line) => line.split(',')) | ||
| .map((cols) => ({ | ||
| code: cols[codeIndex]?.trim() ?? '', | ||
| name: cols[nameIndex]?.trim() ?? '', | ||
| })) | ||
| .filter((row) => row.code.length > 0); | ||
| } | ||
|
|
||
| function buildFileContent(rows) { | ||
| const codeByName = new Map(rows.map((row) => [row.name, row.code])); | ||
|
|
||
| if (rows.length < 6) { | ||
| throw new Error('We require at least 6 rows with a valid product code in products.csv') | ||
| } | ||
| // Stable keys for the generated objects, mapped to the name value | ||
| // in products.csv that identifies the row. The productCode (value) is read | ||
| // from the CSV at generation time, so codes can change without touching tests. | ||
| const PRODUCT_KEYS = { | ||
| ONE: rows[0].name, | ||
| TWO: rows[1].name, | ||
| THREE: rows[2].name, | ||
| FOUR: rows[3].name, | ||
| FIVE: rows[4].name, | ||
| SIX: rows[5].name, | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll approve, but I think you should add the following before this block so that it's clearer what is supposed to be happening. Otherwise you get a random index out of bounds exception: |
||
|
|
||
| const entries = Object.entries(PRODUCT_KEYS).map(([key, name]) => { | ||
| const code = codeByName.get(name); | ||
| if (!code) { | ||
| throw new Error( | ||
| `Product with Name "${name}" (key ${key}) not found in ${CSV_PATH}. ` + | ||
| 'Update PRODUCT_KEYS in scripts/generateProductCodes.mjs or add the row to products.csv.' | ||
| ); | ||
| } | ||
| return ` ${key}: '${code}',`; | ||
| }); | ||
|
|
||
| return `// AUTO-GENERATED FILE. DO NOT EDIT. | ||
| // Regenerated from src/setup/dataImport/products.csv by scripts/generateProductCodes.mjs | ||
| // Output: src/generated/ProductCodes.generated.ts | ||
|
|
||
| export const Product = { | ||
| ${entries.join('\n')} | ||
| } as const; | ||
|
|
||
| export type ProductCode = (typeof Product)[keyof typeof Product]; | ||
| `; | ||
| } | ||
|
|
||
| const csv = fs.readFileSync(CSV_PATH, 'utf8'); | ||
| const rows = parseCsv(csv); | ||
| if (rows.length === 0) { | ||
| throw new Error(`No product codes found in ${CSV_PATH}`); | ||
| } | ||
|
|
||
| fs.mkdirSync(path.dirname(OUTPUT_PATH), { recursive: true }); | ||
| fs.writeFileSync(OUTPUT_PATH, buildFileContent(rows), 'utf8'); | ||
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a fine solution for now, but by always using the same products for all tests, we can get into a situation where one test changes/deletes one of these products, causing all other tests to fail.
A better solution is likely to create the products that we need at the start of each test then delete the products at the end of the test. That way the tests are totally independent of each other.
But we can deal with that later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, the products from CSV are imported during every single run, so we are sure that those products exist while testing