-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from ciatph/dev
v1.3.7
- Loading branch information
Showing
13 changed files
with
163 additions
and
28 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
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,63 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const ExcelFactory = require('../../src/classes/excelfactory') | ||
const Schema = require('../../src/classes/schema') | ||
|
||
const ColorLog = require('../../src/classes/colorlog') | ||
const logger = new ColorLog({ color: ColorLog.COLORS.TEXT.YELLOW }) | ||
|
||
const checkClass = require('../classInitialization/checkClass') | ||
const outputSchema = require('../../src/lib/schemas/outputSchema') | ||
const { arrayToString } = require('../../src/lib/utils') | ||
|
||
/* eslint-disable no-undef */ | ||
describe('Output JSON file format test', () => { | ||
const jsonFileName = 'municipalities.json' | ||
const pathToOutputFile = path.join(__dirname, jsonFileName) | ||
|
||
let jsonDataContent | ||
|
||
it('should write municipalities data to a JSON file', () => { | ||
// ExcelFactory is a shorthand for the ExcelFile class when using only the default settings | ||
const excelFile = new ExcelFactory() | ||
|
||
const regions = excelFile.listRegions() | ||
const regionName = regions[0] | ||
const provinces = excelFile.listProvinces(regionName) | ||
|
||
let msg = `Sample region: ${regionName}\n` | ||
msg += `Provinces: ${arrayToString(provinces)}\n\n` | ||
msg += `Writing municipalities list to "${jsonFileName}"...` | ||
logger.log(msg) | ||
|
||
// Write the municipalities of a region to a JSON file | ||
excelFile.writeMunicipalities({ | ||
fileName: pathToOutputFile, | ||
provinces | ||
}) | ||
|
||
// Read the JSON file from disk | ||
jsonDataContent = fs.readFileSync(pathToOutputFile, { encoding: 'utf8' }) | ||
|
||
logger.log(`JSON data read from the "${jsonFileName}" file:`) | ||
logger.log(jsonDataContent) | ||
|
||
expect(jsonDataContent).toBeDefined() | ||
|
||
checkClass({ | ||
excelInstance: excelFile, | ||
classType: ExcelFactory | ||
}) | ||
}) | ||
|
||
it('should follow the defined output format', () => { | ||
// Validate schema of the output JSON file | ||
const schema = new Schema( | ||
JSON.parse(jsonDataContent), | ||
outputSchema | ||
) | ||
|
||
expect(schema).toBeDefined() | ||
}) | ||
}) |
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,22 @@ | ||
require('dotenv').config() | ||
const path = require('path') | ||
|
||
const ExcelFile = require('../../src/classes/excel') | ||
|
||
/* eslint-disable no-undef */ | ||
describe('Regions listing test', () => { | ||
it('should list region names from config file', () => { | ||
const excelFile = new ExcelFile({ | ||
pathToFile: path.join(__dirname, '..', '..', 'data', 'day1.xlsx') | ||
}) | ||
|
||
const regions = excelFile | ||
.listRegions() | ||
.map(region => ({ 'Region Name': region })) | ||
|
||
console.table(regions) | ||
|
||
expect(regions).toBeDefined() | ||
expect(Array.isArray(regions)).toBe(true) | ||
}) | ||
}) |
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,11 @@ | ||
const ColorLog = require('./colorlog') | ||
const ExcelFile = require('./excel') | ||
const ExcelFactory = require('./excelfactory') | ||
const Schema = require('./schema') | ||
|
||
module.exports = { | ||
ColorLog, | ||
ExcelFile, | ||
ExcelFactory, | ||
Schema | ||
} |
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,32 @@ | ||
const { z } = require('zod') | ||
|
||
// Province name schema | ||
const provinceSchema = z.string().max(40) | ||
|
||
// JSON output schema | ||
const outputSchema = z.object({ | ||
metadata: z.object({ | ||
source: z.string( | ||
// URL link to local or remote Excel file data source website | ||
z.string().url().max(200) | ||
), | ||
// Data set title | ||
title: z.string().max(50), | ||
// Data set description | ||
description: z.string().max(500), | ||
// Date the data set was first created | ||
date_created: z.string().max(20) | ||
}), | ||
|
||
// Keys are province names (Strings) | ||
data: z.record( | ||
// Key-values (municipality names) are arrays of strings | ||
z.array(z.string().max(40)).refine((data) => { | ||
// Each province name-key should have max length of 40 | ||
return Object.keys(data) | ||
.every(province => provinceSchema.safeParse(province).success) | ||
}) | ||
) | ||
}) | ||
|
||
module.exports = outputSchema |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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