devicetype-library/tests/test_definitions.py
2019-12-19 13:23:05 -05:00

46 lines
1.2 KiB
Python

import glob
import json
import pytest
import yaml
from jsonschema import validate
from jsonschema.exceptions import ValidationError
def _get_definition_files():
"""
Return a list of all definition files.
"""
return [f for f in glob.glob("device-types/**/*.yaml", recursive=True)]
# Initialize schema
with open("tests/schema.json") as schema_file:
schema = json.loads(schema_file.read())
def test_environment():
"""
Run basic sanity checks on the environment to ensure tests are running correctly.
"""
# Validate that definition files exist
assert _get_definition_files(), "No definition files found!"
# Validate that the schema exists
assert schema, "Schema definition is empty!"
@pytest.mark.parametrize("file_path", _get_definition_files())
def test_definition(file_path):
"""
Validate DeviceType definitions using the provided JSON schema.
"""
# Read file
with open(file_path) as definition_file:
definition = yaml.load(definition_file.read(), Loader=yaml.SafeLoader)
# Run validation
try:
validate(definition, schema=schema)
except ValidationError as e:
pytest.fail(f"{file_path} failed validation: {e}")