Merge pull request #70 from malmeloo/feat/unit-tests

tests: Add very basic unit tests
This commit is contained in:
Mike Almeloo
2024-09-02 23:26:28 +02:00
committed by GitHub
8 changed files with 183 additions and 4 deletions

View File

@@ -26,7 +26,7 @@ jobs:
run: |
python -m pip install poetry
poetry config virtualenvs.in-project true
poetry install
poetry install --with dev
- name: Build documentation
run: |

View File

@@ -22,7 +22,7 @@ jobs:
run: |
python -m pip install poetry
poetry config virtualenvs.in-project true
poetry install
poetry install --with dev
- uses: pre-commit/action@v3.0.1

View File

@@ -24,7 +24,7 @@ jobs:
run: |
python -m pip install poetry
poetry config virtualenvs.in-project true
poetry install
poetry install --with dev
- name: Prepare README
run: ./scripts/refactor_readme.py README.md

57
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,57 @@
name: Run unit tests
on:
workflow_dispatch:
push:
jobs:
versions:
runs-on: ubuntu-latest
outputs:
py-versions: ${{ steps.supported-versions.outputs.py-versions }}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install poetry
poetry config virtualenvs.in-project true
poetry install --with dev
- id: supported-versions
name: Get supported versions
run: echo "py-versions=$(poetry run ./scripts/supported_py_versions.py)" >> "$GITHUB_OUTPUT"
test:
runs-on: ubuntu-latest
needs: versions
strategy:
matrix:
py-version: ${{ fromJson(needs.versions.outputs.py-versions) }}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "${{ matrix.py-version }}"
- name: Install dependencies
run: |
python -m pip install poetry
poetry config virtualenvs.in-project true
# Only install main dependencies
poetry install --with test
- name: Run unit tests
run: poetry run pytest

64
poetry.lock generated
View File

@@ -564,6 +564,20 @@ files = [
{file = "docutils-0.21.2.tar.gz", hash = "sha256:3a6b18732edf182daa3cd12775bbb338cf5691468f91eeeb109deff6ebfa986f"},
]
[[package]]
name = "exceptiongroup"
version = "1.2.2"
description = "Backport of PEP 654 (exception groups)"
optional = false
python-versions = ">=3.7"
files = [
{file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
{file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
]
[package.extras]
test = ["pytest (>=6)"]
[[package]]
name = "filelock"
version = "3.15.4"
@@ -721,6 +735,17 @@ doc = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linke
perf = ["ipython"]
test = ["flufl.flake8", "importlib-resources (>=1.3)", "jaraco.test (>=5.4)", "packaging", "pyfakefs", "pytest (>=6,!=8.1.*)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy", "pytest-perf (>=0.9.2)", "pytest-ruff (>=0.2.1)"]
[[package]]
name = "iniconfig"
version = "2.0.0"
description = "brain-dead simple config-ini parsing"
optional = false
python-versions = ">=3.7"
files = [
{file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},
{file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
]
[[package]]
name = "jinja2"
version = "3.1.4"
@@ -944,6 +969,21 @@ docs = ["furo (>=2023.9.10)", "proselint (>=0.13)", "sphinx (>=7.2.6)", "sphinx-
test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4.3)", "pytest-cov (>=4.1)", "pytest-mock (>=3.12)"]
type = ["mypy (>=1.8)"]
[[package]]
name = "pluggy"
version = "1.5.0"
description = "plugin and hook calling mechanisms for python"
optional = false
python-versions = ">=3.8"
files = [
{file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"},
{file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"},
]
[package.extras]
dev = ["pre-commit", "tox"]
testing = ["pytest", "pytest-benchmark"]
[[package]]
name = "pre-commit"
version = "3.8.0"
@@ -1081,6 +1121,28 @@ nodeenv = ">=1.6.0"
all = ["twine (>=3.4.1)"]
dev = ["twine (>=3.4.1)"]
[[package]]
name = "pytest"
version = "8.3.2"
description = "pytest: simple powerful testing with Python"
optional = false
python-versions = ">=3.8"
files = [
{file = "pytest-8.3.2-py3-none-any.whl", hash = "sha256:4ba08f9ae7dcf84ded419494d229b48d0903ea6407b030eaec46df5e6a73bba5"},
{file = "pytest-8.3.2.tar.gz", hash = "sha256:c132345d12ce551242c87269de812483f5bcc87cdbb4722e48487ba194f9fdce"},
]
[package.dependencies]
colorama = {version = "*", markers = "sys_platform == \"win32\""}
exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}
iniconfig = "*"
packaging = "*"
pluggy = ">=1.5,<2"
tomli = {version = ">=1", markers = "python_version < \"3.11\""}
[package.extras]
dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
[[package]]
name = "pyyaml"
version = "6.0.1"
@@ -1787,4 +1849,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools",
[metadata]
lock-version = "2.0"
python-versions = ">=3.9,<3.13"
content-hash = "149d6290c83176f706f1d5e34804561ff862e2ada5b1b7a55b29d44ef3d65af0"
content-hash = "4f46b85a3182de2a998c0a9ee8403011da162789e1f43df65b35701045e07a68"

View File

@@ -14,12 +14,22 @@ beautifulsoup4 = "^4.12.3"
aiohttp = "^3.9.5"
bleak = "^0.22.2"
[tool.poetry.group.dev]
optional = true
[tool.poetry.group.dev.dependencies]
pre-commit = "^3.8.0"
sphinx = "^7.2.6"
sphinx-autoapi = "^3.2.1"
pyright = "^1.1.374"
ruff = "0.5.6"
tomli = "^2.0.1"
[tool.poetry.group.test]
optional = true
[tool.poetry.group.test.dependencies]
pytest = "^8.3.2"
[tool.pyright]
venvPath = "."
@@ -32,6 +42,7 @@ reportImplicitOverride = true
[tool.ruff]
exclude = [
"docs/",
"tests/"
]
select = [

View File

@@ -0,0 +1,38 @@
#!/usr/bin/env python3
import json
from itertools import count
from pathlib import Path
from typing import Generator
import tomli
from packaging.specifiers import SpecifierSet
from packaging.version import Version
def get_python_versions() -> Generator[str, None, None]:
"""Get all python versions this package is compatible with."""
with Path("pyproject.toml").open("rb") as f:
pyproject_data = tomli.load(f)
specifier = SpecifierSet(pyproject_data["tool"]["poetry"]["dependencies"]["python"])
below_spec = True
for v_minor in count():
version = Version(f"3.{v_minor}")
# in specifier: yield
if version in specifier:
below_spec = False
yield str(version)
continue
# below specifier: skip
if below_spec:
continue
# above specifier: return
return
print(json.dumps(list(get_python_versions())))

11
tests/test_keygen.py Normal file
View File

@@ -0,0 +1,11 @@
import pytest
@pytest.mark.parametrize('execution_number', range(100))
def test_import(execution_number):
import findmy
kp = findmy.KeyPair.new()
assert len(kp.private_key_bytes) == 28
assert len(kp.adv_key_bytes) == 28
assert len(kp.hashed_adv_key_bytes) == 32