generated from br3ndonland/template-python
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So far, testing has been limited to the Python modules. However, the pyproject.toml is an important source of metadata for the project. This commit will use toml (pytest dependency) to load pyproject.toml, and Pydantic (FastAPI dependency) will be used for data validation. Unit tests will verify the presence of select fields in the pyproject.toml. It is also possible to use `importlib.metadata` to read other package metadata, as explained in python-poetry/poetry#1036. The metadata originate in pyproject.toml, and becomes available after Poetry installs the root project package. The `importlib.metadata` approach works in a virtualenv, in which the Poetry root project is installed, but is error prone in Docker, because the root project is usually not installed. Parsing the TOML is a more durable alternative, because it works even if the root project is not installed. The `importlib.metadata` version calculation in `inboard/__init__.py` was not necessary, so it will be removed.
- Loading branch information
1 parent
0bf44e3
commit ff00f0d
Showing
4 changed files
with
69 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +0,0 @@ | ||
""" | ||
inboard | ||
--- | ||
""" | ||
from importlib.metadata import version | ||
|
||
|
||
def package_version(package: str = __package__) -> str: | ||
"""Calculate version number based on pyproject.toml""" | ||
try: | ||
return version(package) | ||
except Exception: | ||
return "Package not found." | ||
|
||
|
||
__version__ = package_version() | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[mypy] | ||
disallow_untyped_defs = True | ||
files = **/*.py | ||
plugins = pydantic.mypy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,74 @@ | ||
from inboard import __version__, package_version | ||
from importlib.metadata import metadata, version | ||
from pathlib import Path | ||
from typing import Any, Dict, List | ||
|
||
current_version = "0.5.7" | ||
import toml | ||
from pydantic import BaseSettings | ||
|
||
|
||
def test_package_version() -> None: | ||
"""Test package version calculation.""" | ||
assert package_version() == current_version | ||
def load_pyproject(pyproject_path: Path = Path("pyproject.toml")) -> Dict[str, Any]: | ||
"""Load pyproject.toml into a dictionary, with a default dict as a fallback.""" | ||
try: | ||
return dict(toml.load(pyproject_path)) | ||
except Exception: | ||
return { | ||
"tool": {"poetry": {"name": "inboard", "description": "", "version": ""}} | ||
} | ||
|
||
|
||
class Settings(BaseSettings): | ||
"""Instantiate a Pydantic Settings model.""" | ||
|
||
pyproject: Dict[str, Any] = load_pyproject(pyproject_path=Path("pyproject.toml")) | ||
name: str = str(pyproject["tool"]["poetry"]["name"]) | ||
version: str = str(pyproject["tool"]["poetry"]["version"]) | ||
description: str = str(pyproject["tool"]["poetry"]["description"]) | ||
authors: List[str] = list(pyproject["tool"]["poetry"]["authors"]) | ||
license_name: str = str(pyproject["tool"]["poetry"]["license"]) | ||
homepage: str = str(pyproject["tool"]["poetry"]["homepage"]) | ||
readme: str = str(pyproject["tool"]["poetry"]["readme"]) | ||
include: List[str] = list(pyproject["tool"]["poetry"]["include"]) | ||
keywords: List[str] = list(pyproject["tool"]["poetry"]["keywords"]) | ||
classifiers: List[str] = list(pyproject["tool"]["poetry"]["classifiers"]) | ||
|
||
|
||
settings = Settings() | ||
|
||
def test_package_version_not_found() -> None: | ||
"""Test package version calculation when package is not installed.""" | ||
assert package_version(package="incorrect") == "Package not found." | ||
|
||
def test_load_pyproject() -> None: | ||
"""Assert that pyproject.toml is successfully loaded and parsed.""" | ||
pyproject = load_pyproject(pyproject_path=Path("pyproject.toml")) | ||
assert pyproject == settings.pyproject | ||
assert str(pyproject["tool"]["poetry"]["name"]) == settings.name == "inboard" | ||
assert str(pyproject["tool"]["poetry"]["version"]) == settings.version | ||
assert str(pyproject["tool"]["poetry"]["description"]) == settings.description | ||
assert list(pyproject["tool"]["poetry"]["authors"]) == settings.authors | ||
assert "Brendon Smith <br3ndonland@protonmail.com>" in settings.authors | ||
assert str(pyproject["tool"]["poetry"]["license"]) == settings.license_name == "MIT" | ||
assert str(pyproject["tool"]["poetry"]["homepage"]) == settings.homepage | ||
assert str(pyproject["tool"]["poetry"]["readme"]) == settings.readme | ||
assert list(pyproject["tool"]["poetry"]["include"]) == settings.include | ||
assert "inboard/py.typed" in settings.include | ||
assert list(pyproject["tool"]["poetry"]["keywords"]) == settings.keywords | ||
assert "fastapi" in settings.keywords | ||
assert list(pyproject["tool"]["poetry"]["classifiers"]) == settings.classifiers | ||
assert "Topic :: Internet :: WWW/HTTP :: HTTP Servers" in settings.classifiers | ||
|
||
def test_version() -> None: | ||
"""Test package version number.""" | ||
assert __version__ == current_version | ||
|
||
def test_load_pyproject_error() -> None: | ||
"""Assert that default dict is loaded when pyproject.toml is not found.""" | ||
pyproject = load_pyproject(pyproject_path=Path("pyproject.toml")) | ||
pyproject_default = load_pyproject(pyproject_path=Path("error")) | ||
assert pyproject != pyproject_default | ||
assert pyproject_default == { | ||
"tool": {"poetry": {"name": "inboard", "description": "", "version": ""}} | ||
} | ||
|
||
|
||
def test_package_version() -> None: | ||
"""Assert that version number parsed from pyproject.toml matches | ||
version number of installed package obtained by importlib.metadata. | ||
""" | ||
assert version("inboard") == settings.version | ||
assert metadata("inboard")["name"] == settings.name == "inboard" | ||
assert metadata("inboard")["summary"] == settings.description |