Skip to content

Commit

Permalink
Merge pull request #13 from aarhusstadsarkiv/tests
Browse files Browse the repository at this point in the history
Versions 0.3.1: Tests & Fixes
  • Loading branch information
MatteoCampinoti94 authored Oct 30, 2023
2 parents 66c08c8 + 8c98c58 commit 1fcbae3
Show file tree
Hide file tree
Showing 24 changed files with 604 additions and 72 deletions.
30 changes: 27 additions & 3 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
name: Linting & Test

on:
push
push

env:
PYTHON_VERSION: 3.9.18
POETRY_VERSION: 1.6.1

jobs:
call-linting-from-utils:
uses: aarhusstadsarkiv/acautils/.github/workflows/linting_ruff-mypy.yml@main
linting:
uses: aarhusstadsarkiv/acautils/.github/workflows/linting_ruff-mypy.yml@main
pytest:
name: pytest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ env.POETRY_VERSION }}
- uses: actions/setup-go@v4
- run: poetry install
- run: go install github.com/richardlehane/siegfried/cmd/sf@latest
- name: Unit test
env:
GOPATH: /home/runner/go
run: |
poetry run coverage run -m pytest
poetry run coverage report -m --fail-under=80 --skip-empty --skip-covered
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,5 @@ cython_debug/
/*.db

.ruff_cache/

/tests/tmp/
2 changes: 1 addition & 1 deletion acacore/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.0"
__version__ = "0.3.1"
4 changes: 3 additions & 1 deletion acacore/database/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def _schema_to_column(name: str, schema: dict, defs: Optional[dict[str, dict]] =
sql_type = _sql_schema_types.get(schema_type, None)
type_name: str = schema.get("format", schema_type)

if type_name in _sql_schema_type_converters:
if schema.get("enum"):
to_entry, from_entry = lambda e: e.value, str
elif type_name in _sql_schema_type_converters:
to_entry, from_entry = _sql_schema_type_converters[type_name]
else:
raise TypeError(f"Cannot recognize type from schema {schema!r}")
Expand Down
1 change: 1 addition & 0 deletions acacore/database/files_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def init(self):
self.files.create(True)
self.metadata.create(True)
self.converted_files.create(True)
self.history.create(True)
self.not_converted.create(True)
self.identification_warnings.create(True)
self.signature_count.create(True)
Expand Down
3 changes: 1 addition & 2 deletions acacore/models/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from pathlib import Path
from typing import Any

Expand All @@ -12,4 +11,4 @@ def dump(self, to_file: Path) -> None:
to_file.write_text(super().model_dump_json(), encoding="utf-8")

def encode(self) -> Any: # noqa: ANN401
return json.loads(super().model_dump_json())
return super().model_dump(mode="json")
15 changes: 6 additions & 9 deletions acacore/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@


class Action(Enum):
CONVERT = "Convertool: To convert."
REPLACE = "Convertool: Replace with template. File is not preservable."
MANUAL = "Manual: File should be converted manually. [info about the manual conversion from reference_files]."
RENAME = "Renamer: File has extension mismatch. Should be renamed"
CONVERT = "CONVERT" # To convert.
REPLACE = "REPLACE" # Replace with template. File is not preservable.
MANUAL = "MANUAL" # File should be converted manually. [info about the manual conversion from reference_files].
RENAME = "RENAME" # File has extension mismatch. Should be renamed


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -150,14 +150,11 @@ def size_fmt(self) -> str:
str
File size in human-readable format.
"""
return str(size_fmt(self.get_absolute_path().stat().st_size))
return size_fmt(self.get_absolute_path().stat().st_size)

def get_bof_and_eof(self) -> Tuple[str, str]:
"""Get the first and last kilobyte of the file.
Args:
file (Path): Path to file
Returns:
Tuple[str,str]: BOF and then EOF as `str`.
"""
Expand All @@ -172,7 +169,7 @@ def get_bof_and_eof(self) -> Tuple[str, str]:
# File too small :)
file_bytes.seek(-file_bytes.tell(), 2)
eof = file_bytes.read(1024).hex()
return (bof, eof)
return bof, eof


class ArchiveFile(Identification, File):
Expand Down
12 changes: 12 additions & 0 deletions acacore/utils/functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import Callable
from typing import Optional
from typing import TypeVar
Expand All @@ -18,3 +19,14 @@ def or_none(func: Callable[[T], R]) -> Callable[[T], Optional[R]]:
object: A function of type (T) -> R | None.
"""
return lambda x: None if x is None else func(x)


def rm_tree(path: Path):
if not path.is_dir():
path.unlink(missing_ok=True)
return

for item in path.iterdir():
rm_tree(item) if item.is_dir() else item.unlink(missing_ok=True)

path.rmdir()
5 changes: 2 additions & 3 deletions acacore/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ def size_fmt(size: float) -> str:
Parameters
----------
size: float
size:
The file size in bytes.
Returns:
-------
str
Human readable string representing size in binary multiples.
Human readable string representing size in binary multiples.
"""
unit: int = int(log2(size) // 10)
unit = unit if unit < len(binary_units) else len(binary_units) - 1
Expand Down
128 changes: 127 additions & 1 deletion poetry.lock

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

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "acacore"
version = "0.3.0"
version = "0.3.1"
description = ""
authors = ["Matteo Campinoti <[email protected]>"]
license = "GPL-3.0"
Expand All @@ -10,11 +10,13 @@ readme = "README.md"
python = "^3.9"
pydantic = "^2.4.2"
tqdm = "^4.66.1"
coverage = "^7.3.2"


[tool.poetry.group.dev.dependencies]
ruff = "^0.0.286"
black = "^23.7.0"
pytest = "^7.4.3"

[build-system]
requires = ["poetry-core"]
Expand Down
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from json import loads
from pathlib import Path

import pytest

from acacore.utils.functions import rm_tree


@pytest.fixture(scope="session")
def test_folder() -> Path:
return Path(__file__).parent


@pytest.fixture(scope="session")
def temp_folder(test_folder: Path) -> Path:
return test_folder / "tmp"


@pytest.fixture(scope="session")
def test_files(test_folder: Path) -> Path:
return test_folder / "files"


@pytest.fixture(scope="session")
def test_files_data(test_files: Path) -> dict[str, dict]:
return loads(test_files.joinpath("files.json").read_text())


@pytest.fixture(autouse=True, scope="session")
def _pre_test(temp_folder: Path):
rm_tree(temp_folder)
temp_folder.mkdir(parents=True, exist_ok=True)
Loading

0 comments on commit 1fcbae3

Please sign in to comment.