Skip to content

Commit

Permalink
Ruff-format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Mar 26, 2024
1 parent 13303c3 commit f194bc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
22 changes: 14 additions & 8 deletions tests/testapp/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
InheritParentModel,
Model,
MultiOrderedModel,
OneToOneRelatedOrder,
ReferenceModel,
RelatedOrderModel,
StringOrderedModel,
TreeNodeIsOptional,
UnorderedModel,
UUIDModel,
RelatedOrderModel,
OneToOneRelatedOrder,
)
from tree_queries.compiler import SEPARATOR, TreeQuery
from tree_queries.query import pk
Expand Down Expand Up @@ -58,7 +58,11 @@ def test_no_attributes(self):
def test_attributes(self):
tree = self.create_tree()
# Ordering should be deterministic
child2_2 = Model.objects.with_tree_fields().order_siblings_by("order", "pk").get(pk=tree.child2_2.pk)
child2_2 = (
Model.objects.with_tree_fields()
.order_siblings_by("order", "pk")
.get(pk=tree.child2_2.pk)
)
self.assertEqual(child2_2.tree_depth, 2)
# Tree ordering is an array of the ranks assigned to a comment's
# ancestors when they are ordered without respect for tree relations.
Expand Down Expand Up @@ -154,7 +158,7 @@ def test_update_aggregate(self):
)

def test_values(self):
tree = self.create_tree()
self.create_tree()
self.assertEqual(
list(Model.objects.with_tree_fields().values("name")),
[
Expand Down Expand Up @@ -188,7 +192,7 @@ def test_values_ancestors(self):
)

def test_values_list(self):
tree = self.create_tree()
self.create_tree()
self.assertEqual(
list(Model.objects.with_tree_fields().values_list("name", flat=True)),
["root", "1", "1-1", "2", "2-1", "2-2"],
Expand Down Expand Up @@ -691,7 +695,9 @@ def test_multi_field_order(self):
parent=tree.child2, first_position=1, second_position=0, name="2-2"
)

nodes = MultiOrderedModel.objects.order_siblings_by("first_position", "-second_position")
nodes = MultiOrderedModel.objects.order_siblings_by(
"first_position", "-second_position"
)
self.assertEqual(
list(nodes),
[
Expand All @@ -701,7 +707,7 @@ def test_multi_field_order(self):
tree.child2,
tree.child2_1,
tree.child2_2,
]
],
)

def test_order_by_related(self):
Expand Down Expand Up @@ -739,5 +745,5 @@ def test_order_by_related(self):
tree.child2,
tree.child2_1,
tree.child2_2,
]
],
)
48 changes: 27 additions & 21 deletions tree_queries/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_sibling_order(self):


class TreeCompiler(SQLCompiler):
CTE_POSTGRESQL_WITH_INTEGER_ORDERING = """
CTE_POSTGRESQL = """
WITH RECURSIVE __rank_table(
"{pk}",
"{parent}",
Expand Down Expand Up @@ -74,7 +74,7 @@ class TreeCompiler(SQLCompiler):
)
"""

CTE_MYSQL_WITH_INTEGER_ORDERING = """
CTE_MYSQL = """
WITH RECURSIVE __rank_table({pk}, {parent}, rank_order) AS (
SELECT
{rank_pk},
Expand Down Expand Up @@ -105,7 +105,7 @@ class TreeCompiler(SQLCompiler):
)
"""

CTE_SQLITE3_WITH_INTEGER_ORDERING = """
CTE_SQLITE3 = """
WITH RECURSIVE __rank_table({pk}, {parent}, rank_order) AS (
SELECT
{rank_pk},
Expand Down Expand Up @@ -135,24 +135,31 @@ class TreeCompiler(SQLCompiler):
"""

def get_sibling_order_params(self):
'''
This method uses a simple django queryset to generate sql
that can be used to create the __rank_table that orders
siblings. This is done so that any joins required by order_by
are pre-calculated by django
'''
"""
This method uses a simple django queryset to generate sql
that can be used to create the __rank_table that orders
siblings. This is done so that any joins required by order_by
are pre-calculated by django
"""
sibling_order = self.query.get_sibling_order()

if isinstance(sibling_order, (list, tuple)):
order_fields = sibling_order
elif isinstance(sibling_order, str):
order_fields = [sibling_order]
else:
raise ValueError("Sibling order must be a string or a list or tuple of strings.")
raise ValueError(
"Sibling order must be a string or a list or tuple of strings."
)

# Use Django to make a SQL query whose parts can be repurposed for __rank_table
base_query = _find_tree_model(self.query.model).objects.only("pk", "parent").order_by(*order_fields).query

base_query = (
_find_tree_model(self.query.model)
.objects.only("pk", "parent")
.order_by(*order_fields)
.query
)

# Use the base compiler because we want vanilla sql and want to avoid recursion.
base_compiler = SQLCompiler(base_query, self.connection, None)
base_sql, base_params = base_compiler.as_sql()
Expand Down Expand Up @@ -194,12 +201,11 @@ def as_sql(self, *args, **kwargs):
# I am not confident that this is the perfect way to approach this
# problem but I just gotta stop worrying and trust the testsuite.
skip_tree_fields = (
(self.query.distinct and self.query.subquery)
or any( # pragma: no branch
# OK if generator is not consumed completely
annotation.is_summary
for alias, annotation in self.query.annotations.items()
)
self.query.distinct and self.query.subquery
) or any( # pragma: no branch
# OK if generator is not consumed completely
annotation.is_summary
for alias, annotation in self.query.annotations.items()
)
opts = _find_tree_model(self.query.model)._meta

Expand Down Expand Up @@ -251,11 +257,11 @@ def as_sql(self, *args, **kwargs):
)

if self.connection.vendor == "postgresql":
cte = self.CTE_POSTGRESQL_WITH_INTEGER_ORDERING
cte = self.CTE_POSTGRESQL
elif self.connection.vendor == "sqlite":
cte = self.CTE_SQLITE3_WITH_INTEGER_ORDERING
cte = self.CTE_SQLITE3
elif self.connection.vendor == "mysql":
cte = self.CTE_MYSQL_WITH_INTEGER_ORDERING
cte = self.CTE_MYSQL
sql_0, sql_1 = super().as_sql(*args, **kwargs)
explain = ""
if sql_0.startswith("EXPLAIN "):
Expand Down

0 comments on commit f194bc2

Please sign in to comment.