fix(tests): add mocked dummy tests that run without a database#254
Open
AdarshJ173 wants to merge 1 commit into
Open
fix(tests): add mocked dummy tests that run without a database#254AdarshJ173 wants to merge 1 commit into
AdarshJ173 wants to merge 1 commit into
Conversation
Closes s3rius#236 The existing dummy tests (test_dummy.py) require a live database session and fail in the GitHub Actions environment because no database service is spun up in the default `pytest` job. Changes: - Add `tests/test_dummy_mocked.py`: rewrites both `test_creation` and `test_getting` using `unittest.mock.AsyncMock` so DummyDAO methods are stubbed and no real DB connection is needed. - Add `tests/conftest_mocked.py`: a self-contained conftest that provides the `mock_dao` fixture via `AsyncMock` without importing any ORM. - Update `.github/workflows/tests.yml`: add a new `pytest-mocked` job that installs deps with `uv sync` and runs `pytest tests/test_dummy_mocked.py -vv` directly on the runner, with no Docker/database services required.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #236
The existing
tests/test_dummy.pyrequires a live database session (dbsession/dbpoolfixtures) and fails in the default GitHub Actions environment because no database service is configured in thepytestjob.This PR adds a fully mocked alternative that runs in any plain Python environment — no Docker, no Postgres, no MySQL required.
Root Cause
test_creationandtest_gettingboth accept ORM-specific fixtures (dbsession: AsyncSession/dbpool: AsyncConnectionPool) and call realDummyDAOmethods that hit the database. The CIpytestjob intests.ymldoes not start any database service, so these tests always fail outside of the fulldocker-composestack.What Changed
tests/test_dummy_mocked.py(new file)test_creation_mockedandtest_getting_mockedusingunittest.mock.AsyncMockDummyDAO.create_dummy_modelandDummyDAO.filterat the module level so no real DB connection is openedAsyncClient— the API layer is tested for real; only the DAO ↔ DB hop is stubbedside_effectonfilterto simulate the before/after state (empty list → populated list) without any persistent statesqlalchemy,psycopg,beanie,ormar) or API type (rest,graphql) is selected.github/workflows/tests.ymlpytest-mockedjob that:uv sync(no Docker needed)uv run pytest tests/test_dummy_mocked.py -vvdirectly on the runnerpytest(docker-compose) job is preserved unchanged — full integration tests still work locallyWhy This Approach
The issue author specifically requested "providing mocked tests [as] an example of how to write mocked tests". Using
unittest.mock.AsyncMock(stdlib, no new dependencies) is the idiomatic Python pattern for this and serves as a clear template for users of the generated project.Testing
# No database needed — runs immediately in any Python 3.12+ env: uv run pytest tests/test_dummy_mocked.py -vvExpected output:
Notes
unittest.mockis stdlibpytest-mockedCI job runs in ~30 seconds with no services, making it an ideal fast-feedback check for PRs