-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add schema validation support for tests #215
Closed
Closed
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5f44064
Add schema validation support for tests
b1183ef
Add schema validation support for tests
0acaf99
Update discover-granules schema.
bf1096a
Merge branch 'UpdateDiscoverGranules' of https://github.com/cumulus-n…
7abc00e
Merge branch 'message-updates' into UpdateDiscoverGranules
058ea93
Updates based on PR feedback
4a8bf61
Merge branch 'UpdateDiscoverGranules' of https://github.com/cumulus-n…
14fb10e
Remove queueing from pdr.js
fa5d0b5
Remove combined granule discovery and queueing
8212d1f
Fixed the parse-pdr task
048058d
Get discover-pdrs tests working
9f4bd27
Serve temporary test data out of one directory
2dbec89
Remove http-test-data directory
89677f9
Merge branch 'message-updates' into UpdateDiscoverGranules
51d8a7e
Fix race condition in parse-pdr
935e8a7
Update granule.js to use random temp directory
dd2c48e
Get sync-granule tests working
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 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 |
---|---|---|
|
@@ -76,6 +76,7 @@ | |
"webpack-node-externals": "^1.5.4" | ||
}, | ||
"devDependencies": { | ||
"ajv": "^5.5.2", | ||
"ava": "^0.25.0" | ||
} | ||
} |
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,8 +1,10 @@ | ||
'use strict'; | ||
|
||
const Ajv = require('ajv'); | ||
const crypto = require('crypto'); | ||
const url = require('url'); | ||
const aws = require('./aws'); | ||
const { readFile } = require('fs'); | ||
|
||
/** | ||
* Generate a 40-character random string | ||
|
@@ -107,3 +109,83 @@ async function createQueue() { | |
return url.format(returnedQueueUrl); | ||
} | ||
exports.createQueue = createQueue; | ||
|
||
/** | ||
* Read a file and return a promise with the data | ||
* | ||
* Takes the same parameters as fs.readFile: | ||
* | ||
* https://nodejs.org/docs/v6.10.3/api/fs.html#fs_fs_readfile_file_options_callback | ||
* | ||
* @param {string|Buffer|integer} file - filename or file descriptor | ||
* @param {any} options - encoding and flag options | ||
* @returns {Promise} - the contents of the file | ||
*/ | ||
function promisedReadFile(file, options) { | ||
return new Promise((resolve, reject) => { | ||
readFile(file, options, (err, data) => { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Validate an object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {string} schemaFilename - the filename of the schema | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateJSON(t, schemaFilename, data) { | ||
const schema = await promisedReadFile(schemaFilename, 'utf8').then(JSON.parse); | ||
const ajv = new Ajv(); | ||
const valid = (new Ajv()).validate(schema, data); | ||
if (!valid) t.fail(`input validation failed: ${ajv.errorsText()}`); | ||
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.
|
||
return valid; | ||
} | ||
|
||
/** | ||
* Validate a task input object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateInput(t, data) { | ||
return validateJSON(t, './schemas/input.json', data); | ||
} | ||
exports.validateInput = validateInput; | ||
|
||
/** | ||
* Validate a task config object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateConfig(t, data) { | ||
return validateJSON(t, './schemas/config.json', data); | ||
} | ||
exports.validateConfig = validateConfig; | ||
|
||
/** | ||
* Validate a task output object using json-schema | ||
* | ||
* Issues a test failure if there were validation errors | ||
* | ||
* @param {Object} t - an ava test | ||
* @param {Object} data - the object to be validated | ||
* @returns {boolean} - whether the object is valid or not | ||
*/ | ||
async function validateOutput(t, data) { | ||
return validateJSON(t, './schemas/output.json', data); | ||
} | ||
exports.validateOutput = validateOutput; |
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.
What happens if the schema file does not exist?
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.
The assumption is that you would only add this check to your test if the schema file exists.
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.
It might be nice to just comment the expected behavior or spit out an error in that case. I don't love that the schema locations are assumed, but think it's fine for now and we can make updates if we run into a different scenario.