import unittest import unittest.mock import requests import json from nixprstatus.pr import commit_in_branch, pr_merge_status 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) class TestPRMergeStatus(unittest.TestCase): @unittest.mock.patch("requests.get", side_effect=mocked_requests_get) def test_pr_merge_status_345583(self, mock_get): pr = 345583 branches = ["master", "nixos-unstable", "nixos-24.05"] res = pr_merge_status(pr, branches) self.assertTrue(res.merged) self.assertTrue(res.branches["master"]) self.assertTrue(res.branches["nixos-unstable"]) self.assertFalse(res.branches["nixos-24.05"]) class TestCommitInBranch(unittest.TestCase): @unittest.mock.patch("requests.get", side_effect=mocked_requests_get) def test_commit_in_branch_2c5fac3e(self, mock_get): commit_sha = "2c5fac3edf2d00d948253e392ec1604b29b38f14" res = commit_in_branch(commit_sha, "master") self.assertTrue(res) res = commit_in_branch(commit_sha, "nixos-24.05") self.assertFalse(res)