Skip to content

Flaky test: test_agencies_table_logic asserts INSERT where UPDATE expected #953

Description

@maxachis

Summary

tests/test_database.py::test_agencies_table_logic intermittently fails in CI with:

>       assert log["operation_type"] == OperationType.UPDATE.value
E       AssertionError: assert 'INSERT' == 'UPDATE'
E         - UPDATE
E         + INSERT
tests/test_database.py:649

Observed in CI for PR #952 (unrelated JWT/OAuth change): https://github.com/Police-Data-Accessibility-Project/data-sources-app/actions/runs/24569030432/job/71836812437

Likely cause

The test does:

  1. delete_change_log(db_client)
  2. Create agency (expects 1 INSERT log)
  3. Update agency name (expects 1 UPDATE log)
  4. logs = db_client.get_change_logs_for_table(Relations.AGENCIES) — asserts len(logs) == 2 and logs[1] is the UPDATE

The length assertion passes (2 logs present), but logs[1] is the INSERT rather than the UPDATE. This suggests get_change_logs_for_table does not return rows in a deterministic order — when the INSERT and UPDATE have identical or near-identical created_at timestamps, their relative order is undefined.

Suggested fix

Either:

  • Add a deterministic ORDER BY (e.g., created_at ASC, id ASC) in get_change_logs_for_table, or
  • Have the test look up the log by operation_type rather than list index.

Repro

Re-run CI on any PR; surfaces intermittently. Not caused by the changes in #952.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Dev testing

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions