Skip to content

Add testing infrastructure for database-backed tests - #583

Open
MoralCode wants to merge 41 commits into
mainfrom
ntdn/db-testing
Open

MoralCode wants to merge 41 commits into
mainfrom
ntdn/db-testing

Conversation

@MoralCode

@MoralCode MoralCode commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description
This PR adds the following things:

  • a docker-compose.yml containing a postgres DB for use in testing
  • a github actions CI job for running the database-backed tests
  • Some test fixtures to allow unit tests to easily get sessions in this test DB, build the schema with the currently-valid structure, etc
  • a unit test that does a very simple query to test that the database can be connected to and the schema exists
  • docs updates to provide instructions for how to use this new testing infrastructure for the database tests.
  • cleans up some older fixtures from the codebase that are not currently being used

This PR fixes #577

The testing infrastructure in this PR is helpful for testing some upcoming critical changes, such as #580 and #450

Notes for Reviewers

I have yet to evaluate how these tests interact with @EngCaioFonseca's code coverage changes. done

Because the CollectOSS-specific tests create and drop schemas regularly, including the same names that the live CollectOSS schema uses, it WILL cause damage if it is pointed at a database that matters. That is why it uses a different environment variable for specifying a test database (maybe the wording of this var should be more strongly indicating danger)

If the database tests fail or are killed halfway through, the next run will try to create one of these identically named schemas (assuming an empty db), when it encounters the one that already exists in the DB, the tests will crash with a duplicate schema error. This "bug" is being left in on purpose to hopefully warn users that something may already exist and avoid unintended data loss.

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.

If AI tools were used, please provide details below:
- What tools were used? Cursor, GLM 5.3 High
- How were these tools used? Probably code gen, Integrating the ideas from #582 to deduplicate the PRs
- Did you review these outputs before submitting this PR? This PR is based on changes that were drafted out a long time ago, i have been through it a few times since to clean it up and test it. this cleanup was done without AI assistance. The integration of the other PRs changes was started with AI and organized/selected by me when organizing them into commits.

Comment thread conftest.py Fixed
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Generated-By: GLM 5.3 High
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
…backed tests

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Database tests will run if BOTH:
1. they are not specifically excluded using the `database` label
2. the `COLLECTOSS_TEST_DB` environment variable is present and contains a database URL to connect to

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
…rk in the future

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 17.04%. Comparing base (0bb5f6b) to head (39f6307).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
Flag Coverage Δ
database 14.77% <ø> (?)
unit 17.04% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Dev10-sys Dev10-sys left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The docker-compose setup and dedicated CI service container job look great. Left a few inline notes on fixture scoping, fallback credentials, and parallel schema isolation vs truncate.

Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Outdated
test_db_name = "test_db_template_" + uuid.uuid4().hex
create_database(conn, cursor, test_db_name)
@pytest.fixture(scope="session")
def db_session(database_engine):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should db_session be function-scoped rather than session-scoped? If a test mutates state or errors mid-transaction, a session-scoped session will retain cached entities in its identity map or remain in an aborted transaction state (InFailedSqlTransaction) for subsequent tests.

Comment thread conftest.py Resolved
Comment thread tests/test_tests/test_database.py Resolved
MoralCode and others added 3 commits November 23, 2026 14:12
Generated-By: GLM 5.3 High
Co-Authored-By: Dev10-sys <kalpanagola9897@gmail.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
Comment thread conftest.py Resolved
MoralCode and others added 5 commits November 23, 2026 14:19
…e env var is not present

Generated-By: GLM 5.3 High
Co-Authored-By: Dev10-sys <kalpanagola9897@gmail.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
…nstances

Generated-By: GLM 5.3 High
Co-Authored-By: Dev10-sys <kalpanagola9897@gmail.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
… tests as DB tests

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Generated-By: GLM 5.3 High
Co-Authored-By: Dev10-sys <kalpanagola9897@gmail.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
MoralCode and others added 6 commits November 23, 2026 14:19
Generated-By: GLM 5.3 High
Co-Authored-By: Dev10-sys <kalpanagola9897@gmail.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
…schemas

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
… session fixture better

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
fixtures that depend on other fixtures shadow their names in their parameters
this is normal and expected and reviewdog shouldn't flag it.

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
…o-skipping

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Comment thread conftest.py Resolved
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Comment thread tests/test_routes/test_util_routes.py Resolved
@MoralCode

Copy link
Copy Markdown
Contributor Author

@Dev10-sys ok, i think i have essentially incorporated the structure from your PR into this one (and also built on it, by creating a whole group of fixtures that build CollectOSS-specific schema).

This required adding a feature to make the randomization of the schema names optional because the CollectOSS schema is pretty much hardcoded to use three specific schema names (data, operations, and spdx although that last one is unused AFAIK)

I also removed some of the stuff surrounding passing the DB url in via CLI args (until theres a concrete need for it - to keep maintainance surface manageable and not overcompicate the docs for newcomers)

Curious what you think of this version of the test fixtures!

@MoralCode MoralCode added the ready ready to merge, likely waiting for unit tests or something label Nov 23, 2026
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
@MoralCode MoralCode removed the ready ready to merge, likely waiting for unit tests or something label Nov 23, 2026
The previous guidance was bad

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
…something important

Signed-off-by: Adrian Edwards <adredwar@redhat.com>
Comment thread conftest.py Resolved
The full set of commands needed to run the database-backed tests locally are:
.. code-block:: bash

docker compose -f tests/docker-compose.yml up -d

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should we add --wait here (matching the comment in tests/docker-compose.yml)? Without --wait, docker compose up -d returns before postgres finishes initdb, which can cause a quick connection race if someone runs uv run pytest right after.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

does --wait work with -d?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yep, --wait works with -d in Docker Compose v2 (it actually implies -d). It starts the container in the background and waits for the pg_isready healthcheck to pass before exiting.

Comment thread tests/test_routes/test_util_routes.py Resolved
Comment thread tests/test_tests/test_database.py Resolved
Signed-off-by: Adrian Edwards <adredwar@redhat.com>
@MoralCode
MoralCode requested a review from drkrillo November 24, 2026 18:11
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

3 participants