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

Add Query mock to MockSet #181

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

nfantone
Copy link
Contributor

@nfantone nfantone commented Sep 4, 2024

Mock QuerySet.query by means of a MagicMock following the sql.Query spec.

This makes it so attributes from QuerySet.query behave as expected, including callable properties.

As an example, the following currently breaks:

from django.db.models import Subquery

ms = MockSet()

Subquery(ms.only("field"))
# Raises TypeError: 'NoneType' object is not callable

This is due to the Subquery constructor trying to call query.clone() on the given QuerySet.

class Subquery(BaseExpression, Combinable):
    # ...

    def __init__(self, queryset, output_field=None, **extra):
        # Allow the usage of both QuerySet and sql.Query objects.
        self.query = getattr(queryset, "query", queryset).clone()

@stefan6419846
Copy link
Collaborator

Thanks for your PR. Could you please add corresponding tests as well?

If I understand #182 correctly, this will add some further breakage? In this case, we probably should look for a fix first.

@nfantone
Copy link
Contributor Author

nfantone commented Sep 4, 2024

@stefan6419846 I'm not sure if this is the right approach, if I'm being honest. My goal was to add support for Subquery but, ultimately, couldn't make it work.

AFAICT, changes from this PR wouldn't break anything (CI complains about flake8 formatting).

Let's continue the discussion in #182 and we can, then, revisit this.

@stefan6419846 stefan6419846 marked this pull request as draft September 6, 2024 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants