Skip to content

Test isolation: test_visible_to_user_superuser_sees_all fails under sequential dir run #1845

Description

@JSv4

Summary

opencontractserver/tests/research/test_research_report_model.py::ResearchReportModelTestCase::test_visible_to_user_superuser_sees_all fails when the whole opencontractserver/tests/research/ directory is run sequentially (single worker), but passes when run alone or as a single file.

Root cause

The test hardcodes a global count:

self.assertEqual(ResearchReport.objects.visible_to_user(admin).count(), 2)

This assumes only the 2 ResearchReport rows created in the test exist. It is a TestCase (per-test transaction rollback), so within-file isolation holds — but a sibling test in the same directory that commits data (e.g. a TransactionTestCase) leaks rows that a superuser-sees-all query then counts, pushing the count above 2.

Why it's green on CI

CI runs pytest -n 4 --dist loadscope, which distributes test files across separate worker databases, so the polluting file and this test land on different DBs and never collide.

Reproduction

docker compose -f test.yml run --rm django pytest opencontractserver/tests/research/ --create-db
# -> 1 failed (test_visible_to_user_superuser_sees_all), 40 passed
docker compose -f test.yml run --rm django pytest \
  opencontractserver/tests/research/test_research_report_model.py --reuse-db
# -> 6 passed

Proposed fix

Scope the assertion to the test's own data instead of a global count, e.g. assert both created reports are assertIn(...) the superuser queryset (and the other user's report is visible to admin), rather than .count() == 2. Optionally audit the research/ test files for a TransactionTestCase that should clean up committed rows.

Context

Surfaced while babysitting PR #1836 (frontend-only; this is a pre-existing latent isolation weakness, not introduced by that PR).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions