Skip to content

test: add isolated pytest fixtures for database-backed tests (#577) - #582

Closed
Dev10-sys wants to merge 1 commit into
chaoss:mainfrom
Dev10-sys:feat/db-test-fixtures
Closed

Dev10-sys wants to merge 1 commit into
chaoss:mainfrom
Dev10-sys:feat/db-test-fixtures

Conversation

@Dev10-sys

@Dev10-sys Dev10-sys commented Nov 21, 2026 •

Copy link
Copy Markdown

Description

This PR addresses #577 by introducing a unified, reusable set of pytest fixtures for database-backed tests across CollectOSS.

Previously, the test setup relied on root-level PostgreSQL privileges to create and drop entire databases, which fails in non-superuser environments and causes test collisions during parallel runs. I reworked conftest.py to use dynamic per-test schema isolation instead. Each test that needs a clean database environment gets its own isolated PostgreSQL schema ( est_schema_), which automatically drops with CASCADE during teardown. This ensures complete isolation without requiring special database permissions.

Along with the core engine and session fixtures (db_engine, db_session, �lank_db_schema, schema_factory, and collectoss_db_session), I added automatic skip handling. When no database connection string is configured via environment variables (COLLECTOSS_DB, TEST_DB_URL) or CLI flags (--db-url), all database-marked tests gracefully skip with an informative reason rather than erroring out. I also added a minimal docker-compose.test.yml so contributors can easily spin up a local PostgreSQL test container with a single command, as well as a test suite in ests/test_application/test_db/test_db_fixtures.py that validates the fixtures against schema isolation, multi-schema independence, session lifecycles, and materialized view creation and refresh. All 138 existing unit tests continue to pass cleanly.

This PR fixes #577

Notes for Reviewers

All unit tests run and pass without requiring any database running locally. For CI, database tests will skip cleanly until a PostgreSQL service container or connection URL is supplied. The legacy fixtures have also been maintained and routed through the new engine logic to ensure full backwards compatibility with existing test files.

Signed commits

  • Yes, I signed my commits.

Generative AI disclosure

Please select one option:

  • This contribution was NOT assisted or created by Generative AI tools.
  • This contribution was assisted or created by Generative AI tools.

@Dev10-sys
Dev10-sys requested a review from MoralCode as a code owner November 21, 2026 16:08
@Dev10-sys Dev10-sys closed this Nov 21, 2026
@Dev10-sys Dev10-sys reopened this Nov 21, 2026
@Dev10-sys
Dev10-sys force-pushed the feat/db-test-fixtures branch from 5043519 to 48e6d90 Compare November 21, 2026 16:23

@MoralCode MoralCode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dev10-sys thanks for the PR! This PR includes a lot of things.

I have also been working on a similar PR to add this kind of testing infrastructure (#583), Could you maybe code review my version? id like sure I'm covering most of the same features in that pr as well

Sorry for forgetting to self-assign the relevant issue to indicate it was being worked on already!

@pytest.mark.db
def test_startup_forge_instance_population_use_case(db_engine, schema_factory):
"""Verify database-backed startup data population (CollectOSS issue #577 and PR #575 use case)."""
schema = schema_factory("startup_test")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting pattern for specifying a schema name!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The per-test schema isolation prevents tests from stepping on each other's data and avoids table-level truncate locks when running tests concurrently.



@pytest.mark.db
def test_startup_forge_instance_population_use_case(db_engine, schema_factory):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests a feature that hasnt yet been merged

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Removed it for now since #575 hasn't merged yet. We can add it back in a follow-up PR once #575 lands.

Comment thread conftest.py Outdated
Comment on lines +54 to +60
def pytest_configure(config):
"""Register custom pytest markers."""
config.addinivalue_line(
"markers",
"db: mark test as requiring a database connection (skipped when no database URL is provided)",
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? this PR already defines the db marker in pyproject.toml

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're completely right since it's already registered statically in pyproject.toml, the dynamic addinivalue_line in pytest_configure is redundant and can be removed

Introduce reusable pytest database fixtures supporting schema isolation and automatic skipping when no database is configured.

- Add db pytest marker and CLI option --db-url for database test runs
- Support TEST_DB_URL, COLLECTOSS_DB, and AUGUR_DB environment variables
- Implement db_engine session fixture with connectivity validation
- Add blank_db_schema and schema_factory fixtures creating per-test PostgreSQL schemas that automatically drop with CASCADE on teardown
- Add db_session and collectoss_db_session fixtures operating within isolated schemas
- Add docker-compose.test.yml for running database tests locally
- Add unit and integration tests verifying schema isolation and fixture lifecycle

Fixes chaoss#577

Signed-off-by: Dev10-sys <kalpanagola9897@gmail.com>
@Dev10-sys
Dev10-sys force-pushed the feat/db-test-fixtures branch from 8a3fd64 to 505ab5e Compare November 22, 2026 09:17
@MoralCode

Copy link
Copy Markdown
Contributor

@Dev10-sys what do you think of closing this PR and continuing to collaborate on #583 ? i think we have some good conversations started there already

@MoralCode

Copy link
Copy Markdown
Contributor

Many of the ideas from this PR have been incorporated into #583 (with attribution given). Closing as superseded.

@MoralCode MoralCode closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add test fixtures for database-backed tests

2 participants