|
| 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 | +const propertyNames = [ |
| 9 | + 'processing:expression', |
| 10 | + 'processing:facility', |
| 11 | + 'processing:level', |
| 12 | + 'processing:lineage', |
| 13 | + 'processing:software', |
| 14 | +]; |
| 15 | + |
| 16 | +let validate; |
| 17 | +beforeAll(async () => { |
| 18 | + const data = JSON.parse(await promises.readFile(schemaPath)); |
| 19 | + validate = await ajv.compileAsync(data); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('Collection example', () => { |
| 23 | + it('should pass validation', async () => { |
| 24 | + // given |
| 25 | + const example = JSON.parse(await promises.readFile(examplePath)); |
| 26 | + |
| 27 | + // when |
| 28 | + let valid = validate(example); |
| 29 | + |
| 30 | + // then |
| 31 | + expect(valid).toBeTruthy() |
| 32 | + }); |
| 33 | + |
| 34 | + it('should fail validation with invalid providers processing:software value', async () => { |
| 35 | + // given |
| 36 | + const example = JSON.parse(await promises.readFile(examplePath)); |
| 37 | + example['providers'][0]['processing:software'] = null; |
| 38 | + |
| 39 | + // when |
| 40 | + let valid = validate(example); |
| 41 | + |
| 42 | + // then |
| 43 | + expect(valid).toBeFalsy(); |
| 44 | + expect( |
| 45 | + validate.errors.some( |
| 46 | + (error) => |
| 47 | + error.instancePath === '/providers/0/processing:software' && |
| 48 | + error.message === 'must be object', |
| 49 | + ), |
| 50 | + ).toBeTruthy(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should fail validation without any summaries processing: properties', async () => { |
| 54 | + // given |
| 55 | + const example = JSON.parse(await promises.readFile(examplePath)); |
| 56 | + for (const propertyName of propertyNames) { |
| 57 | + delete example['summaries'][propertyName]; |
| 58 | + } |
| 59 | + |
| 60 | + // when |
| 61 | + let valid = validate(example); |
| 62 | + |
| 63 | + // then |
| 64 | + expect(valid).toBeFalsy(); |
| 65 | + for (const propertyName of propertyNames) { |
| 66 | + expect( |
| 67 | + validate.errors.some( |
| 68 | + (error) => |
| 69 | + error.instancePath === "/summaries" && |
| 70 | + error.message === `must have required property '${propertyName}'`, |
| 71 | + ), |
| 72 | + ).toBeTruthy(); |
| 73 | + } |
| 74 | + }); |
| 75 | +}); |
0 commit comments