-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_db.py
More file actions
299 lines (248 loc) · 10.3 KB
/
Copy pathtest_cli_db.py
File metadata and controls
299 lines (248 loc) · 10.3 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from unittest.mock import patch, MagicMock
from click.testing import CliRunner
from gh_similarity_detector.cli.main import main
from gh_similarity_detector.infrastructure.storage.fingerprint_db import FingerprintDB
from gh_similarity_detector.models.entities import Project, Module, FingerprintSet
from gh_similarity_detector.models.enums import ModuleType
def _seed_db(db_path: str) -> str:
db = FingerprintDB(db_path)
proj = Project(name="test/project", source="test", language="python")
mod = Module(
name="foo",
file_path="foo.py",
module_type=ModuleType.FUNCTION,
source_code="def foo(): pass",
start_line=1,
end_line=1,
language="python",
project_id=proj.id,
)
fp = FingerprintSet(module_id=mod.id, winnowing_fingerprints={1, 2, 3})
db.add_project(proj, {"foo.py": [mod]}, {mod.id: fp})
return proj.id
class TestDbInit:
def test_db_init(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
runner = CliRunner()
result = runner.invoke(main, ["db", "init", "--path", db_path])
assert result.exit_code == 0
assert "指纹库已初始化" in result.output
assert "项目数" in result.output
class TestDbAdd:
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_add_success(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
mock_pipeline = MagicMock()
mock_pipeline.add_to_db.return_value = True
mock_pipeline.fingerprint_db.get_stats.return_value = {
"project_count": 1,
"module_count": 2,
"fingerprint_count": 10,
}
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(main, ["db", "add", "-p", "test/project", "--db", db_path])
assert result.exit_code == 0
assert "项目已添加到指纹库" in result.output
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_add_failure(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
mock_pipeline = MagicMock()
mock_pipeline.add_to_db.return_value = False
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(main, ["db", "add", "-p", "test/project", "--db", db_path])
assert result.exit_code == 0
assert "添加失败" in result.output
class TestDbUpdate:
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_update_has_update(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
mock_pipeline = MagicMock()
mock_pipeline.update_db.return_value = True
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(main, ["db", "update", "-p", "test/project", "--db", db_path])
assert result.exit_code == 0
assert "项目指纹已更新" in result.output
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_update_no_update(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
mock_pipeline = MagicMock()
mock_pipeline.update_db.return_value = False
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(main, ["db", "update", "-p", "test/project", "--db", db_path])
assert result.exit_code == 0
assert "项目指纹已是最新" in result.output
class TestDbStats:
def test_db_stats_db_not_exist(self):
runner = CliRunner()
result = runner.invoke(main, ["db", "stats", "--db", "/nonexistent/db.sqlite"])
assert result.exit_code == 0
assert "指纹库不存在" in result.output
def test_db_stats_empty(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
from gh_similarity_detector.infrastructure.storage.fingerprint_db import FingerprintDB
FingerprintDB(db_path)
runner = CliRunner()
result = runner.invoke(main, ["db", "stats", "--db", db_path])
assert result.exit_code == 0
assert "项目数" in result.output
assert "模块数" in result.output
def test_db_stats_with_project(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
_seed_db(db_path)
runner = CliRunner()
result = runner.invoke(main, ["db", "stats", "--db", db_path])
assert result.exit_code == 0
assert "项目列表" in result.output
class TestDbList:
def test_db_list_db_not_exist(self):
runner = CliRunner()
result = runner.invoke(main, ["db", "list", "--db", "/nonexistent/db.sqlite"])
assert result.exit_code == 0
assert "指纹库不存在" in result.output
def test_db_list_empty(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
from gh_similarity_detector.infrastructure.storage.fingerprint_db import FingerprintDB
FingerprintDB(db_path)
runner = CliRunner()
result = runner.invoke(main, ["db", "list", "--db", db_path])
assert result.exit_code == 0
assert "指纹库为空" in result.output
def test_db_list_with_project(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
_seed_db(db_path)
runner = CliRunner()
result = runner.invoke(main, ["db", "list", "--db", db_path])
assert result.exit_code == 0
assert "项目列表" in result.output
assert "test/project" in result.output
class TestDbDelete:
def test_db_delete_with_force(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
project_id = _seed_db(db_path)
runner = CliRunner()
result = runner.invoke(main, ["db", "delete", "-p", project_id, "--db", db_path, "--force"])
assert result.exit_code == 0
assert "项目已删除" in result.output
def test_db_delete_not_exist(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
FingerprintDB(db_path)
runner = CliRunner()
result = runner.invoke(
main, ["db", "delete", "-p", "nonexistent", "--db", db_path, "--force"]
)
assert result.exit_code == 0
assert "项目不存在" in result.output
def test_db_delete_confirm_yes(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
project_id = _seed_db(db_path)
runner = CliRunner()
result = runner.invoke(
main, ["db", "delete", "-p", project_id, "--db", db_path], input="y\n"
)
assert result.exit_code == 0
assert "项目已删除" in result.output
def test_db_delete_confirm_no(self, tmp_path):
db_path = str(tmp_path / "test.sqlite")
project_id = _seed_db(db_path)
runner = CliRunner()
result = runner.invoke(
main, ["db", "delete", "-p", project_id, "--db", db_path], input="n\n"
)
assert result.exit_code == 0
class TestDbImport:
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_import_success(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
import_file = tmp_path / "projects.txt"
import_file.write_text(
"https://github.com/user/repo1\nhttps://github.com/user/repo2\n", encoding="utf-8"
)
mock_pipeline = MagicMock()
mock_pipeline.add_to_db.return_value = True
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(
main,
[
"db",
"import",
"-f",
str(import_file),
"--db",
db_path,
],
)
assert result.exit_code == 0
assert "批量导入" in result.output
assert "成功" in result.output
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_import_empty_file(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
import_file = tmp_path / "projects.txt"
import_file.write_text("# comment\n\n", encoding="utf-8")
mock_pipeline = MagicMock()
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(
main,
[
"db",
"import",
"-f",
str(import_file),
"--db",
db_path,
],
)
assert result.exit_code == 0
assert "项目列表为空" in result.output
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_import_with_failure(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
import_file = tmp_path / "projects.txt"
import_file.write_text("https://github.com/user/repo1\n", encoding="utf-8")
mock_pipeline = MagicMock()
mock_pipeline.add_to_db.return_value = False
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(
main,
[
"db",
"import",
"-f",
str(import_file),
"--db",
db_path,
],
)
assert result.exit_code == 0
assert "失败" in result.output
@patch("gh_similarity_detector.cli.db_commands.DetectionPipeline")
def test_db_import_with_exception(self, MockPipeline, tmp_path):
db_path = str(tmp_path / "test.sqlite")
import_file = tmp_path / "projects.txt"
import_file.write_text("https://github.com/user/repo1\n", encoding="utf-8")
def side_effect(project, progress=None):
raise RuntimeError("network error")
mock_pipeline = MagicMock()
mock_pipeline.add_to_db.side_effect = side_effect
MockPipeline.return_value = mock_pipeline
runner = CliRunner()
result = runner.invoke(
main,
[
"db",
"import",
"-f",
str(import_file),
"--db",
db_path,
],
)
assert result.exit_code == 0
assert "异常" in result.output