Add support for comparing objects that are later deleted.

Review Request #14332 — Created Feb. 6, 2025 and updated — Latest diff uploaded

Information

django-assert-queries
main

Reviewers

When deleting an object via any relation (including when cascading
certain types of relations), we can end up in a situation where objects
can no longer be compared.

Model object comparisons check the primary key and model type. If one of
the primary keys is null, the model falls back to checking if the two
objects are the same exact instances.

What happens with deletion is that Django will perform a query
internally during delete object collection, delete them, and erase the
primary keys. This leads to several queries that are captured that
reference an object that no longer has a primary key and won't be the
same instances the unit test would have.

The solution to this is to listen in on object deletion and record any
primary keys being deleted. Then, during Q normalization, we can wrap
every model object referenced in a query to improve its equality checks,
factoring in instances that are deleted. This allows us to compare
instances.

All unit tests pass.

Tested this with unit tests in Review Board that were breaking due to
the deletion comparison behavior in Django.

Diff Revision 2 (Latest)

orig
1
2

Commits

First Last Summary ID Author
Add support for comparing objects that are later deleted.
When deleting an object via any relation (including when cascading certain types of relations), we can end up in a situation where objects can no longer be compared. Model object comparisons check the primary key and model type. If one of the primary keys is null, the model falls back to checking if the two objects are the same exact instances. What happens with deletion is that Django will perform a query internally during delete object collection, delete them, and erase the primary keys. This leads to several queries that are captured that reference an object that no longer has a primary key and won't be the same instances the unit test would have. The solution to this is to listen in on object deletion and record any primary keys being deleted. Then, during `Q` normalization, we can wrap every model object referenced in a query to improve its equality checks, factoring in instances that are deleted. This allows us to compare instances.
0df2ffa268ab14052910ebcd7a1b17cfa64ed47b Christian Hammond
django_assert_queries/query_catcher.py
django_assert_queries/query_comparator.py
django_assert_queries/tests/test_query_catcher.py
django_assert_queries/tests/test_query_comparator.py
Loading...