Skip to content

Commit 060ae37

Browse files
l0b0Mitchell Paff
andcommitted
feat: Add unit tests
Co-authored-by: Mitchell Paff <[email protected]> Co-authored-by: Victor Engmark <[email protected]>
1 parent ed66517 commit 060ae37

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"name": "stac-extensions",
33
"version": "1.0.0",
44
"scripts": {
5-
"test": "npm run check-markdown && npm run check-examples",
5+
"test": "jest && npm run check-markdown && npm run check-examples",
66
"check-markdown": "remark . -f -r .github/remark.yaml",
77
"check-examples": "stac-node-validator . --lint --verbose --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json",
88
"format-examples": "stac-node-validator . --format --schemaMap https://stac-extensions.github.io/template/v1.0.0/schema.json=./json-schema/schema.json"
99
},
10+
"type": "module",
1011
"dependencies": {
12+
"jest": "^27.4.4",
1113
"remark-cli": "^8.0.0",
1214
"remark-lint": "^7.0.0",
1315
"remark-lint-no-html": "^2.0.0",

tests/collection.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { join } = require('path');
2+
const { promises } = require('fs');
3+
const { AjvOptions, rootDirectory, schemaPath } = require('./validation.js');
4+
const ajv = new (require('ajv'))(AjvOptions);
5+
6+
const examplePath = join(rootDirectory, 'examples/collection.json');
7+
8+
let validate;
9+
beforeAll(async () => {
10+
const data = JSON.parse(await promises.readFile(schemaPath));
11+
validate = await ajv.compileAsync(data);
12+
});
13+
14+
describe('Collection example', () => {
15+
it('should pass validation', async () => {
16+
// given
17+
const example = JSON.parse(await promises.readFile(examplePath));
18+
19+
// when
20+
let valid = validate(example);
21+
22+
// then
23+
expect(valid).toBeTruthy();
24+
});
25+
26+
it('should fail validation without mandatory template:new_field field', async () => {
27+
// given
28+
const example = JSON.parse(await promises.readFile(examplePath));
29+
delete example['assets'];
30+
delete example['item_assets'];
31+
delete example['template:new_field'];
32+
33+
// when
34+
let valid = validate(example);
35+
36+
// then
37+
expect(valid).toBeFalsy();
38+
expect(
39+
validate.errors.some((error) => error.message === "must have required property 'template:new_field'"),
40+
).toBeTruthy();
41+
});
42+
});

tests/item.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { join } = require('path');
2+
const { promises } = require('fs');
3+
const { AjvOptions, rootDirectory, schemaPath } = require('./validation.js');
4+
const ajv = new (require('ajv'))(AjvOptions);
5+
6+
const examplePath = join(rootDirectory, 'examples/item.json');
7+
8+
let validate;
9+
beforeAll(async () => {
10+
const data = JSON.parse(await promises.readFile(schemaPath));
11+
validate = await ajv.compileAsync(data);
12+
});
13+
14+
describe('Item example', () => {
15+
it('should pass validation', async () => {
16+
// given
17+
const example = JSON.parse(await promises.readFile(examplePath));
18+
19+
// when
20+
let valid = validate(example);
21+
22+
// then
23+
expect(valid).toBeTruthy();
24+
});
25+
26+
it('should fail validation without mandatory template:new_field property ', async () => {
27+
// given
28+
const example = JSON.parse(await promises.readFile(examplePath));
29+
delete example.properties['template:new_field'];
30+
// when
31+
let valid = validate(example);
32+
33+
// then
34+
expect(valid).toBeFalsy();
35+
expect(
36+
validate.errors.some((error) => error.message === "must have required property 'template:new_field'"),
37+
).toBeTruthy();
38+
});
39+
});

tests/validation.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const axios = require('axios');
2+
const { dirname, join } = require('path');
3+
const iriFormats = require('stac-node-validator/iri.js');
4+
5+
// const directory = dirname(fileURLToPath(import.meta.url));
6+
7+
const Schemas = new Map();
8+
const loadSchema = function (uri) {
9+
let existing = Schemas.get(uri);
10+
if (existing == null) {
11+
existing = loadSchemaFromUri(uri);
12+
Schemas.set(uri, existing);
13+
}
14+
return existing;
15+
}
16+
17+
/**
18+
* function passed in to Ajv instance which allows us to load schemas from a url at run time.
19+
*/
20+
module.exports.loadSchemaFromUri = async function (uri) {
21+
try {
22+
let response = await axios.get(uri);
23+
return response.data;
24+
} catch (error) {
25+
throw new Error(`-- Schema at '${uri}' not found. Please ensure all entries in 'stac_extensions' are valid.`);
26+
}
27+
}
28+
29+
module.exports.AjvOptions = {loadSchema, formats: Object.assign(iriFormats)};
30+
module.exports.rootDirectory = dirname(__dirname);
31+
module.exports.schemaPath = join(module.exports.rootDirectory, 'json-schema/schema.json');

0 commit comments

Comments
 (0)