Skip to content

Commit

Permalink
Merge pull request #70 from malmeloo/feat/unit-tests
Browse files Browse the repository at this point in the history
tests: Add very basic unit tests
  • Loading branch information
malmeloo authored Sep 2, 2024
2 parents 6b9299f + ae98d61 commit d9a9f64
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "."
Expand All @@ -32,6 +42,7 @@ reportImplicitOverride = true
[tool.ruff]
exclude = [
"docs/",
"tests/"
]

select = [
Expand Down
38 changes: 38 additions & 0 deletions scripts/supported_py_versions.py
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions tests/test_keygen.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d9a9f64

Please sign in to comment.