Coverage for tests/requests_mock.py: 100%

58 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-30 09:04 +0000

1import json 

2 

3from typing import NamedTuple 

4from collections import defaultdict 

5 

6import requests 

7 

8 

9class MockResponse(NamedTuple): 

10 status_code: int 

11 body: str 

12 content_type: str 

13 

14 def json(self): 

15 return json.loads(self.body) 

16 

17 @property 

18 def text(self): 

19 return self.body 

20 

21 

22class Mock: 

23 registry = defaultdict(list) 

24 index = defaultdict(int) 

25 gitea_added = False 

26 github_added = False 

27 

28 @classmethod 

29 def get(cls, url, status=200, body="", content_type="text/html"): 

30 cls.registry["get", url].append( 

31 MockResponse(status_code=status, body=body, content_type=content_type) 

32 ) 

33 

34 @classmethod 

35 def post(cls, url, status=200, body="", content_type="text/html"): 

36 cls.registry["post", url].append( 

37 MockResponse(status_code=status, body=body, content_type=content_type) 

38 ) 

39 

40 @classmethod 

41 def patch(cls, url, status=200, body="", content_type="text/html"): 

42 cls.registry["patch", url].append( 

43 MockResponse(status_code=status, body=body, content_type=content_type) 

44 ) 

45 

46 @classmethod 

47 def handle(cls, method): 

48 def handler(url, **_): 

49 options = cls.registry[method, url] 

50 index = cls.index[method, url] 

51 resp = options[index] 

52 cls.index[method, url] = (cls.index[method, url] + 1) % len(options) 

53 return resp 

54 

55 return handler 

56 

57 

58def add_gitea_mocks(gitea): 

59 ISSUE_ID = 1 

60 # --- create PR 

61 create_pr_url = f"{gitea.api}/repos/{gitea.owner}/{gitea.repo}/pulls" 

62 Mock.post(create_pr_url, body="", status=201) 

63 Mock.post(create_pr_url, body=f"issue_id:{ISSUE_ID}", status=409) 

64 # --- get existing body 

65 Mock.get( 

66 f"{gitea.api}/repos/{gitea.owner}/{gitea.repo}/pulls/{ISSUE_ID}", 

67 body=json.dumps({"body": "Previous body in PR description."}), 

68 content_type="application/json", 

69 ) 

70 # --- update body 

71 Mock.patch(f"{gitea.api}/repos/{gitea.owner}/{gitea.repo}/pulls/{ISSUE_ID}") 

72 # --- set commit status 

73 Mock.post(f"{gitea.api}/repos/{gitea.owner}/{gitea.repo}/statuses/{gitea.sha}") 

74 Mock.gitea_added = True 

75 

76 

77def add_github_mocks(github): 

78 ISSUE_ID = 1 

79 # --- create PR 

80 create_pr_url = f"{github.api}/repos/{github.owner}/{github.repo}/pulls" 

81 Mock.post(create_pr_url, body="", status=404) 

82 Mock.get( 

83 create_pr_url, 

84 body=json.dumps([{"number": ISSUE_ID}]), 

85 content_type="application/json", 

86 ) 

87 Mock.post(create_pr_url, body=f"issue_id:{ISSUE_ID}", status=409) 

88 # --- get existing body 

89 Mock.get( 

90 f"{github.api}/repos/{github.owner}/{github.repo}/pulls/{ISSUE_ID}", 

91 body=json.dumps({"body": "Already existing body in PR description."}), 

92 content_type="application/json", 

93 ) 

94 # --- update body 

95 Mock.patch(f"{github.api}/repos/{github.owner}/{github.repo}/pulls/{ISSUE_ID}") 

96 # --- set commit status 

97 Mock.post(f"{github.api}/repos/{github.owner}/{github.repo}/statuses/{github.sha}") 

98 Mock.github_added = True 

99 

100 

101requests.get = Mock.handle("get") 

102requests.post = Mock.handle("post") 

103requests.patch = Mock.handle("patch")