From f24a0b3141e22f7261e36ac74d4c00137121c294 Mon Sep 17 00:00:00 2001 From: MartinGotelli Date: Wed, 6 Nov 2024 12:16:56 -0300 Subject: [PATCH] 192: Cover all edge cases of using values_list --- django_mock_queries/utils.py | 5 ++--- tests/test_mocks.py | 19 ------------------- tests/test_query.py | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/django_mock_queries/utils.py b/django_mock_queries/utils.py index 884ab31..a8bc774 100644 --- a/django_mock_queries/utils.py +++ b/django_mock_queries/utils.py @@ -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))) diff --git a/tests/test_mocks.py b/tests/test_mocks.py index 6bf897f..08367f5 100644 --- a/tests/test_mocks.py +++ b/tests/test_mocks.py @@ -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] \ No newline at end of file diff --git a/tests/test_query.py b/tests/test_query.py index 416e37c..c0dce46 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -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')