Skip to content

Commit

Permalink
Use attname instead of name when assigning the attributes values on t…
Browse files Browse the repository at this point in the history
…he mocked _do_update (#174)

This PR fixes bug #173.
ModelMocker._do_update now uses fields attname instead of name to support assigning PKs instead of instances to FK fields.
  • Loading branch information
stefan6419846 authored Apr 19, 2024
2 parents af5d4c7 + ef3bbc3 commit d7c7e32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django_mock_queries/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _do_update(self, *args, **_):
objects = self.objects.filter(pk=pk_val)

if objects.exists():
attrs = {field.name: value for field, _, value in values if value is not None}
attrs = {field.attname: value for field, _, value in values if value is not None}
self.objects.update(**attrs)
return True
else:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ def test_model_mocker_objects_create(self):
obj = Car.objects.create(speed=10)
self.assertEqual(Car.objects.get(pk=obj.id), obj)

def test_model_mocker_update_fk_from_instance(self):
with ModelMocker(Manufacturer):
with ModelMocker(Car, outer=False):
manufacturer = Manufacturer.objects.create(name='foo')
obj = Car.objects.create(speed=10, make=manufacturer)
obj.make = Manufacturer.objects.create(name='bar')
obj.save()

self.assertEqual(Car.objects.get(pk=obj.id).make.name, 'bar')

def test_model_mocker_with_custom_method(self):
with self.CarModelMocker(Car, 'validate_price') as mocker:
obj = Car()
Expand Down

0 comments on commit d7c7e32

Please sign in to comment.