Skip to content

192: Add support to hash objects that are not Django models #193

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 0 deletions django_mock_queries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ def truncate(obj, kind):
def hash_dict(obj, *fields):
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
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