Skip to content

Commit 0e0e092

Browse files
authored
Merge pull request #31 from BAMresearch/29-testcases-jmp
Test changes and clean up
2 parents 41f264c + 44ab96b commit 0e0e092

6 files changed

Lines changed: 493 additions & 197 deletions

File tree

openbis_upload_helper/app/utils/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from openbis_upload_helper.uploader.entry_points import get_entry_point_parsers
2121

2222

23-
2423
def get_cipher_suite():
2524
"""Return a Fernet instance using the configured secret key.
2625

pyproject.toml

Lines changed: 96 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0.0", "setuptools-scm>=8.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "openbis_upload_helper"
7+
version = "0.1.0"
8+
description = "The openBIS Upload Helper is a tool to parse files and extract their relevant metadata into objects and vocabularies in openBIS."
9+
readme = "README.md"
10+
11+
license = { text = "Not open source" }
12+
authors = [
13+
{ name = "Jose M. Pizarro", email = "jose.pizarro-blanco@bam.de" },
14+
]
15+
requires-python = ">=3.12.12"
16+
dependencies = [
17+
"argon2-cffi==25.1.0",
18+
"celery==5.6.0",
19+
"crispy-bootstrap5==2025.6",
20+
"django==5.2.9",
21+
"django-allauth[mfa]==65.13.1",
22+
"django-anymail[mailgun]==14.0",
23+
"django-celery-beat==2.8.1",
24+
"django-crispy-forms==2.5",
25+
"django-environ==0.12.0",
26+
"django-model-utils==5.0.0",
27+
"django-redis==6.0.0",
28+
"gunicorn==23.0.0",
29+
"hiredis==3.3.0",
30+
"pillow==12.0.0",
31+
"psycopg[binary]==3.3.2",
32+
"python-slugify==8.0.4",
33+
"redis==7.1.0",
34+
"sentry-sdk==2.48.0",
35+
"uvicorn-worker==0.4.0",
36+
"uvicorn[standard]==0.38.0",
37+
"whitenoise==6.11.0",
38+
"bam-masterdata>=0.8.4",
39+
]
40+
41+
[project.urls]
42+
"Homepage" = "https://github.com/BAMresearch/openbis-upload-helper"
43+
"Bug Tracker" = "https://github.com/BAMresearch/openbis-upload-helper/issues"
44+
45+
[project.optional-dependencies]
46+
dev = [
47+
"coverage==7.13.0",
48+
"django-coverage-plugin==3.2.0",
49+
"django-debug-toolbar==6.1.0",
50+
"django-extensions==4.1",
51+
"django-stubs[compatible-mypy]==5.2.8",
52+
"djlint==1.36.4",
53+
"factory-boy==3.3.2",
54+
"ipdb==0.13.13",
55+
"mypy==1.19.1",
56+
"pre-commit==4.5.1",
57+
"psycopg[binary]==3.3.2",
58+
"pytest==9.0.2",
59+
"pytest-cov",
60+
"pytest-django==4.11.1",
61+
"pytest-sugar==1.1.1",
62+
"pytest-timeout",
63+
"ruff==0.14.10",
64+
"sphinx==9.0.4",
65+
"sphinx-autobuild==2025.8.25",
66+
"structlog==24.4.0",
67+
"watchfiles==1.1.1",
68+
"werkzeug[watchdog]==3.1.4",
69+
"ruff",
70+
]
71+
parsers = [
72+
"masterdata-parser-example@git+https://github.com/BAMresearch/masterdata-parser-example.git@main",
73+
]
74+
75+
# "sigmabam2openbis@git+https://github.com/BAMresearch/sigmabam2openbis.git@main"
76+
177
# ==== pytest ====
278
[tool.pytest.ini_options]
379
minversion = "6.0"
@@ -6,8 +82,6 @@ python_files = [
682
"tests.py",
783
"test_*.py",
884
]
9-
DJANGO_SETTINGS_MODULE = "config.settings.test"
10-
1185

1286
# ==== Coverage ====
1387
[tool.coverage.run]
@@ -91,68 +165,27 @@ indent-width = 4
91165

92166
[tool.ruff.lint]
93167
select = [
94-
"F",
95-
"E",
96-
"W",
97-
"C90",
98-
"I",
99-
"N",
100-
"UP",
101-
"YTT",
102-
# "ANN", # flake8-annotations: we should support this in the future but 100+ errors atm
103-
"ASYNC",
104-
"S",
105-
"BLE",
106-
"FBT",
107-
"B",
108-
"A",
109-
"COM",
110-
"C4",
111-
"DTZ",
112-
"T10",
113-
"DJ",
114-
"EM",
115-
"EXE",
116-
"FA",
117-
'ISC',
118-
"ICN",
119-
"G",
120-
'INP',
121-
'PIE',
122-
"T20",
123-
'PYI',
124-
'PT',
125-
"Q",
126-
"RSE",
127-
"RET",
128-
"SLF",
129-
"SLOT",
130-
"SIM",
131-
"TID",
132-
"TC",
133-
"INT",
134-
# "ARG", # Unused function argument
135-
"PTH",
136-
"ERA",
137-
"PD",
138-
"PGH",
139-
"PL",
140-
"TRY",
141-
"FLY",
142-
# "NPY",
143-
# "AIR",
144-
"PERF",
145-
# "FURB",
146-
# "LOG",
147-
"RUF",
168+
"E", # pycodestyle
169+
"PL", # pylint
170+
"F", # Pyflakes
171+
"UP", # pyupgrade
172+
"I", # isort
148173
]
149174
ignore = [
150-
"S101", # Use of assert detected https://docs.astral.sh/ruff/rules/assert/
151-
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
152-
"SIM102", # sometimes it's better to nest
153-
# of types for comparison.
154-
# Deactivated because it can make the code slow:
155-
# https://github.com/astral-sh/ruff/issues/7871
175+
"F401", # Module imported but unused
176+
"E501", # Line too long ({width} > {limit} characters)
177+
"E701", # Multiple statements on one line (colon)
178+
"E731", # Do not assign a lambda expression, use a def
179+
"E402", # Module level import not at top of file
180+
"PLR0911", # Too many return statements
181+
"PLR0912", # Too many branches
182+
"PLR0913", # Too many arguments in function definition
183+
"PLR0915", # Too many statements
184+
"PLR2004", # Magic value used instead of constant
185+
"PLW0603", # Using the global statement
186+
"PLW2901", # redefined-loop-name
187+
"PLR1714", # consider-using-in
188+
"PLR5501", # else-if-used
156189
]
157190
fixable = ["ALL"]
158191
# Allow unused variables when underscore-prefixed.
@@ -161,89 +194,7 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
161194
[tool.ruff.lint.isort]
162195
force-single-line = true
163196

164-
[dependency-groups]
165-
dev = [
166-
"coverage==7.13.0",
167-
"django-coverage-plugin==3.2.0",
168-
"django-debug-toolbar==6.1.0",
169-
"django-extensions==4.1",
170-
"django-stubs[compatible-mypy]==5.2.8",
171-
"djlint==1.36.4",
172-
"factory-boy==3.3.2",
173-
"ipdb==0.13.13",
174-
"mypy==1.19.1",
175-
"pre-commit==4.5.1",
176-
"psycopg[binary]==3.3.2",
177-
"pytest==9.0.2",
178-
"pytest-django==4.11.1",
179-
"pytest-sugar==1.1.1",
180-
"ruff==0.14.10",
181-
"sphinx==9.0.4",
182-
"sphinx-autobuild==2025.8.25",
183-
"watchfiles==1.1.1",
184-
"werkzeug[watchdog]==3.1.4",
185-
]
186-
187-
[project]
188-
name = "openbis_upload_helper"
189-
version = "0.1.0"
190-
description = "The openBIS Upload Helper is a tool to parse files and extract their relevant metadata into objects and vocabularies in openBIS."
191-
readme = "README.md"
192-
193-
license = { text = "Not open source" }
194-
authors = [
195-
{ name = "Jose M. Pizarro", email = "jose.pizarro-blanco@bam.de" },
196-
]
197-
requires-python = ">=3.12.12"
198-
dependencies = [
199-
"argon2-cffi==25.1.0",
200-
"celery==5.6.0",
201-
"crispy-bootstrap5==2025.6",
202-
"django==5.2.9",
203-
"django-allauth[mfa]==65.13.1",
204-
"django-anymail[mailgun]==14.0",
205-
"django-celery-beat==2.8.1",
206-
"django-crispy-forms==2.5",
207-
"django-environ==0.12.0",
208-
"django-model-utils==5.0.0",
209-
"django-redis==6.0.0",
210-
"gunicorn==23.0.0",
211-
"hiredis==3.3.0",
212-
"pillow==12.0.0",
213-
"psycopg[binary]==3.3.2",
214-
"python-slugify==8.0.4",
215-
"redis==7.1.0",
216-
"sentry-sdk==2.48.0",
217-
"uvicorn-worker==0.4.0",
218-
"uvicorn[standard]==0.38.0",
219-
"whitenoise==6.11.0",
220-
"bam-masterdata>=0.8.4",
221-
]
222-
223-
[build-system]
224-
requires = ["setuptools>=61.0.0", "setuptools-scm>=8.0"]
225-
build-backend = "setuptools.build_meta"
226-
227-
[project.urls]
228-
"Homepage" = "https://github.com/BAMresearch/openbis-upload-helper"
229-
"Bug Tracker" = "https://github.com/BAMresearch/openbis-upload-helper/issues"
230-
231-
[project.optional-dependencies]
232-
dev = [
233-
"mypy==1.18.1",
234-
"django-stubs",
235-
"pytest",
236-
"pytest-timeout",
237-
"pytest-cov",
238-
"structlog==24.4.0",
239-
]
240-
parsers = [
241-
"masterdata-parser-example@git+https://github.com/BAMresearch/masterdata-parser-example.git@main",
242-
]
243-
244-
# "sigmabam2openbis@git+https://github.com/BAMresearch/sigmabam2openbis.git@main"
245-
246-
197+
# this is entirely optional, you can remove this if you wish to
247198
[tool.ruff.format]
248199
# use double quotes for strings.
249200
quote-style = "double"

tests/conftest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from unittest.mock import MagicMock
44

5+
import django
56
import pytest
67
from bam_masterdata.logger import log_storage
78
from bam_masterdata.metadata.definitions import ObjectTypeDef
@@ -27,6 +28,20 @@ def pytest_internalerror(excinfo):
2728
raise excinfo.value
2829

2930

31+
@pytest.fixture(scope="session", autouse=True)
32+
def configure_django_settings():
33+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.test")
34+
if not settings.configured:
35+
django.setup()
36+
settings.ROOT_URLCONF = "openbis_upload_helper.app.urls"
37+
settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
38+
# pytest-django clears django.core.mail.outbox automatically; ensure it exists
39+
from django.core import mail # noqa: PLC0415
40+
41+
if not hasattr(mail, "outbox"):
42+
mail.outbox = []
43+
44+
3045
@pytest.fixture(autouse=True)
3146
def cleared_log_storage():
3247
"""Fixture to clear the log storage before each test."""
@@ -335,3 +350,34 @@ def set_secret_encryption_key():
335350
settings.SECRET_ENCRYPTION_KEY = base64.urlsafe_b64encode(
336351
Fernet.generate_key(),
337352
).decode()
353+
354+
355+
class DummySession(dict):
356+
def __init__(self):
357+
super().__init__()
358+
self.flushed = False
359+
360+
def flush(self):
361+
self.flushed = True
362+
self.clear()
363+
364+
365+
@pytest.fixture
366+
def attach_session():
367+
def _attach(request):
368+
request.session = DummySession()
369+
return request
370+
371+
return _attach
372+
373+
374+
@pytest.fixture
375+
def make_uploaded_file():
376+
def _make(name="file.txt", content=b"x"):
377+
uploaded_file = MagicMock()
378+
uploaded_file.name = name
379+
uploaded_file.size = len(content)
380+
uploaded_file.chunks.return_value = [content]
381+
return uploaded_file
382+
383+
return _make

tests/entry_points/test_load.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
from types import SimpleNamespace
2+
from unittest.mock import patch
3+
14
from openbis_upload_helper.uploader.entry_points import get_entry_point_parsers
25

36

4-
def test_get_entry_point_parsers():
7+
@patch("importlib.metadata.entry_points")
8+
def test_get_entry_point_parsers(mock_entry_points):
9+
mock_entry_points.return_value = [
10+
SimpleNamespace(
11+
name="masterdata_parser_example_entry_point",
12+
load=lambda: {
13+
"name": "MasterdataParserExample",
14+
"description": "An example parser for masterdata.",
15+
"parser_class": object(),
16+
},
17+
),
18+
]
519
parsers = get_entry_point_parsers()
620
assert isinstance(parsers, dict)
721
assert "masterdata_parser_example_entry_point" in parsers

tests/test_dummy.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)