Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from forge.reviews import ANSWERED, CHANGES_REQUESTED, PullView, Review, Thread
|
||||
from failure import Failure
|
||||
from forge import reviews
|
||||
|
||||
ASKED = {"author": {"login": "rev"}, "state": CHANGES_REQUESTED, "submittedAt": "2026-11-02T10:00:00Z", "commit": {"oid": "abc"}}
|
||||
PULL = {"isDraft": False, "reviewDecision": CHANGES_REQUESTED, "author": {"login": "me"}, "reviews": [ASKED], "comments": []}
|
||||
|
||||
|
||||
def printing(answer: object):
|
||||
"""A run whose command prints answer as JSON."""
|
||||
return lambda args: json.dumps(answer)
|
||||
|
||||
|
||||
class Faulty:
|
||||
"""A pull request's number or head whose use in a comparison or in a command raises a fault of our own."""
|
||||
|
||||
def __ne__(self, other: object) -> bool:
|
||||
raise TypeError("ours")
|
||||
|
||||
def __format__(self, spec: str) -> str:
|
||||
raise TypeError("ours")
|
||||
|
||||
|
||||
class ThePullRequestAsViewed(unittest.TestCase):
|
||||
def test_parses_what_gh_prints_into_one_value(self):
|
||||
pending = {"author": {"login": "me"}, "state": "PENDING", "submittedAt": None, "commit": None}
|
||||
printed = json.dumps(PULL | {"reviews": [ASKED, pending], "comments": [{"author": {"login": "me"}, "createdAt": "2026-11-02T11:00:00Z"}, {"author": {"login": "rev"}, "createdAt": "2026-11-02T12:00:00Z"}]})
|
||||
self.assertEqual(reviews.pull_view(printed), PullView(False, CHANGES_REQUESTED, (Review("rev", CHANGES_REQUESTED, "2026-11-02T10:00:00Z", "abc"), Review("me", "PENDING", "", "")), ("", "2026-11-02T11:00:00Z")))
|
||||
|
||||
def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self):
|
||||
unshaped = ("not json", "[]", json.dumps(PULL | {"reviews": None}), json.dumps(PULL | {"author": None}), json.dumps(PULL | {"reviews": [ASKED | {"commit": None}]}), json.dumps(PULL | {"reviews": [ASKED | {"submittedAt": 7}]}), json.dumps({key: value for key, value in PULL.items() if key != "isDraft"}))
|
||||
for printed in unshaped:
|
||||
with self.subTest(printed=printed):
|
||||
self.assertEqual(reviews.pull_view(printed), Failure(f"gh pr view printed no review decision: {printed[:200]}"))
|
||||
self.assertEqual(reviews.answered_decision("7", "abc", lambda args: printed), Failure(f"gh pr view printed no review decision: {printed[:200]}"))
|
||||
|
||||
|
||||
class WhetherTheWorkerAnswered(unittest.TestCase):
|
||||
def view(self, *others: Review, said: tuple[str, ...] = ()) -> PullView:
|
||||
return PullView(False, CHANGES_REQUESTED, (Review("rev", CHANGES_REQUESTED, "2026-11-02T10:00:00Z", "abc"), *others), tuple(review.submitted_at for review in others if review.author == "me") + said)
|
||||
|
||||
def test_a_new_head_or_a_later_word_of_the_authors_answers_the_newest_request(self):
|
||||
self.assertFalse(reviews.answered(self.view(), "abc"))
|
||||
self.assertTrue(reviews.answered(self.view(), "def"))
|
||||
self.assertTrue(reviews.answered(self.view(said=("2026-11-02T10:30:00Z",)), "abc"))
|
||||
self.assertFalse(reviews.answered(self.view(said=("2026-11-02T09:00:00Z", "")), "abc"))
|
||||
self.assertTrue(reviews.answered(self.view(Review("me", "COMMENTED", "2026-11-02T10:30:00Z", "")), "abc"))
|
||||
self.assertFalse(reviews.answered(PullView(False, "", (), ()), "abc"))
|
||||
|
||||
def test_the_decision_reads_answered_once_the_worker_answered(self):
|
||||
self.assertEqual(reviews.answered_decision("7", "abc", printing(PULL)), CHANGES_REQUESTED)
|
||||
self.assertEqual(reviews.answered_decision("7", "def", printing(PULL)), ANSWERED)
|
||||
self.assertIsNone(reviews.answered_decision("7", "abc", printing(PULL | {"isDraft": True})))
|
||||
self.assertEqual(reviews.answered_decision("7", "abc", printing(PULL | {"reviewDecision": None, "reviews": []})), "")
|
||||
|
||||
def test_a_fault_in_the_decision_is_not_reported_as_the_forges(self):
|
||||
with self.assertRaisesRegex(TypeError, "ours"):
|
||||
reviews.answered_decision("7", Faulty(), printing(PULL))
|
||||
|
||||
|
||||
class TheThreadsThatWait(unittest.TestCase):
|
||||
def printed(self, *threads: tuple[str, bool, str]) -> str:
|
||||
nodes = [{"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}} for path, resolved, last in threads]
|
||||
return json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": nodes}}}}})
|
||||
|
||||
def test_parses_what_gh_prints_into_the_author_and_each_thread(self):
|
||||
self.assertEqual(reviews.threads_read(self.printed(("a.py", False, "rev"), ("b.py", True, "me"))), ("me", (Thread("a.py", False, ("rev",)), Thread("b.py", True, ("me",)))))
|
||||
|
||||
def test_a_thread_waits_while_unresolved_with_a_reviewers_last_word(self):
|
||||
printed = self.printed(("b.py", False, "rev"), ("a.py", False, "rev"), ("a.py", False, "rev"), ("c.py", True, "rev"), ("d.py", False, "me"))
|
||||
self.assertEqual(reviews.waiting_threads("7", lambda args: printed), ["a.py", "b.py"])
|
||||
|
||||
def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self):
|
||||
for printed in ("not json", "{}", json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False}]}}}}})):
|
||||
with self.subTest(printed=printed):
|
||||
self.assertEqual(reviews.waiting_threads("7", lambda args: printed), Failure(f"gh api graphql printed no review threads: {printed[:200]}"))
|
||||
|
||||
def test_a_fault_in_the_choice_is_not_reported_as_the_forges(self):
|
||||
with self.assertRaisesRegex(TypeError, "ours"):
|
||||
reviews.waiting_threads(Faulty(), lambda args: self.printed(("a.py", False, "rev")))
|
||||
|
||||
|
||||
class TheDecisionOnGitea(unittest.TestCase):
|
||||
def decision(self, answer: object):
|
||||
return reviews.gitea_decision("7", printing(answer))
|
||||
|
||||
def test_a_title_tells_a_draft_and_an_answer_without_one_is_a_failure(self):
|
||||
self.assertIsNone(self.decision({"title": "wip: later"}))
|
||||
self.assertEqual(self.decision({"title": "Fix it"}), "")
|
||||
self.assertEqual(self.decision({"title": 7}), Failure("tea api printed no title of pull request 7: {'title': 7}"))
|
||||
self.assertEqual(self.decision([]), Failure("tea api printed no title of pull request 7: []"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user