Skip to content

Distinct when asking flat values hashes incorrectly #192

@MartinGotelli

Description

@MartinGotelli

When trying to build a distinct query for a char field while mocking the query set I have been having a problem. Basically the way the distinct works tries to build the hash for an object, and in this case is not an object, is a string.

    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]
tests/test_mocks.py:559 (TestMockers.test_distinct_for_flat_values)
['Toyota'] != ['Toyota', 'Ford']

Expected :['Toyota', 'Ford']
Actual   :['Toyota']

The problem seems to be in the hash_dict function, it ends up hashing an empty list

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}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions