Skip to content
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

feat: replace remaining non-examples with unit tests #104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions extensions/aerial-photo/non-examples/no_run.json

This file was deleted.

26 changes: 0 additions & 26 deletions extensions/aerial-photo/non-examples/no_sequence_number.json

This file was deleted.

27 changes: 0 additions & 27 deletions extensions/aerial-photo/non-examples/run_integer.json

This file was deleted.

This file was deleted.

103 changes: 103 additions & 0 deletions extensions/aerial-photo/tests/aerial_photo_item.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import o from 'ospec';
import Ajv from 'ajv';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { promises as fs } from 'fs';
import { AjvOptions } from '../../validation.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const schemaPath = join(__dirname, '..', 'schema.json');
const exampleItemPath = join(__dirname, '..', 'examples/item.json');

o.spec('aerial-photo-item', () => {
let validate;
const ajv = new Ajv(AjvOptions);

o.before(async () => {
const data = JSON.parse(await fs.readFile(schemaPath));
validate = await ajv.compileAsync(data);
});

o('Item should pass validation', async () => {
// given
const aerialPhotoItemExample = JSON.parse(await fs.readFile(exampleItemPath));

// when
let valid = validate(aerialPhotoItemExample);

// then
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
});

o("Item with an incorrect 'aerial-photo:run' field should fail validation", async () => {
// given
const aerialPhotoItemExample = JSON.parse(await fs.readFile(exampleItemPath));
aerialPhotoItemExample.properties['aerial-photo:run'] = 1234;

// when
let valid = validate(aerialPhotoItemExample);

// then
o(valid).equals(false);
o(
validate.errors.some(
(error) => error.dataPath === ".properties['aerial-photo:run']" && error.message === 'should be string',
),
).equals(true)(JSON.stringify(validate.errors));
});

o("Item without the mandatory 'aerial-photo:run' field should fail validation", async () => {
// given
const aerialPhotoItemExample = JSON.parse(await fs.readFile(exampleItemPath));
delete aerialPhotoItemExample.properties['aerial-photo:run'];

// when
let valid = validate(aerialPhotoItemExample);

// then
o(valid).equals(false);
o(
validate.errors.some(
(error) =>
error.dataPath === '.properties' && error.message === "should have required property '['aerial-photo:run']'",
),
).equals(true)(JSON.stringify(validate.errors));
});

o("Item without a mandatory 'aerial-photo:sequence_number' field should fail validation", async () => {
// given
const aerialPhotoItemExample = JSON.parse(await fs.readFile(exampleItemPath));
delete aerialPhotoItemExample.properties['aerial-photo:sequence_number'];

// when
let valid = validate(aerialPhotoItemExample);

// then
o(valid).equals(false);
o(
validate.errors.some(
(error) =>
error.dataPath === '.properties' &&
error.message === "should have required property '['aerial-photo:sequence_number']'",
),
).equals(true)(JSON.stringify(validate.errors));
});

o("Item with an incorrect 'aerial-photo:sequence_number' field should fail validation", async () => {
// given
const aerialPhotoItemExample = JSON.parse(await fs.readFile(exampleItemPath));
aerialPhotoItemExample.properties['aerial-photo:sequence_number'] = 'incorrect_example';

// when
let valid = validate(aerialPhotoItemExample);

// then
o(valid).equals(false);
o(
validate.errors.some(
(error) =>
error.dataPath === ".properties['aerial-photo:sequence_number']" && error.message === 'should be integer',
),
).equals(true)(JSON.stringify(validate.errors));
});
});
19 changes: 11 additions & 8 deletions extensions/camera/tests/camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,29 @@ o.spec('camera', () => {

o('camera-validates-successfully', async () => {
// given
const camera_item_example = JSON.parse(await fs.readFile(examplePath));
const cameraItemExample = JSON.parse(await fs.readFile(examplePath));

// when
let valid = validate(camera_item_example);
let valid = validate(cameraItemExample);

// then
o(valid).equals(true)(JSON.stringify(validate.errors, null, 2));
});

o('camera-validation-fails', async () => {
o("Item with an incorrect 'camera:sequence_number' field should fail validation", async () => {
// given
const camera_item_example = JSON.parse(await fs.readFile(examplePath));
camera_item_example.properties['camera:sequence_number'] = 'incorrect_value';
const cameraItemExample = JSON.parse(await fs.readFile(examplePath));
cameraItemExample.properties['camera:sequence_number'] = 'incorrect_value';

// when
let valid = validate(camera_item_example);
let valid = validate(cameraItemExample);

// then
o(valid).equals(false);
o(validate.errors.length).equals(1);
o(validate.errors[0].message).equals('should be integer');
o(
validate.errors.some(
(error) => error.dataPath === ".properties['camera:sequence_number']" && error.message === 'should be integer',
),
).equals(true)(JSON.stringify(validate.errors));
});
});
26 changes: 0 additions & 26 deletions extensions/film/non-examples/film_id_integer.json

This file was deleted.

26 changes: 0 additions & 26 deletions extensions/film/non-examples/negative_sequence_string.json

This file was deleted.

25 changes: 0 additions & 25 deletions extensions/film/non-examples/no_film_id.json

This file was deleted.

25 changes: 0 additions & 25 deletions extensions/film/non-examples/no_negative_sequence.json

This file was deleted.

Loading