opensafely-core / opensafely-core/tpp-database-utils
`main.py` is currently untested and not easily testable
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 0
- Forks
- 0
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 4
Description
Although we run using Coverage and configure it to fail at less than 100% we don't actually tell it what directories to cover, so it ends up not enforcing anything.
The fix is to change this line:
https://github.com/opensafely-core/tpp-database-utils/blob/067273b699a5d8d8946ea337dd8466af7205390f/justfile#L103
To this:
uv run coverage run --source tpp_database_utils,tests --module pytest {{ args }}
This then fails because main.py is untested:
uv run coverage report || uv run coverage html
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------
tpp_database_utils/main.py 45 45 16 0 0% 1-74
------------------------------------------------------------------------
TOTAL 253 45 40 0 79%
However, it's not (straightforwardly) possible to write tests for it because it assumes it's being invoked directly as a script and therefore that its sibling modules are all on sys.path.
If you try creating tests/test_main.py:
from tpp_database_utils import main
def test_main():
assert main
It will fail with an import error:
$ pytest tests/test_main.py
========================== test session starts ==========================
platform linux -- Python 3.11.14, pytest-9.0.2, pluggy-1.6.0
rootdir: /home/dave/projects/bennett/tpp-database-utils
configfile: pyproject.toml
collected 0 items / 1 error
================================ ERRORS =================================
__________________ ERROR collecting tests/test_main.py __________________
ImportError while importing test module '/home/dave/projects/bennett/tpp-database-utils/tests/test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../.local/share/uv/python/cpython-3.11.14-linux-x86_64-gnu/lib/python3.11/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tests/test_main.py:1: in <module>
from tpp_database_utils import main
tpp_database_utils/main.py:4: in <module>
import maintenance_mode
E ModuleNotFoundError: No module named 'maintenance_mode'
======================== short test summary info ========================
ERROR tests/test_main.py
!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!
=========================== 1 error in 0.07s ============================
The simplest fix here is probably to move to main.py to __main__.py, rewrite the imports to use the package name and then change the Docker entrypoint to use python -m tpp_database_utils:
https://github.com/opensafely-core/tpp-database-utils/blob/067273b699a5d8d8946ea337dd8466af7205390f/docker/Dockerfile#L155
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tpp_database_utils/main.py, the coverage command at justfile line 103, and the Docker entrypoint at docker/Dockerfile line 155. Try the proposed main.py and package-import approach, then add tests/test_main.py and run the coverage command. Done means main.py is testable and coverage reports the intended package and tests without the import error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, python
- Domain
- backend, devops, testing-qa
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100