Add watchlist command
All checks were successful
test / test (pull_request) Successful in 34s
build / build (pull_request) Successful in 2m3s

This commit is contained in:
2024-10-09 22:21:28 +02:00
parent 99e0887505
commit 06abde6f6f
7 changed files with 184 additions and 32 deletions

0
tests/__init__.py Normal file
View File

32
tests/helpers/mocks.py Normal file
View File

@@ -0,0 +1,32 @@
import requests
import json
def mocked_requests_get(*args, **kwargs):
class MockedResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return json.loads(self.json_data)
def raise_for_status(self):
if self.status_code not in [200, 201]:
raise requests.exceptions.HTTPError()
if "pulls" in args[0]:
pr = args[0].split("/")[-1]
with open(f"tests/fixtures/pulls_{pr}.json") as f:
data = f.read()
return MockedResponse(data, 200)
elif "compare" in args[0]:
branch, commit_sha = args[0].split("/")[-1].split("...")
with open(f"tests/fixtures/compare_{branch}_{commit_sha}.json") as f:
data = f.read()
return MockedResponse(data, 200)
elif "comments" in args[0]:
pr = args[0].split("/")[-2]
with open(f"tests/fixtures/comments_{pr}.json") as f:
data = f.read()
return MockedResponse(data, 200)

View File

@@ -1,39 +1,8 @@
import unittest
import unittest.mock
import requests
import json
from nixprstatus.pr import commit_in_branch, pr_merge_status, commits_since
def mocked_requests_get(*args, **kwargs):
class MockedResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return json.loads(self.json_data)
def raise_for_status(self):
if self.status_code not in [200, 201]:
raise requests.exceptions.HTTPError()
if "pulls" in args[0]:
pr = args[0].split("/")[-1]
with open(f"tests/fixtures/pulls_{pr}.json") as f:
data = f.read()
return MockedResponse(data, 200)
elif "compare" in args[0]:
branch, commit_sha = args[0].split("/")[-1].split("...")
with open(f"tests/fixtures/compare_{branch}_{commit_sha}.json") as f:
data = f.read()
return MockedResponse(data, 200)
elif "comments" in args[0]:
pr = args[0].split("/")[-2]
with open(f"tests/fixtures/comments_{pr}.json") as f:
data = f.read()
return MockedResponse(data, 200)
from tests.helpers.mocks import mocked_requests_get
class TestPRMergeStatus(unittest.TestCase):

34
tests/test_watchlist.py Normal file
View File

@@ -0,0 +1,34 @@
from nixprstatus.watchlist import Watchlist, PRInfo
from tempfile import TemporaryDirectory
import unittest
from tests.helpers.mocks import mocked_requests_get
class TestWatchlist(unittest.TestCase):
def test_save_load(self):
with TemporaryDirectory() as d:
filename = f"{d}/test.json"
watchlist = Watchlist(prs=[PRInfo(pr=1, title="PR 1")])
watchlist.to_file(filename)
# Check that the file was written correctly
with open(filename, "r") as f:
self.assertEqual(watchlist.model_dump_json(), f.read())
# Check that the file can be read back
loaded = Watchlist.from_file(filename)
self.assertEqual(watchlist, loaded)
@unittest.mock.patch("requests.get", side_effect=mocked_requests_get)
def test_add_pr(self, mock_get):
w = Watchlist(prs=[])
w.add_pr(345583)
self.assertEqual(len(w.prs), 1)
self.assertEqual(w.prs[0].title, "wireshark: 4.2.6 -> 4.2.7")
def test_get_pr(self):
w = Watchlist(prs=[PRInfo(pr=1, title="PR 1")])
self.assertEqual(w.pr(1), PRInfo(pr=1, title="PR 1"))
self.assertEqual(w.pr(2), None)