Skip to content

Commit

Permalink
Unfold subtest.
Browse files Browse the repository at this point in the history
  • Loading branch information
WaVEV committed Nov 28, 2024
1 parent 3a654dc commit 507a026
Showing 1 changed file with 62 additions and 59 deletions.
121 changes: 62 additions & 59 deletions tests/queries_/test_objectid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,77 @@ def setUpTestData(cls):
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):
def test_filter_group_id_is_null_false(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])
qs = Tag.objects.filter(group_id__isnull=False).order_by("name")
self.assertSequenceEqual(qs, [self.t3, self.t4])

def test_filter_group_id_is_null_true(self):
"""Filter objects where group_id is null"""
qs = Tag.objects.filter(group_id__isnull=True).order_by("name")
self.assertSequenceEqual(qs, [self.t1, self.t2, self.t5])

def test_filter_group_id_equal_str(self):
"""Filter by group_id with a specific string value"""
qs = Tag.objects.filter(group_id=self.group_id_str_1).order_by("name")
self.assertSequenceEqual(qs, [self.t3])

def test_filter_group_id_equal_obj(self):
"""Filter by group_id with a specific ObjectId value"""
qs = Tag.objects.filter(group_id=self.group_id_obj_1).order_by("name")
self.assertSequenceEqual(qs, [self.t3])

def test_filter_group_id_in_str_values(self):
"""Filter by group_id with string values in a list"""
qs = Tag.objects.filter(group_id__in=[self.group_id_str_1, self.group_id_str_2]).order_by(
"name"
)
self.assertSequenceEqual(qs, [self.t3, self.t4])

def test_filter_group_id_in_obj_values(self):
"""Filter by group_id with ObjectId values in a list"""
qs = Tag.objects.filter(group_id__in=[self.group_id_obj_1, self.group_id_obj_2]).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])
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_filter_parent_by_children_values(self):
"""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])
qs = Tag.objects.filter(group_id__in=subquery).order_by("name")
self.assertSequenceEqual(qs, [self.t3, self.t4])

def test_filter_parent_by_children_values_str(self):
"""Query to select parents of children with specific string group_id"""
child_ids = Tag.objects.filter(group_id=self.group_id_str_1).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_parent_by_children_values_obj(self):
"""Query to select parents of children with specific ObjectId group_id"""
child_ids = Tag.objects.filter(group_id=self.group_id_obj_1).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_str(self):
"""Combine queries using union with string values"""
qs_a = Tag.objects.filter(group_id=self.group_id_str_1)
qs_b = Tag.objects.filter(group_id=self.group_id_str_2)
union_qs = qs_a.union(qs_b).order_by("name")
self.assertSequenceEqual(union_qs, [self.t3, self.t4])

def test_filter_group_id_union_with_obj(self):
"""Combine queries using union with ObjectId values"""
qs_a = Tag.objects.filter(group_id=self.group_id_obj_1)
qs_b = Tag.objects.filter(group_id=self.group_id_obj_2)
union_qs = qs_a.union(qs_b).order_by("name")
self.assertSequenceEqual(union_qs, [self.t3, self.t4])

def test_invalid_object_id(self):
"""Test invalid value in filter"""
Expand Down

0 comments on commit 507a026

Please sign in to comment.