|
| 1 | +import o from 'ospec'; |
| 2 | +import Ajv from 'ajv'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { promises as fs } from 'fs'; |
| 5 | +import { AjvOptions, rootDirectory, schemaPath } from './validation.js'; |
| 6 | + |
| 7 | +const examplePath = join(rootDirectory, 'examples/collection.json'); |
| 8 | + |
| 9 | +const propertyNames = [ |
| 10 | + 'processing:expression', |
| 11 | + 'processing:facility', |
| 12 | + 'processing:level', |
| 13 | + 'processing:lineage', |
| 14 | + 'processing:software', |
| 15 | +]; |
| 16 | + |
| 17 | +o.spec('Collection', () => { |
| 18 | + let validate; |
| 19 | + const ajv = new Ajv(AjvOptions); |
| 20 | + |
| 21 | + o.before(async () => { |
| 22 | + const data = JSON.parse(await fs.readFile(schemaPath)); |
| 23 | + validate = await ajv.compileAsync(data); |
| 24 | + }); |
| 25 | + |
| 26 | + o('Example should pass validation', async () => { |
| 27 | + // given |
| 28 | + const example = JSON.parse(await fs.readFile(examplePath)); |
| 29 | + |
| 30 | + // when |
| 31 | + let valid = validate(example); |
| 32 | + |
| 33 | + // then |
| 34 | + o(valid).equals(true)(JSON.stringify(validate.errors, null, 2)); |
| 35 | + }); |
| 36 | + |
| 37 | + o("Example without any provider processing: values should fail validation", async () => { |
| 38 | + // given |
| 39 | + const example = JSON.parse(await fs.readFile(examplePath)); |
| 40 | + for (const propertyName of propertyNames) { |
| 41 | + delete example['providers'][0][propertyName]; |
| 42 | + } |
| 43 | + |
| 44 | + // when |
| 45 | + let valid = validate(example); |
| 46 | + |
| 47 | + // then |
| 48 | + o(valid).equals(false); |
| 49 | + for (const propertyName of propertyNames) { |
| 50 | + o( |
| 51 | + validate.errors.some( |
| 52 | + (error) => |
| 53 | + error.dataPath === ".providers[0]" && |
| 54 | + error.message === `should have required property '['${propertyName}']'`, |
| 55 | + ), |
| 56 | + ).equals(true)(JSON.stringify(validate.errors)); |
| 57 | + } |
| 58 | + }); |
| 59 | +}); |
0 commit comments