Opened 3 weeks ago

Last modified 11 days ago

#37175 assigned New feature

Add database backend methods to get hardcoded or nonexistent primary key values for tests

Reported by: Tim Graham Owned by: Tim Graham
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Simon Charette Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

While hardcoding primary key values in the Django test suite is avoided as much as possible, sometimes there are legitimate uses. The existing hardcoded integer values, however, don't work on databases that don't use integer primary key values. For example, MongoDB uses DEFAULT_AUTO_FIELD = "django_mongodb_backend.fields.ObjectIdAutoField".

I propose adding new methods DatabaseOperations.get_hardcoded_pk() and get_nonexistent_pk() to allow a database backend to provide suitable values, minimizing the size of the Django fork needed for running the test suite on such databases.

Change History (3)

comment:1 by Tim Graham, 3 weeks ago

Has patch: set

comment:2 by Natalia Bidart, 13 days ago

Cc: Simon Charette added
Triage Stage: UnreviewedAccepted

Hello Tim! Thank you for taking the time to create the report. I understand your concerns and the issue that this raises for other backends. I agree we could improve this situation by providing, potentially, some hooks at the database backend layer, but I also think that many of the tests currently hardcoding the PKs could be re-worked to not use a PK at all, or at least not assume is an int.

Right now we are prioritizing 6.1 beta work, but I'll commit to do a more informed triage/review of this ticket after beta (likely next week). I'm accepting it right now since the problem statement is real, but I'd like to evaluate other options for the solution, ideally a solution that would not require a get_hardcoded_pk(). Have you considered posting in the forum to see what our creative community can suggest?

Thanks for your patience!

Last edited 13 days ago by Natalia Bidart (previous) (diff)

comment:3 by Tim Graham, 12 days ago

Description: modified (diff)

First, I removed hardcoded pks where they aren't needed (PR 21364).

Trying to rework the tests that require hardcoded pks may be possible in some cases, however, I believe some form of get_hardcoded_pk() API will be needed, so I feel the most expedient path forward is to merge the patch and leave test refactoring to a later task.

Incidentally, I didn't update serializers and fixtures tests to use get_hardcoded_pk() since it's a bit trickier, but this would also be a useful follow up task. There's a commit on the Django fork used to run the test suite on MongoDB that updates all fixtures to use ObjectIds instead of integers (e.g. "pk": 1, because "pk": "000000000000000000000001",) and this would remain, but the hardcoded pks in files such as tests/fixtures_regress/tests.py could be replaced.

At your request, I created forum topic Adding database backend methods to get hardcoded or nonexistent primary key values for tests to solicit feedback.

Last edited 11 days ago by Tim Graham (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top