Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 3052b2a

Browse files
fix: search_base_query to handle desc ordering (#907)
1 parent 4546df1 commit 3052b2a

File tree

3 files changed

+43
-68
lines changed

3 files changed

+43
-68
lines changed

graphql_api/tests/test_test_analytics.py

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -598,64 +598,36 @@ def test_desc_failure_rate_ordering_on_test_results_with_after(self) -> None:
598598
pass_count=2,
599599
fail_count=3,
600600
)
601-
res = self.fetch_test_analytics(
602-
repo.name,
603-
"""testResults(ordering: { parameter: FAILURE_RATE, direction: DESC }, first: 1) { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }""",
604-
)
605-
606-
assert res["testResults"] == {
607-
"edges": [
608-
{"node": {"name": test_2.name, "failureRate": 0.6}},
609-
],
610-
"pageInfo": {
611-
"endCursor": base64_encode_string(f"0.6|{test_2.name}"),
612-
"hasNextPage": True,
613-
"hasPreviousPage": False,
614-
"startCursor": base64_encode_string(f"0.6|{test_2.name}"),
615-
},
616-
"totalCount": 2,
617-
}
618601

619-
res = self.fetch_test_analytics(
620-
repo.name,
621-
"""testResults(ordering: { parameter: FAILURE_RATE, direction: DESC }, first: 1, after: "%s") { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }"""
622-
% res["testResults"]["pageInfo"]["endCursor"],
602+
test_3 = TestFactory(repository=repo)
603+
_ = DailyTestRollupFactory(
604+
test=test_3,
605+
date=datetime.date.today(),
606+
repoid=repo.repoid,
607+
pass_count=1,
608+
fail_count=4,
623609
)
624-
625-
assert res["testResults"] == {
626-
"edges": [
627-
{"node": {"name": test.name, "failureRate": 0.2}},
628-
],
629-
"pageInfo": {
630-
"endCursor": base64_encode_string(f"0.2|{test.name}"),
631-
"hasNextPage": False,
632-
"hasPreviousPage": False,
633-
"startCursor": base64_encode_string(f"0.2|{test.name}"),
634-
},
635-
"totalCount": 2,
636-
}
637-
638610
res = self.fetch_test_analytics(
639611
repo.name,
640-
"""testResults(ordering: { parameter: FAILURE_RATE, direction: ASC }, first: 1) { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }""",
612+
"""testResults(ordering: { parameter: FAILURE_RATE, direction: DESC }, first: 1) { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }""",
641613
)
642614

643615
assert res["testResults"] == {
644616
"edges": [
645-
{"node": {"name": test.name, "failureRate": 0.2}},
617+
{"node": {"name": test_3.name, "failureRate": 0.8}},
646618
],
647619
"pageInfo": {
648-
"endCursor": base64_encode_string(f"0.2|{test.name}"),
620+
"endCursor": base64_encode_string(f"0.8|{test_3.name}"),
649621
"hasNextPage": True,
650622
"hasPreviousPage": False,
651-
"startCursor": base64_encode_string(f"0.2|{test.name}"),
623+
"startCursor": base64_encode_string(f"0.8|{test_3.name}"),
652624
},
653-
"totalCount": 2,
625+
"totalCount": 3,
654626
}
655627

656628
res = self.fetch_test_analytics(
657629
repo.name,
658-
"""testResults(ordering: { parameter: FAILURE_RATE, direction: ASC }, first: 1, after: "%s") { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }"""
630+
"""testResults(ordering: { parameter: FAILURE_RATE, direction: DESC }, first: 1, after: "%s") { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }"""
659631
% res["testResults"]["pageInfo"]["endCursor"],
660632
)
661633

@@ -665,48 +637,30 @@ def test_desc_failure_rate_ordering_on_test_results_with_after(self) -> None:
665637
],
666638
"pageInfo": {
667639
"endCursor": base64_encode_string(f"0.6|{test_2.name}"),
668-
"hasNextPage": False,
640+
"hasNextPage": True,
669641
"hasPreviousPage": False,
670642
"startCursor": base64_encode_string(f"0.6|{test_2.name}"),
671643
},
672-
"totalCount": 2,
644+
"totalCount": 3,
673645
}
674646

675647
res = self.fetch_test_analytics(
676648
repo.name,
677-
"""testResults(ordering: { parameter: FAILURE_RATE, direction: ASC }, last: 2) { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }""",
649+
"""testResults(ordering: { parameter: FAILURE_RATE, direction: DESC }, first: 1, after: "%s") { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }"""
650+
% res["testResults"]["pageInfo"]["endCursor"],
678651
)
679652

680653
assert res["testResults"] == {
681654
"edges": [
682-
{"node": {"name": test_2.name, "failureRate": 0.6}},
683655
{"node": {"name": test.name, "failureRate": 0.2}},
684656
],
685657
"pageInfo": {
686658
"endCursor": base64_encode_string(f"0.2|{test.name}"),
687659
"hasNextPage": False,
688660
"hasPreviousPage": False,
689-
"startCursor": base64_encode_string(f"0.6|{test_2.name}"),
690-
},
691-
"totalCount": 2,
692-
}
693-
694-
res = self.fetch_test_analytics(
695-
repo.name,
696-
"""testResults(ordering: { parameter: FAILURE_RATE, direction: ASC }, last: 1) { edges { node { name failureRate } }, pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor }, totalCount }""",
697-
)
698-
699-
assert res["testResults"] == {
700-
"edges": [
701-
{"node": {"name": test_2.name, "failureRate": 0.6}},
702-
],
703-
"pageInfo": {
704-
"endCursor": base64_encode_string(f"0.6|{test_2.name}"),
705-
"hasNextPage": False,
706-
"hasPreviousPage": True,
707-
"startCursor": base64_encode_string(f"0.6|{test_2.name}"),
661+
"startCursor": base64_encode_string(f"0.2|{test.name}"),
708662
},
709-
"totalCount": 2,
663+
"totalCount": 3,
710664
}
711665

712666
def test_flake_rate_filtering_on_test_results(self) -> None:

utils/test_results.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def search_base_query(
242242
rows: list[TestResultsRow],
243243
ordering: str,
244244
cursor: CursorValue | None,
245+
descending: bool = False,
245246
) -> list[TestResultsRow]:
246247
"""
247248
The reason we have to do this filtering in the application logic is because we need to get the total count of rows that
@@ -270,7 +271,12 @@ def compare(row: TestResultsRow) -> int:
270271
row_value = getattr(row, ordering)
271272
row_value_str = str(row_value)
272273
cursor_value_str = cursor.ordered_value
273-
return (row_value_str > cursor_value_str) - (row_value_str < cursor_value_str)
274+
row_is_greater = row_value_str > cursor_value_str
275+
row_is_less = row_value_str < cursor_value_str
276+
if descending:
277+
return row_is_less - row_is_greater
278+
else:
279+
return row_is_greater - row_is_less
274280

275281
left, right = 0, len(rows) - 1
276282
while left <= right:
@@ -442,8 +448,13 @@ def generate_test_results(
442448
page_size: int = first or last or 20
443449

444450
cursor_value = decode_cursor(after) if after else decode_cursor(before)
445-
446-
search_rows = search_base_query(rows, ordering, cursor_value)
451+
descending = ordering_direction == "DESC"
452+
search_rows = search_base_query(
453+
rows,
454+
ordering,
455+
cursor_value,
456+
descending=descending,
457+
)
447458

448459
page: list[dict[str, str | TestResultsRow]] = [
449460
{"cursor": encode_cursor(row, ordering), "node": row}

utils/tests/unit/test_search_base_query.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,13 @@ def test_search_base_query_with_missing_cursor_low_name_high_failure_rate():
5151
cursor = CursorValue(name="0", ordered_value="0.15")
5252
res = search_base_query(rows, "failure_rate", cursor)
5353
assert res == rows[-1:]
54+
55+
56+
def test_search_base_query_descending():
57+
# [(2, "0.2"), (1, "0.1"), (0, "0.0")]
58+
# ^
59+
# here's where the cursor is pointing at
60+
rows = [row_factory(str(i), float(i) * 0.1) for i in range(2, -1, -1)]
61+
cursor = CursorValue(name="0", ordered_value="0.15")
62+
res = search_base_query(rows, "failure_rate", cursor, descending=True)
63+
assert res == rows[1:]

0 commit comments

Comments
 (0)