Skip to content

Commit

Permalink
192: Cover all edge cases of using values_list
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGotelli committed Nov 6, 2024
1 parent 825011b commit f24a0b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
5 changes: 2 additions & 3 deletions django_mock_queries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,10 @@ def truncate(obj, kind):


def hash_dict(obj, *fields):
if not isinstance(obj, models.Model):
return hash(obj)

field_names = fields or find_field_names(obj, concrete_only=True)[1]
obj_values = {f: get_field_value(obj, f) for f in field_names}
if not obj_values and not fields:
obj_values = {"field": obj}

return hash(tuple(sorted((k, v) for k, v in obj_values.items() if not fields or k in fields)))

Expand Down
19 changes: 0 additions & 19 deletions tests/test_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,22 +587,3 @@ def test_model_mocker_does_not_interfere_with_non_mocked_models(self):

with self.assertRaises(NotSupportedError):
CarVariation.objects.all().delete()

def test_distinct_for_flat_values(self):
with ModelMocker(Manufacturer):
toyota = Manufacturer.objects.create(name="Toyota")
ford = Manufacturer.objects.create(name="Ford")
distinct_names = Manufacturer.objects.values_list("name", flat=True).distinct()
assert list(distinct_names) == [toyota.name, ford.name]

def test_distinct_for_objects(self):
with ModelMocker(Manufacturer):
toyota = Manufacturer.objects.create(name="Toyota")
ford = Manufacturer.objects.create(name="Ford")
ford_2 = Manufacturer.objects.create(name="Ford")
distinct_objects = Manufacturer.objects.distinct()
assert list(distinct_objects) == [toyota, ford, ford_2]
distinct_objects = Manufacturer.objects.distinct("name")
assert list(distinct_objects) == [toyota, ford]
distinct_objects = Manufacturer.objects.distinct("id")
assert list(distinct_objects) == [toyota, ford, ford_2]
22 changes: 22 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,28 @@ def test_query_distinct_values(self):
]
assert results == expected, results

def test_query_distinct_values_list(self):
item_1 = MockModel(foo=1, mock_name='item_1')
item_2 = MockModel(foo=2, mock_name='item_2')
item_3 = MockModel(foo=3, mock_name='item_3')

self.mock_set.add(item_2, item_3, item_1, item_3)
results = list(self.mock_set.values_list('foo').distinct().order_by('foo'))

expected = [(item_2.foo,), (item_3.foo,), (item_1.foo,)]
assert results == expected, results

def test_query_distinct_values_list_flatten(self):
item_1 = MockModel(foo=1, mock_name='item_1')
item_2 = MockModel(foo=2, mock_name='item_2')
item_3 = MockModel(foo=3, mock_name='item_3')

self.mock_set.add(item_2, item_3, item_1, item_3)
results = list(self.mock_set.values_list('foo', flat=True).distinct().order_by('foo'))

expected = [item_2.foo, item_3.foo, item_1.foo]
assert results == expected, results

def test_query_distinct_with_fields(self):
item_1 = MockModel(foo=1, bar='c', foo_bar='x', mock_name='item_1')
item_2 = MockModel(foo=2, bar='a', foo_bar='y', mock_name='item_2')
Expand Down

0 comments on commit f24a0b3

Please sign in to comment.