llama-hub/tests/test_library.py
Jesse Zhang 3c497ac430
Better tests, including download_loader (#36)
* Better tests including download_loader

* Fix file name

* Import loaders locally

* Fix relative imports

* Update sys path

* Update sys path

* Import path

* Import path
2023-02-15 17:18:21 -08:00

38 lines
1.3 KiB
Python

"""Check that the library is working as expected."""
import json
from importlib import util
from pathlib import Path
def test_library_matches() -> None:
"""Check that library.json corresponds to valid files."""
hub_dir = Path(__file__).parent.parent / "loader_hub"
library_path = hub_dir / "library.json"
library_dict = json.load(open(library_path, "r"))
for k, entry in library_dict.items():
# make sure every entry has an "id" field
assert "id" in entry
entry_id = entry["id"]
# make sure the loader directory exists
entry_dir = hub_dir / entry_id
assert entry_dir.exists()
# make sure that the loader file exists
entry_file = entry_dir / "base.py"
assert entry_file.exists()
# make sure that the README file exists
readme_file = entry_dir / "README.md"
assert readme_file.exists()
spec = util.spec_from_file_location("custom_loader", location=str(entry_file))
if spec is None:
raise ValueError(f"Could not find file: {str(entry_file)}.")
module = util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore
# make sure the specified class is in the loader file
assert hasattr(module, k)