-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github_parser.py
More file actions
143 lines (116 loc) · 5.04 KB
/
Copy pathtest_github_parser.py
File metadata and controls
143 lines (116 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import unittest
from unittest import mock
from github_parser import GitHubParser
class FakeResponse:
def __init__(self, status_code=200, json_data=None, text=""):
self.status_code = status_code
self._json = json_data if json_data is not None else {}
self.text = text
def json(self):
return self._json
class TestParseRepo(unittest.TestCase):
def test_standard(self):
self.assertEqual(
GitHubParser._parse_repo("https://github.com/octocat/Hello-World"),
("octocat", "Hello-World"),
)
def test_git_suffix(self):
self.assertEqual(
GitHubParser._parse_repo("https://github.com/octocat/Hello-World.git"),
("octocat", "Hello-World"),
)
def test_query_string(self):
self.assertEqual(
GitHubParser._parse_repo("github.com/octocat/Hello-World?tab=readme"),
("octocat", "Hello-World"),
)
def test_tree_path(self):
self.assertEqual(
GitHubParser._parse_repo("https://github.com/octocat/Hello-World/tree/main"),
("octocat", "Hello-World"),
)
def test_blob_path(self):
self.assertEqual(
GitHubParser._parse_repo("https://github.com/octocat/Hello-World/blob/main/README.md"),
("octocat", "Hello-World"),
)
def test_invalid(self):
self.assertIsNone(GitHubParser._parse_repo("notaurl"))
class TestErrorMessage(unittest.TestCase):
def test_statuses(self):
self.assertEqual(GitHubParser._error_message(401), "Token GitHub non valido o scaduto")
self.assertEqual(GitHubParser._error_message(403), "Rate-limit raggiunto o accesso negato")
self.assertEqual(GitHubParser._error_message(404), "Repository inesistente o privata")
self.assertEqual(GitHubParser._error_message(500), "Errore API GitHub (status 500)")
class TestBuildCodeContext(unittest.TestCase):
def setUp(self):
self.parser = GitHubParser()
def test_ranking_and_limit(self):
files = [
"src/utils/helper.py",
"src/main.py",
"src/app.py",
"src/controller/user.py",
"tests/test_main.py",
"other/whatever.py",
"zz.py",
"zz2.py",
]
self.parser._get = mock.Mock(return_value=FakeResponse(200, text="print('hi')"))
ctx = self.parser._build_code_context("user", "repo", "main", files, max_files=5)
blocks = ctx.split("--- INIZIO FILE: ")[1:]
names = [b.split(" ---")[0] for b in blocks]
self.assertEqual(len(names), 5)
self.assertEqual(names[0], "src/main.py")
self.assertEqual(names[1], "src/app.py")
def test_no_code_files(self):
ctx = self.parser._build_code_context("user", "repo", "main", ["README.md", "LICENSE"])
self.assertEqual(ctx, "Nessun file di codice trovato")
class TestGetRepoData(unittest.TestCase):
def setUp(self):
self.parser = GitHubParser()
def _mock_api(self, responses):
def fake_get(url, timeout=None):
if url.endswith("/repos/u/r"):
return responses["meta"]
if "/readme" in url:
return responses.get("readme", FakeResponse(404))
if "/git/trees/" in url:
return responses.get("tree", FakeResponse(404))
if "/languages" in url:
return responses.get("lang", FakeResponse(200))
return FakeResponse(200)
self.parser._get = mock.Mock(side_effect=fake_get)
def test_files_joined_correctly(self):
self._mock_api({
"meta": FakeResponse(200, json_data={"default_branch": "main"}),
"tree": FakeResponse(200, json_data={"tree": [
{"path": "src/main.py", "type": "blob"},
{"path": "src/app.py", "type": "blob"},
{"path": ".venv/lib/foo", "type": "blob"},
{"path": "sub/dir", "type": "tree"},
]}),
"lang": FakeResponse(200, json_data={"Python": 100}),
})
data = self.parser.get_repo_data("https://github.com/u/r")
self.assertEqual(data["files"], "src/main.py, src/app.py")
self.assertIn("Python", data["languages"])
def test_truncated_flag(self):
self._mock_api({
"meta": FakeResponse(200, json_data={"default_branch": "main"}),
"tree": FakeResponse(200, json_data={
"truncated": True,
"tree": [{"path": "a.py", "type": "blob"}],
}),
})
data = self.parser.get_repo_data("https://github.com/u/r")
self.assertIn("struttura troncata", data["files"])
def test_repo_error_404(self):
self._mock_api({"meta": FakeResponse(404)})
data = self.parser.get_repo_data("https://github.com/u/r")
self.assertIn("Repository inesistente o privata", data["error"])
def test_invalid_url(self):
data = self.parser.get_repo_data("notaurl")
self.assertIn("non valido", data["error"])
if __name__ == "__main__":
unittest.main()