Skip to content
Open
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
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.py]
indent_style = space
indent_size = 4

[*.{yml,yaml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
24 changes: 24 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Auto detect text files and perform LF normalization
* text=auto

# Explicitly declare text files
*.md text diff=markdown
*.txt text
*.csv text
*.yml text
*.yaml text
*.json text
*.xml text
*.html text diff=html
*.css text diff=css

# Denote binary files
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.pdf binary
*.zip binary
*.gz binary
*.tar binary
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Open Connectivity Foundatation | IoT Data Models
© 2016-2022 Open Connectivity Foundatation. All rights reserved.
# Open Connectivity Foundation | IoT Data Models
© 2016-2022 Open Connectivity Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Expand Down
128 changes: 128 additions & 0 deletions scripts/test_validate_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env python3
"""Tests for validate_models.py"""

import json
import os
import tempfile
import pytest
from validate_models import (
ModelValidator,
ValidationReport,
ValidationIssue,
find_model_files,
)


@pytest.fixture
def tmp_dir():
with tempfile.TemporaryDirectory() as d:
yield d


def write_json(directory, filename, data):
path = os.path.join(directory, filename)
with open(path, "w") as f:
json.dump(data, f)
return path


class TestValidationReport:
def test_add_error(self):
report = ValidationReport()
report.add_issue(ValidationIssue("f.json", "/", "error", "test", "msg"))
assert report.total_errors == 1

def test_add_warning(self):
report = ValidationReport()
report.add_issue(ValidationIssue("f.json", "/", "warning", "test", "msg"))
assert report.total_warnings == 1


class TestModelValidator:
def test_invalid_json(self, tmp_dir):
path = os.path.join(tmp_dir, "bad.json")
with open(path, "w") as f:
f.write("{invalid}")
v = ModelValidator(tmp_dir)
v.validate_file("bad.json")
assert v.report.total_errors >= 1

def test_missing_swagger_fields(self, tmp_dir):
write_json(tmp_dir, "api.json", {"swagger": "2.0"})
v = ModelValidator(tmp_dir)
v.validate_file("api.json")
errs = [i for i in v.report.issues if i.category == "required_field"]
assert len(errs) >= 1

def test_valid_swagger(self, tmp_dir):
write_json(tmp_dir, "api.json", {
"swagger": "2.0",
"info": {"title": "Test", "version": "1.0.0"},
"paths": {}
})
v = ModelValidator(tmp_dir)
v.validate_file("api.json")
assert v.report.total_errors == 0

def test_invalid_type(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"properties": {
"name": {"type": "invalid_type"}
}
})
v = ModelValidator(tmp_dir)
v.validate_file("model.json")
errs = [i for i in v.report.issues if i.category == "type"]
assert len(errs) >= 1

def test_array_missing_items(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"properties": {
"tags": {"type": "array"}
}
})
v = ModelValidator(tmp_dir)
v.validate_file("model.json")
warns = [i for i in v.report.issues if "items" in i.message]
assert len(warns) >= 1

def test_required_prop_not_defined(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"required": ["name"],
"properties": {}
})
v = ModelValidator(tmp_dir)
v.validate_file("model.json")
errs = [i for i in v.report.issues if "Required property" in i.message]
assert len(errs) >= 1

def test_naming_convention(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"properties": {
"BadName": {"type": "string"}
}
})
v = ModelValidator(tmp_dir, naming_convention="camel")
v.validate_file("model.json")
infos = [i for i in v.report.issues if i.category == "naming"]
assert len(infos) >= 1


class TestFindModelFiles:
def test_finds_json(self, tmp_dir):
write_json(tmp_dir, "model.json", {})
files = find_model_files(tmp_dir)
assert len(files) == 1

def test_ignores_hidden(self, tmp_dir):
write_json(tmp_dir, ".hidden.json", {})
files = find_model_files(tmp_dir)
assert len(files) == 0


if __name__ == "__main__":
pytest.main([__file__, "-v"])
Loading