Repository navigation
test: run tests against a real MongoDB - #164
Conversation
If MONGODB_TEST_URI is set, the fixtures use the MongoDB at that URI instead of mongomock: one client for the session, with database serviceStore dropped before each test, and network access allowed to the database's addresses only. Unit tests use database serviceStore too (was: db). Collection patches use the collection class of the tests' database. Without the variable, the tests use mongomock as before. Document how to run the tests against a local MongoDB. The full suite passes against MongoDB 7.0 without changes to the app. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add job test_mongodb, which runs the full suite against a MongoDB 7.0 service container (MONGODB_TEST_URI), and require it for the stable "Run tests" check. Closes #154 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fail the MongoDB job if MONGODB_TEST_URI is missing instead of falling back to mongomock, check the mode against the environment in the fixture tests, publish the test MongoDB on localhost only in the docs, and say that each run needs its own single-node MongoDB. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThe PR extends the test fixtures to optionally use a real, isolated MongoDB while retaining mongomock by default, adapts race-test patching and network controls accordingly, adds fixture coverage, and introduces a required MongoDB 7.0 CI job with local-development documentation. Sequence diagram for isolated real MongoDB testssequenceDiagram
participant Runner as Test runner
participant Fixtures as Test fixtures
participant MongoDB as MongoDB
participant Test as Test case
Runner->>Fixtures: create session client from MONGODB_TEST_URI
Fixtures->>MongoDB: drop_database(serviceStore)
Fixtures->>Test: provide client and serviceStore database
Test->>MongoDB: execute test operations
Test-->>Fixtures: test completes
Fixtures->>MongoDB: drop_database(serviceStore)
Runner->>Fixtures: finish test session
Fixtures->>MongoDB: drop_database(serviceStore)
Flow diagram for selecting the test database backendflowchart TD
Start[Test suite starts] --> URI{MONGODB_TEST_URI set?}
URI -->|Yes| Resolve[Resolve URI host addresses]
Resolve --> Allow[_no_network allows resolved addresses]
Allow --> Real[Use session MongoDB client]
URI -->|No| Mock[Use mongomock client]
Real --> Isolate[Drop serviceStore before each test]
Mock --> Run[Run tests]
Isolate --> Run
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #164 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 11 11
Lines 880 880
=========================================
Hits 880 880 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/conftest.py" line_range="245" />
<code_context>
+ if _mongodb is None:
+ client: MongoClient[dict[str, Any]] = mongomock.MongoClient()
+ return client
+ _mongodb.drop_database(DB)
+ return _mongodb
+
</code_context>
<issue_to_address>
**Test run deletes service data**
When a developer or CI operator sets `MONGODB_TEST_URI` to a shared or production MongoDB that contains `serviceStore`, `db_client` unconditionally calls `_mongodb.drop_database(DB)` for each test, deleting every collection in `serviceStore` on the configured server; the operator loses that data.
Require an explicit destructive-test opt-in or otherwise verify that the configured target is disposable before dropping `DB`.
</issue_to_address>
### Comment 2
<location path="tests/conftest.py" line_range="245" />
<code_context>
+ if _mongodb is None:
+ client: MongoClient[dict[str, Any]] = mongomock.MongoClient()
+ return client
+ _mongodb.drop_database(DB)
+ return _mongodb
+
</code_context>
<issue_to_address>
**Parallel workers erase each other's data**
When pytest-xdist runs tests in multiple workers against the same `MONGODB_TEST_URI`, worker A can drop `serviceStore` while worker B's test is using it, so B loses its records or indexes and its assertions fail. Use a worker-specific database or serialize tests that share this database.
Give each worker an isolated database, or prevent parallel workers from sharing the test database.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and when MONGODB_TEST_URI is set, the test fixture drops the serviceStore database before each test. If the URI points to a database with real data, that data is deleted and reverting the change cannot restore it.
Blocking findings: tests/conftest.py:245, tests/conftest.py:245
End the test run before any test if the MongoDB at MONGODB_TEST_URI already has database serviceStore (e.g., of a deployment), instead of dropping it (Sourcery review). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
All tests ran against mongomock. With this PR, the full suite can run against a real MongoDB, and CI does so against MongoDB 7.0.
tests/conftest.py:MONGODB_TEST_URIis set, the fixtures use the MongoDB at that URI. One client serves the whole session, and databaseserviceStoreis dropped before each test (and at the end). Without the variable, the tests use mongomock as before. The run ends with a usage error, before any test, if that MongoDB already has a databaseserviceStore(for example a deployment's data; Sourcery review)._no_networkstill blocks network access, but allows connections to the resolved addresses of the URI's hosts (DB_ADDRESSES).unit_app) use databaseserviceStoreof the same client (was:dbof their own mongomock client).COLLECTION_CLASS(pymongo's or mongomock'sCollection) is the patch target of the race tests intests/test_access_control.py, which patched mongomock's class before.tests/test_fixtures.py: tests of the switch, the per-test isolation, the address resolution and the network block.Run tests (MongoDB 7.0)(test_mongodb), with amongo:7.0service container, as indocker-compose.yaml(8.0 doesn't start on some runner kernels). The stableRun testscheck now requires it, and the job fails if the URI is missing instead of silently using mongomock.docs/development.md: how to run the tests against a local MongoDB.Real differences found: none. The full suite passed unchanged against MongoDB 7.0, including the race tests, which assert that their interleaving ran.
Closes #154
Verification
MONGODB_TEST_URI=mongodb://127.0.0.1:12345): the full suite passed (884 passed after the Sourcery fix). The mongod log shows the session client (3 connections) and the per-test drops. The adversarial reviewer independently got 738 passed and 145 skipped (helm missing) against its own mongod, plus alocalhostURI run. Afterwards no database was left besides admin/config/local.coverage run --source cloud_registry,scripts -m pytestgave 884 passed and 100% coverage.mkdocs build --strict: all pass.Run tests (MongoDB 7.0)job ran the full suite against the service container (883 passed, chart tests included). Also passing: static analysis, tests on 3.10 and 3.12, Helm chart, docs, Docker Compose smoke test and Read the Docs.Declined review findings
Rename
COLLECTION_CLASS(quality, low). Kept; its comment says what it is for.Drop the typed local variable in
db_client(quality, low). It avoids atype: ignore.Deduplicate the setup steps of
test_mongodb(quality, low). The other jobs ofchecks.yamlfollow the same pattern.Allow replica-set members discovered at runtime, or re-resolve DNS at connect time (adversarial, low). The docs and the conftest docstring now require a single-node MongoDB, as in CI.
A database name per run, so that concurrent runs can share a MongoDB (adversarial, low). The docs now ask for one MongoDB per run; the tests hard-code
serviceStorein places.Separate databases per pytest-xdist worker (Sourcery, medium). The project doesn't run tests in parallel, and the database name comes from
config.yaml. The docs ask for one MongoDB per run.Open questions
None.
🤖 Generated with Claude Code