|
| 1 | +import os |
| 2 | +from datetime import date |
| 3 | +from glob import glob |
| 4 | + |
| 5 | +import yaml |
| 6 | + |
| 7 | +from matbench_discovery import ROOT |
| 8 | + |
| 9 | +MODEL_DIRS = glob(f"{ROOT}/models/*/") |
| 10 | + |
| 11 | + |
| 12 | +def test_model_dirs_have_metadata() -> None: |
| 13 | + required = ( |
| 14 | + "authors", |
| 15 | + "date_added", |
| 16 | + "matbench_discovery_version", |
| 17 | + "model_name", |
| 18 | + "model_version", |
| 19 | + "repo", |
| 20 | + ) |
| 21 | + for model_dir in MODEL_DIRS: |
| 22 | + md_file = f"{model_dir}metadata.yml" |
| 23 | + assert os.path.isfile(md_file), f"Missing metadata file: {md_file}" |
| 24 | + |
| 25 | + # make sure all required keys are non-empty |
| 26 | + with open(md_file) as yml_file: |
| 27 | + metadata = yaml.full_load(yml_file) |
| 28 | + |
| 29 | + for key in required: |
| 30 | + assert metadata.get(key), f"Empty {key=} in {md_file}" |
| 31 | + |
| 32 | + authors, date_added, mbd_version, model_name, model_version, repo = ( |
| 33 | + metadata[key] for key in required |
| 34 | + ) |
| 35 | + |
| 36 | + # make sure all keys are valid |
| 37 | + assert ( |
| 38 | + 3 < len(model_name) < 50 |
| 39 | + ), f"Invalid {model_name=} not between 3 and 50 characters" |
| 40 | + assert ( |
| 41 | + 1 < len(model_version) < 15 |
| 42 | + ), f"Invalid {model_version=} not between 1 and 15 characters" |
| 43 | + # TODO increase max version when releasing new versions |
| 44 | + assert ( |
| 45 | + 1 <= mbd_version <= 1 |
| 46 | + ), f"Invalid matbench-discovery version: {mbd_version}" |
| 47 | + assert isinstance(date_added, date), f"Invalid {date_added=} not a string" |
| 48 | + assert ( |
| 49 | + isinstance(authors, list) and 1 < len(authors) < 30 |
| 50 | + ), "authors not list or not between 1 and 30 authors" |
| 51 | + assert repo.startswith( |
| 52 | + "https://" |
| 53 | + ), f"Invalid {repo=} not starting with https://" |
| 54 | + |
| 55 | + |
| 56 | +def test_model_dirs_have_test_scripts() -> None: |
| 57 | + for model_dir in MODEL_DIRS: |
| 58 | + test_scripts = glob(f"{model_dir}*test_*.py") |
| 59 | + test_nbs = glob(f"{model_dir}*test_*.ipynb") |
| 60 | + assert len(test_scripts + test_nbs) > 0, f"Missing test file in {model_dir}" |
0 commit comments