|
| 1 | +"""Tests to verify that there is a data yaml file for each entry in the catalog-nk.yml and catalog-n2.yml files, |
| 2 | +and that all files can be parsed and conform to the expected schema. |
| 3 | +
|
| 4 | +This test can be run by the command |
| 5 | +
|
| 6 | + pytest tests/test_yaml.py |
| 7 | +
|
| 8 | +or simply `pytest` from the repo directory. (The latter will automatically discover |
| 9 | +all test files.) |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import pathlib |
| 15 | +import re |
| 16 | + |
| 17 | +import pytest |
| 18 | +import yaml |
| 19 | +from cerberus import Validator |
| 20 | + |
| 21 | + |
| 22 | +# Extract all YAML paths from the catalog |
| 23 | +def extract_paths(catalog: str | list, base_path: str = "") -> list: |
| 24 | + paths = [] |
| 25 | + for item in catalog: |
| 26 | + if isinstance(item, dict): |
| 27 | + for key, value in item.items(): |
| 28 | + if key == "data": |
| 29 | + paths.append(base_path + value) |
| 30 | + elif key == "content": |
| 31 | + paths.extend(extract_paths(value, base_path)) |
| 32 | + elif key == "DIVIDER": |
| 33 | + continue |
| 34 | + else: |
| 35 | + paths.extend(extract_paths([value], base_path)) |
| 36 | + return paths |
| 37 | + |
| 38 | + |
| 39 | +# Path to the catalog files |
| 40 | +CATALOG_ROOT_PATH = pathlib.Path(__file__).resolve().parent.parent / "database" |
| 41 | +CATALOG_NK_PATH = CATALOG_ROOT_PATH / "catalog-nk.yml" |
| 42 | +CATALOG_N2_PATH = CATALOG_ROOT_PATH / "catalog-n2.yml" |
| 43 | + |
| 44 | +# Load the catalog files |
| 45 | +with open(CATALOG_NK_PATH, "r", encoding="utf-8") as stream: |
| 46 | + catalog_nk = yaml.safe_load(stream) |
| 47 | + |
| 48 | +with open(CATALOG_N2_PATH, "r", encoding="utf-8") as stream: |
| 49 | + catalog_n2 = yaml.safe_load(stream) |
| 50 | + |
| 51 | +# Get all paths from the catalogs |
| 52 | +ALL_NK_PATHS = extract_paths(catalog_nk) |
| 53 | +ALL_N2_PATHS = extract_paths(catalog_n2) |
| 54 | + |
| 55 | +# Define the regex pattern for URLs, this is used to validate the URLs in the YAML files |
| 56 | +url_regex = re.compile( |
| 57 | + r"(?i)^(?:http|ftp)s?://" # http:// or https:// also ignore case |
| 58 | + r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain... |
| 59 | + r"localhost|" # localhost... |
| 60 | + r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|" # ...or ipv4 |
| 61 | + r"\[?[A-F0-9]*:[A-F0-9:]+\]?)" # ...or ipv6 |
| 62 | + r"(?::\d+)?" # optional port |
| 63 | + r"(?:/?|[/?]\S+)$" |
| 64 | +) |
| 65 | + |
| 66 | +# Define the schema for the YAML nk data files. This could be extended to test other levels of the yaml files and/or test |
| 67 | +# the content of each field more thoroughly. E.g. check that temperature is a number, that 'tabulated n' is a list with two columns, etc. |
| 68 | +schema = { |
| 69 | + "REFERENCES": {"type": "string", "required": True}, |
| 70 | + "COMMENTS": {"type": "string", "required": False}, |
| 71 | + "DATA": {"type": "list", "required": True}, |
| 72 | + "CONDITIONS": { |
| 73 | + "oneof": [{"type": "string"}, {"type": "list"}, {"type": "dict"}], |
| 74 | + "required": False, |
| 75 | + }, |
| 76 | + "PROPERTIES": { |
| 77 | + "oneof": [{"type": "string"}, {"type": "list"}, {"type": "dict"}], |
| 78 | + "required": False, |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +validator = Validator(schema) |
| 83 | + |
| 84 | +# Define the schema for the 'about.yaml' files |
| 85 | +about_scheme = { |
| 86 | + "NAMES": {"type": "list", "schema": {"type": "string"}}, |
| 87 | + "ABOUT": {"type": "string"}, |
| 88 | + "LINKS": { |
| 89 | + "type": "list", |
| 90 | + "schema": { |
| 91 | + "type": "dict", |
| 92 | + "schema": { |
| 93 | + "url": {"type": "string", "regex": url_regex.pattern}, |
| 94 | + "text": {"type": "string"}, |
| 95 | + }, |
| 96 | + }, |
| 97 | + "required": False, |
| 98 | + }, |
| 99 | +} |
| 100 | +validator_about = Validator(about_scheme) |
| 101 | + |
| 102 | +# Discover the paths for all `.yml` files. |
| 103 | +DATABASE_PATH = pathlib.Path(__file__).resolve().parent.parent / "database" / "data" |
| 104 | +ALL_YAML_FILES = list(DATABASE_PATH.rglob("*.yml")) |
| 105 | + |
| 106 | + |
| 107 | +# Verify that each about.yml file conforms to the expected schema |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "yaml_file", |
| 110 | + ALL_YAML_FILES, |
| 111 | + ids=lambda x: str(x).replace(str(DATABASE_PATH), "")[1:].replace(".yml", ""), |
| 112 | +) |
| 113 | +def test_yaml_schema(yaml_file): |
| 114 | + with open(yaml_file, "r") as file: |
| 115 | + data = yaml.safe_load(file) |
| 116 | + if yaml_file.name == "about.yml": |
| 117 | + assert validator_about.validate(data), ( |
| 118 | + f"Schema validation failed for {yaml_file.name}: {validator_about.errors}" |
| 119 | + ) |
| 120 | + else: |
| 121 | + assert validator.validate(data), ( |
| 122 | + f"Schema validation failed for {yaml_file.name}: {validator.errors}" |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +# Verify that each yaml path referenced in the catalog files exists in the database/data directory |
| 127 | +@pytest.mark.parametrize( |
| 128 | + "path", |
| 129 | + ALL_NK_PATHS + ALL_N2_PATHS, |
| 130 | + ids=lambda x: str(x).replace(str(DATABASE_PATH), "").replace(".yml", ""), |
| 131 | +) |
| 132 | +def test_paths_exist(path): |
| 133 | + full_path = ( |
| 134 | + pathlib.Path(__file__).resolve().parent.parent / "database" / "data" / path |
| 135 | + ) |
| 136 | + assert full_path.exists(), f"Path does not exist: {full_path}" |
0 commit comments