Opened 2 years ago

Last modified 23 months ago

#25493 new Cleanup/optimization

Model instances created with unittest.mock can raise confusing errors

Reported by: Josh Smeaton Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: josh.smeaton@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django can raise one of the following

  • ValueError: Failed to insert expression or;
  • ValueError: Failed to insert expression "<MagicMock name='create().id.resolve_expression()' id='139633074508184'>" on fundraising.Payment.stripe_charge_id. F() expressions can only be used to update, not to insert.

when using unittest.mock to mock the creation of an object. The djangoproject.com tests began throwing errors. Tim fixed these problems in this PR: https://github.com/django/djangoproject.com/pull/524

The PR adds return values for the id of columns that are used to create new instances to avoid these errors.

What I think is happening is that the lack of id is somehow causing the mock to be treated as a Col. The existence of a Col in a objects.create() call is what triggers the confusing error message. Since no F() expression has been used by the user, tracking down the cause is difficult.

We should try and figure out why the mock is being resolved to a Col and stop that from happening or improve the error message if that's not possible.

Attachments (1)

25493-test.diff (1.1 KB) - added by Tim Graham 2 years ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 2 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

The difficulty is that all hasattr checks on a MagicMock return True and any attribute or method access return MagicMock objects.

Changed 2 years ago by Tim Graham

Attachment: 25493-test.diff added

comment:2 Changed 23 months ago by Tim Graham

Version: 1.9a11.9
Note: See TracTickets for help on using tickets.
Back to Top