-
-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests for finace and case studies (#3037)
Co-authored-by: Ansh Goyal <[email protected]>
- Loading branch information
1 parent
d74cbac
commit 0a999d9
Showing
7 changed files
with
245 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,25 +1,19 @@ | ||
const { readdir, writeFile, readFile } = require('fs').promises; | ||
const { convertToJson } = require('../utils'); | ||
const { resolve } = require('path'); | ||
const { convertToJson } = require('../../scripts/utils'); | ||
|
||
const dirWithCaseStudy = 'config/casestudies'; | ||
module.exports = async function buildCaseStudiesList() { | ||
let files = await readdir(dirWithCaseStudy); | ||
let caseStudiesList = []; | ||
module.exports = async function buildCaseStudiesList(dirWithCaseStudy, writeFilePath) { | ||
try { | ||
let files = await readdir(dirWithCaseStudy); | ||
let caseStudiesList = []; | ||
for (let file of files) { | ||
const caseStudyFileName = [dirWithCaseStudy, file].join('/'); | ||
const caseStudyContent = await readFile(caseStudyFileName, 'utf-8'); | ||
const jsonContent = convertToJson(caseStudyContent); | ||
|
||
caseStudiesList.push(jsonContent); | ||
await writeFile( | ||
resolve(__dirname, '../../config', 'case-studies.json'), | ||
JSON.stringify(caseStudiesList) | ||
) | ||
await writeFile(writeFilePath, JSON.stringify(caseStudiesList)) | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
throw err | ||
throw new Error(err); | ||
} | ||
}; | ||
}; |
This file contains 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 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 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,80 @@ | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
const buildCaseStudiesList = require('../../scripts/casestudies/index'); | ||
const { yaml1,yaml2,json1,json2 } = require("../fixtures/caseStudyData"); | ||
|
||
describe('buildCaseStudiesList', () => { | ||
const tempDir = path.join(__dirname, 'temp-test-dir'); | ||
const tempConfigDir = path.join(tempDir, 'config', 'casestudies'); | ||
const tempOutputFile = path.join(tempDir, 'case-studies.json'); | ||
|
||
beforeAll(async () => { | ||
// Create temporary directories | ||
await fs.mkdir(tempConfigDir, { recursive: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
// Clean up temporary directories | ||
await fs.rm(tempDir, { recursive: true, force: true }); | ||
}); | ||
|
||
beforeEach(async () => { | ||
// Clear the config directory before each test | ||
const files = await fs.readdir(tempConfigDir); | ||
await Promise.all(files.map(file => fs.unlink(path.join(tempConfigDir, file)))); | ||
}); | ||
|
||
it('should read YAML files and create a JSON file with case studies', async () => { | ||
// Create sample YAML files | ||
|
||
await fs.writeFile(path.join(tempConfigDir, 'casestudy1.yml'), yaml1); | ||
await fs.writeFile(path.join(tempConfigDir, 'casestudy2.yml'), yaml2); | ||
|
||
// Run the function | ||
await buildCaseStudiesList(tempConfigDir, tempOutputFile); | ||
|
||
// Read the output JSON file | ||
const outputContent = await fs.readFile(tempOutputFile, 'utf-8'); | ||
const outputJson = JSON.parse(outputContent); | ||
|
||
expect(outputJson).toHaveLength(2); | ||
expect(outputJson[0]).toEqual(json1); | ||
expect(outputJson[1]).toEqual(json2); | ||
}); | ||
|
||
|
||
it('should throw an error with incorrect parameters', async () => { | ||
try { | ||
await buildCaseStudiesList('invalid-dir', tempOutputFile); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toMatch(/ENOENT/); // Error for directory not found | ||
} | ||
}); | ||
|
||
it('should throw an error when the output file path is invalid', async () => { | ||
try { | ||
// Call the function with an invalid output file path | ||
await buildCaseStudiesList(tempConfigDir, '/invalid-path/case-studies.json'); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error); | ||
} | ||
}); | ||
|
||
it('should throw an error when YAML content is invalid', async () => { | ||
// Create an invalid YAML file | ||
const invalidYaml = ` | ||
invalid: yaml: content | ||
`; | ||
await fs.writeFile(path.join(tempConfigDir, 'invalid.yml'), invalidYaml); | ||
|
||
try { | ||
await buildCaseStudiesList(tempConfigDir, tempOutputFile); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toContain("Invalid content format"); // Error for invalid YAML content | ||
} | ||
}); | ||
|
||
|
||
}); |
This file contains 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,105 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const buildFinanceInfoList = require('../../scripts/finance/index'); | ||
const { expensesYaml, expensesLinkYaml, expensesjson, expensesLinkjson } = require("../fixtures/financeData") | ||
|
||
describe('buildFinanceInfoList', () => { | ||
const testDir = path.resolve(__dirname, 'test-finance-info'); | ||
const configDir = 'config'; | ||
const financeDir = 'finance'; | ||
const year = '2024'; | ||
const jsonDataDir = 'json-data'; | ||
|
||
beforeAll(() => { | ||
// Create test directory structure | ||
fs.mkdirSync(path.resolve(testDir, configDir, financeDir, year), { recursive: true }); | ||
|
||
fs.writeFileSync(path.resolve(testDir, configDir, financeDir, year, 'Expenses.yml'), expensesYaml); | ||
fs.writeFileSync(path.resolve(testDir, configDir, financeDir, year, 'ExpensesLink.yml'), expensesLinkYaml); | ||
}); | ||
|
||
afterAll(() => { | ||
// Clean up test directory | ||
fs.rmSync(testDir, { recursive: true, force: true }); | ||
}); | ||
|
||
it('should create JSON files from YAML files', async () => { | ||
await buildFinanceInfoList({ | ||
currentDir: testDir, | ||
configDir, | ||
financeDir, | ||
year, | ||
jsonDataDir | ||
}); | ||
|
||
const jsonDir = path.resolve(testDir, configDir, financeDir, jsonDataDir, year); | ||
|
||
// Check if JSON directory was created | ||
expect(fs.existsSync(jsonDir)).toBe(true); | ||
|
||
// Check if JSON files were created | ||
const expensesJsonPath = path.resolve(jsonDir, 'Expenses.json'); | ||
const expensesLinkJsonPath = path.resolve(jsonDir, 'ExpensesLink.json'); | ||
|
||
expect(fs.existsSync(expensesJsonPath)).toBe(true); | ||
expect(fs.existsSync(expensesLinkJsonPath)).toBe(true); | ||
|
||
// Check contents of JSON files | ||
const expensesJson = JSON.parse(fs.readFileSync(expensesJsonPath, 'utf8')); | ||
const expensesLinkJson = JSON.parse(fs.readFileSync(expensesLinkJsonPath, 'utf8')); | ||
|
||
expect(expensesJson).toEqual(expensesjson); | ||
expect(expensesLinkJson).toEqual(expensesLinkjson); | ||
}); | ||
|
||
it('should throw an error if YAML files are not found', async () => { | ||
try { | ||
await buildFinanceInfoList({ | ||
currentDir: testDir, | ||
configDir, | ||
financeDir, | ||
year: '2023', // Non-existent year | ||
jsonDataDir | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toMatch(/ENOENT/); // Expecting a "no such file or directory" error | ||
} | ||
}); | ||
|
||
it('should throw an error if JSON directory creation fails', async () => { | ||
try { | ||
await buildFinanceInfoList({ | ||
currentDir: testDir, | ||
configDir, | ||
financeDir, | ||
year, | ||
jsonDataDir: 'nonexistent-dir' // Invalid JSON data directory path | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toMatch(/ENOENT/); // Expecting a "no such file or directory" error | ||
} | ||
}); | ||
|
||
it('should throw an error if YAML content is invalid', async () => { | ||
// Write invalid YAML content | ||
const invalidYaml = ` | ||
invalid yaml content | ||
`; | ||
fs.writeFileSync(path.resolve(testDir, configDir, financeDir, year, 'InvalidExpenses.yml'), invalidYaml); | ||
|
||
try { | ||
await buildFinanceInfoList({ | ||
currentDir: testDir, | ||
configDir, | ||
financeDir, | ||
year, | ||
jsonDataDir | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(Error); | ||
expect(error.message).toMatch(/YAMLException/); // Expecting a YAML parsing error | ||
} | ||
}); | ||
}); |
This file contains 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,21 @@ | ||
module.exports = { | ||
yaml1: ` | ||
title: case study 1 | ||
description: random data | ||
`, | ||
|
||
yaml2:` | ||
title: case study 2 | ||
description: again random data | ||
`, | ||
|
||
json1: { | ||
title: 'case study 1', | ||
description: 'random data' | ||
}, | ||
|
||
json2: { | ||
title: 'case study 2', | ||
description: 'again random data' | ||
} | ||
} |
This file contains 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,6 @@ | ||
module.exports = { | ||
expensesYaml: 'expenses:\n - name: Test Expense\n amount: 100', | ||
expensesLinkYaml: 'links:\n - name: Test Link\n url: http://asyncapi.com', | ||
expensesjson: { expenses: [{ name: 'Test Expense', amount: 100 }] }, | ||
expensesLinkjson: { links: [{ name: 'Test Link', url: 'http://asyncapi.com' }] } | ||
} |