-
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.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from bson import ObjectId | ||
from django.test import TestCase | ||
|
||
from .models import Tag | ||
|
||
|
||
class QueriesFilterByObjectId(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.group_id_str_1 = "1" * 24 | ||
cls.group_id_obj_1 = ObjectId(cls.group_id_str_1) | ||
cls.group_id_str_2 = "2" * 24 | ||
cls.group_id_obj_2 = ObjectId(cls.group_id_str_2) | ||
|
||
cls.t1 = Tag.objects.create(name="t1") | ||
cls.t2 = Tag.objects.create(name="t2", parent=cls.t1) | ||
cls.t3 = Tag.objects.create(name="t3", parent=cls.t1, group_id=cls.group_id_str_1) | ||
cls.t4 = Tag.objects.create(name="t4", parent=cls.t3, group_id=cls.group_id_obj_2) | ||
cls.t5 = Tag.objects.create(name="t5", parent=cls.t3) | ||
|
||
def test_filter_group_id_is_null(self): | ||
"""Filter objects where group_id is not null""" | ||
for value, expected in [(False, [self.t3, self.t4]), (True, [self.t1, self.t2, self.t5])]: | ||
with self.subTest(object_id=value): | ||
qs = Tag.objects.filter(group_id__isnull=value).order_by("name") | ||
self.assertSequenceEqual(qs, expected) | ||
|
||
def test_filter_group_id_equal_value(self): | ||
"""Filter by group_id with a specific value""" | ||
for value in [self.group_id_str_1, self.group_id_obj_1]: | ||
with self.subTest(object_id=value): | ||
qs = Tag.objects.filter(group_id=value).order_by("name") | ||
self.assertSequenceEqual(qs, [self.t3]) | ||
|
||
def test_filter_group_id_in_value(self): | ||
"""Filter by group_id where value is in a list""" | ||
test_cases = [ | ||
[self.group_id_str_1, self.group_id_str_2], | ||
[self.group_id_obj_1, self.group_id_obj_2], | ||
] | ||
for values in test_cases: | ||
with self.subTest(values=values): | ||
qs = Tag.objects.filter(group_id__in=values).order_by("name") | ||
self.assertSequenceEqual(qs, [self.t3, self.t4]) | ||
|
||
def test_filter_group_id_equal_subquery(self): | ||
"""Filter by group_id using a subquery""" | ||
subquery = Tag.objects.filter(name="t3").values("group_id") | ||
for value in [self.group_id_str_1, self.group_id_obj_1]: | ||
with self.subTest(object_id=value): | ||
qs = Tag.objects.filter(group_id__in=subquery).order_by("name") | ||
self.assertSequenceEqual(qs, [self.t3]) | ||
|
||
def test_filter_group_id_in_subquery(self): | ||
"""Filter by group_id using a subquery with multiple values""" | ||
subquery = Tag.objects.filter(name__in=["t3", "t4"]).values("group_id") | ||
test_cases = [ | ||
[self.group_id_str_1, self.group_id_str_2], | ||
[self.group_id_obj_1, self.group_id_obj_2], | ||
] | ||
for values in test_cases: | ||
with self.subTest(values=values): | ||
qs = Tag.objects.filter(group_id__in=subquery).order_by("name") | ||
self.assertSequenceEqual(qs, [self.t3, self.t4]) | ||
|
||
def test_union_children_to_select_parents(self): | ||
"""Union query to select parents of children based on group_id""" | ||
child_group_ids = [self.group_id_str_1, self.group_id_obj_1] | ||
for group_id in child_group_ids: | ||
with self.subTest(group_id=group_id): | ||
child_ids = Tag.objects.filter(group_id=group_id).values_list("id", flat=True) | ||
parent_qs = ( | ||
Tag.objects.filter(children__id__in=child_ids).distinct().order_by("name") | ||
) | ||
self.assertSequenceEqual(parent_qs, [self.t1]) | ||
|
||
def test_filter_group_id_union_with(self): | ||
"""Combine queries using union""" | ||
test_cases = [ | ||
(self.group_id_str_1, self.group_id_str_2), | ||
(self.group_id_obj_1, self.group_id_obj_2), | ||
] | ||
for value1, value2 in test_cases: | ||
with self.subTest(value1=value1, value2=value2): | ||
qs_a = Tag.objects.filter(group_id=value1) | ||
qs_b = Tag.objects.filter(group_id=value2) | ||
union_qs = qs_a.union(qs_b).order_by("name") | ||
self.assertSequenceEqual(union_qs, [self.t3, self.t4]) | ||
|
||
def test_invalid_object_id(self): | ||
"""Combine queries using union""" | ||
value = "value1" | ||
msg = f"Field 'group_id' expected an ObjectId but got '{value}'." | ||
with self.assertRaisesMessage(ValueError, msg): | ||
Tag.objects.filter(group_id=value) |