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

EMF support annotations, group by, and ordering #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

WaVEV
Copy link
Collaborator

@WaVEV WaVEV commented Jan 18, 2025

No description provided.

@WaVEV WaVEV force-pushed the EMF-support-group-by branch from 8c0e138 to d7d40c3 Compare January 18, 2025 05:12
@WaVEV WaVEV requested a review from timgraham January 18, 2025 05:13
@WaVEV WaVEV changed the title support annotations, group by, and ordering EMF support annotations, group by, and ordering Jan 18, 2025
@@ -104,6 +113,46 @@ def test_nested(self):
)
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])

def truncate_ms(self, value):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper methods like this should typically be organized at the top of a class so they don't lost among test methods, however, I suppose we should make it a function in this file so we don't have to repeat the method on every class (the move can be a separate commit in this PR that precedes the fix).

(Holder.objects.create(data=Data(integer=x)) for x in range(6)),
key=lambda x: x.data.integer,
)
query = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use qs = since "query" usually refers to SQL or, in our case, MQL.

"""Truncate microsends to millisecond precision as supported by MongoDB."""
return value.replace(microsecond=(value.microsecond // 1000) * 1000)

def test_ordering_by_embedded_field(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to have, but it passes without the fix, so it should be a separate commit. (I think the fix is mostly about group by, not ordering without grouping.) I would sort these tests above test_nested since they also use the Holder model.

key=lambda x: x.data.integer,
)
query = (
Holder.objects.annotate(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to add some comments about what this query is calculating since it's not straightforward.

.annotate(max_auto_now=Max("data__auto_now"))
.order_by("data__integer")
)
query_response = [{**e, "max_auto_now": self.truncate_ms(e["max_auto_now"])} for e in query]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is truncate needed here? I'd expect MongoDB to return an already truncated result.

self.assertSequenceEqual(query, expected)

def test_ordering_grouping_by_embedded_field(self):
expected = sorted(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say expected_objs since there is still some modification in calculating the expected result.

],
)

def test_ordering_grouping_by_sum(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it's hard for me to reason about the expected result of query, so some explanatory comment would be useful. For example, the "cheat sheet" at https://docs.djangoproject.com/en/dev/topics/db/aggregation/ is easy to follow.

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