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

Distinct when asking flat values hashes incorrectly #192

Open
MartinGotelli opened this issue Nov 4, 2024 · 2 comments
Open

Distinct when asking flat values hashes incorrectly #192

MartinGotelli opened this issue Nov 4, 2024 · 2 comments
Labels

Comments

@MartinGotelli
Copy link
Contributor

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)))
@stefan6419846
Copy link
Collaborator

Thanks for the report. Feel free to submit a corresponding PR.

@MartinGotelli
Copy link
Contributor Author

Will do, probably this week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants