Skip to content

Commit

Permalink
Merge pull request #3 from m3brown/tests
Browse files Browse the repository at this point in the history
Set up test framework
  • Loading branch information
tenekev authored Aug 16, 2024
2 parents b98d99e + 40a0258 commit 5195412
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 2 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
9 changes: 9 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.12-alpine

COPY *.py *.txt tests /script/

RUN pip install --no-cache-dir -r /script/requirements-test.txt \
&& rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/*

WORKDIR /script
CMD ["python", "-m", "pytest"]
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Or with Docker exec:
docker exec -it immich-auto-stack /script/immich_auto_stack.sh
```

## Running tests

```sh
docker build -f Dockerfile.test -t immich-auto-stack-pytest .
docker run immich-auto-stack-pytest
=======
## Customizing the criteria

Configurable criteria allows for the customization of how files are grouped
Expand All @@ -79,8 +85,6 @@ The default in pretty json is:

Functionally, this JSON config is the same as the lamdba implementation currently in place:



```python
lambda x: (
x["originalFileName"].split(".")[0],
Expand All @@ -95,6 +99,13 @@ The CRITERIA env var.
docker -e CRITERIA='[{"key": "originalFileName", "split": {"key": "_", "index": 0}}]' ...
```

This is the equivalent of:
```python
lambda x: (
x["originalFileName"].split("_")[0]
)
```

The parser also supports regex, which adds a lot more flexibility.
The index will select a substring using `re.match.group(index)`. For example:

Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tool.pytest.ini_options]
pythonpath = [
"."
]
11 changes: 11 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-r requirements.txt

exceptiongroup==1.2.2
Faker==27.0.0
iniconfig==2.0.0
packaging==24.1
pluggy==1.5.0
pytest==8.3.2
python-dateutil==2.9.0.post0
six==1.16.0
tomli==2.0.1
105 changes: 105 additions & 0 deletions tests/test_stackBy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from faker import Faker
import pytest
from unittest.mock import ANY

from immich_auto_stack import stackBy


def mock_criteria(x):
return (x["originalFileName"].split(".")[0], x["localDateTime"])


fake = Faker()


def asset_factory(file_base=None, date_time=None, extension="jpg"):
file_name = (file_base or fake.unique.file_name(extension="")) + "." + extension
return {
"originalFileName": file_name,
"localDateTime": date_time or fake.unique.date_time(),
}


@pytest.mark.parametrize(
"test_base1,test_ext1,test_base2,test_ext2",
[
("test_filename", "raw", "test_filename2", "raw"),
("test_Filename", "raw", "test_filename", "raw"),
("_", "raw", "__", "raw"),
("test_filename", "Jpg", "test_filename2", "Jpg"),
("test_filename", "tar.jpg", "test_filename2", "tar.jpg"),
],
)
def test_stackBy_creates_two_groups_for_different_filenames(
test_base1, test_ext1, test_base2, test_ext2
):
# Arrange
file1_1 = asset_factory(file_base=test_base1, extension=test_ext1)
file1_2 = asset_factory(file_base=test_base1, date_time=file1_1["localDateTime"])
file2_1 = asset_factory(file_base=test_base2, extension=test_ext2)
file2_2 = asset_factory(file_base=test_base2, date_time=file2_1["localDateTime"])
expected_result = [
((test_base1, file1_1["localDateTime"]), ANY),
((test_base2, file2_1["localDateTime"]), ANY),
]

# Act
result = stackBy(data=[file1_1, file2_1, file1_2, file2_2], criteria=mock_criteria)

# Assert
assert result == expected_result


@pytest.mark.parametrize(
"extensions",
[
["raw", "jpg"],
["raw", "jpg", "txt", "XMP"],
["raw", "png", "_"],
["jpg", "5"],
],
)
def test_stackBy_creates_list_of_file_assets_within_group(extensions):
# Arrange
extensions.sort()
test_file_base = "test_filename"
test_date_time = fake.date_time()
assets = [
asset_factory(file_base=test_file_base, extension=e, date_time=test_date_time)
for e in extensions
]
expected_result = [((test_file_base, test_date_time), assets)]

# Act
result = stackBy(data=assets, criteria=mock_criteria)

# Assert
assert result == expected_result
assert len(result[0][1]) > 1


def test_stackBy_does_not_group_files_with_same_date_time_but_different_filename():
# Arrange
test_date_time = fake.date_time()
file_1 = asset_factory(date_time=test_date_time)
file_2 = asset_factory(date_time=test_date_time)
expected_result = []

# Act
result = stackBy(data=[file_1, file_2], criteria=mock_criteria)

# Assert
assert result == expected_result


def test_stackBy_does_not_group_files_with_same_filename_but_different_datetime():
# Arrange
file_1 = asset_factory(file_base="test_filename", extension="jpg")
file_2 = asset_factory(file_base="test_filename", extension="raw")
expected_result = []

# Act
result = stackBy(data=[file_1, file_2], criteria=mock_criteria)

# Assert
assert result == expected_result
Loading

0 comments on commit 5195412

Please sign in to comment.