Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#173: Use attname instead of name when assigning the attributes values on the mocked _do_update #174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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