-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
executable file
·41 lines (29 loc) · 1.13 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
"""Tests for schema which runs validation using schema over examples folder."""
# pylint: disable=redefined-outer-name
import json
from pathlib import Path
import jsonschema
import pytest
from ruamel.yaml import YAML
yaml = YAML(typ="safe")
root = Path(__file__).resolve().parent
examples_dir = root / "examples"
valid_dir = examples_dir / "valid"
invalid_dir = examples_dir / "invalid"
@pytest.fixture
def schema() -> dict:
return json.loads((root / "schema.json").read_text(encoding="utf-8"))
def ids_gen(val: str) -> str:
if not isinstance(val, Path):
return str(val)
if val.name.endswith(".dvc.yaml"):
return val.name[:-9]
return val.name
@pytest.mark.parametrize("example", valid_dir.iterdir(), ids=ids_gen)
def test_valid_examples(schema: dict, example: Path) -> None:
jsonschema.validate(yaml.load(example), schema=schema)
@pytest.mark.parametrize("example", invalid_dir.iterdir(), ids=ids_gen)
def test_invalid_examples(schema: dict, example: Path) -> None:
with pytest.raises(jsonschema.ValidationError):
jsonschema.validate(yaml.load(example), schema=schema)