-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functions for converting a FilterSet class to a tree
- Loading branch information
Showing
8 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Functions for converting a FilterSet class to a tree and then to an input type.""" | ||
|
||
from typing import List, Optional, Sequence, Type | ||
|
||
from anytree import Node | ||
from django_filters import FilterSet | ||
|
||
|
||
def sequence_to_tree(values: Sequence[str]) -> Node: | ||
"""Convert a sequence to a tree.""" | ||
node: Optional[Node] = None | ||
for value in values: | ||
node = Node(value, parent=node) | ||
return node.root | ||
|
||
|
||
def try_add_sequence(tree: Node, values: Sequence[str]) -> bool: | ||
"""Try to add a sequence to a tree. | ||
Return a flag indicating whether the mutation was made. | ||
""" | ||
if tree.name == values[0]: | ||
for child in tree.children: | ||
is_mutated = try_add_sequence(child, values[1:]) | ||
if is_mutated: | ||
return True | ||
tree.children = (*tree.children, sequence_to_tree(values[1:])) | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
def filter_set_to_trees(filter_set: Type[FilterSet]) -> List[Node]: | ||
"""Convert a FilterSet class to a tree.""" | ||
trees: List[Node] = [] | ||
for filter_value in filter_set.base_filters.values(): | ||
values = (*filter_value.field_name.split('__'), filter_value.lookup_expr) | ||
if len(trees) == 0 or not any([try_add_sequence(tree, values) for tree in trees]): | ||
trees.append(sequence_to_tree(values)) | ||
return trees |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
"""Tests for graphene-django-filter project.""" | ||
|
||
from .connection_field import DjangoFilterConnectionFieldTest | ||
from .input_type_builders import FilterInputTypeBuilderTest, LookupInputTypeBuilderTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""FilterSet converters tests.""" | ||
|
||
from anytree import Node | ||
from anytree.exporter import DictExporter | ||
from django.test import TestCase | ||
from graphene_django_filter.filter_set_converters import ( | ||
filter_set_to_trees, | ||
sequence_to_tree, | ||
try_add_sequence, | ||
) | ||
|
||
from .filter_set import TaskFilter | ||
|
||
|
||
class FilterSetConverterTest(TestCase): | ||
"""FilterSet converters tests.""" | ||
|
||
def setUp(self) -> None: | ||
"""Set up TreeFunctions tests.""" | ||
self.tree = Node( | ||
'field1', children=( | ||
Node( | ||
'field2', children=( | ||
Node( | ||
'field3', children=( | ||
Node('field4'), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
) | ||
|
||
def test_list_to_tree(self) -> None: | ||
"""Test the sequence_to_tree function.""" | ||
self.assertEqual( | ||
DictExporter().export(sequence_to_tree(('field1', 'field2'))), | ||
{ | ||
'name': 'field1', | ||
'children': [{'name': 'field2'}], | ||
}, | ||
) | ||
|
||
def test_possible_try_add_iterable(self) -> None: | ||
"""Test the try_add_sequence function when adding a sequence is possible.""" | ||
is_mutated = try_add_sequence(self.tree, ('field1', 'field5', 'field6')) | ||
self.assertEqual(is_mutated, True) | ||
self.assertEqual( | ||
DictExporter().export(self.tree), { | ||
'name': 'field1', | ||
'children': [ | ||
{ | ||
'name': 'field2', | ||
'children': [ | ||
{ | ||
'name': 'field3', | ||
'children': [{'name': 'field4'}], | ||
}, | ||
], | ||
}, | ||
{ | ||
'name': 'field5', | ||
'children': [{'name': 'field6'}], | ||
}, | ||
], | ||
}, | ||
) | ||
|
||
def test_impossible_try_add_iterable(self) -> None: | ||
"""Test the try_add_sequence function when adding a sequence is impossible.""" | ||
is_mutated = try_add_sequence(self.tree, ('field5', 'field6')) | ||
self.assertEqual(is_mutated, False) | ||
|
||
def test_filter_set_to_trees(self) -> None: | ||
"""Test the filter_set_to_trees function.""" | ||
trees = filter_set_to_trees(TaskFilter) | ||
exporter = DictExporter() | ||
self.assertEqual( | ||
[exporter.export(tree) for tree in trees], [ | ||
{ | ||
'name': 'name', | ||
'children': [{'name': 'exact'}], | ||
}, | ||
{ | ||
'name': 'user', | ||
'children': [ | ||
{ | ||
'name': 'last_name', | ||
'children': [{'name': 'exact'}], | ||
}, | ||
{ | ||
'name': 'email', | ||
'children': [{'name': 'exact'}, {'name': 'contains'}], | ||
}, | ||
], | ||
}, | ||
], | ||
) |