Commit 179a08f8 179a08f8e141280b533af96ab18328a2a0c83bbc by 沈秋雨

fix

1 parent 0f853eb5
1 from pathlib import Path
2
3 from openpyxl import load_workbook
4
5 from check_record_titles import combine_rows, fetch_crawler_rows, fetch_record_names, write_excel
6
7
8 class Cursor:
9 def __init__(self, rows):
10 self.rows = rows
11 self.calls = []
12
13 def __enter__(self):
14 return self
15
16 def __exit__(self, exc_type, exc_value, traceback):
17 return False
18
19 def execute(self, sql, params=None):
20 self.calls.append((sql, params))
21
22 def fetchall(self):
23 return self.rows
24
25
26 class Connection:
27 def __init__(self, cursor_rows):
28 self.cursors = [Cursor(rows) for rows in cursor_rows]
29
30 def cursor(self):
31 return self.cursors.pop(0)
32
33
34 def test_fetch_crawler_rows_joins_all_platform_tables():
35 connection = Connection([[(10, "1", 101, "recording-1", "同名")]])
36 cursor = connection.cursors[0]
37
38 rows = fetch_crawler_rows(connection, "yinyan_song_records")
39
40 assert rows == [{
41 "record_id": 10,
42 "platform": "1",
43 "platform_song_id": 101,
44 "recording_id": "recording-1",
45 "title": "同名",
46 }]
47 assert "WHERE ysr.is_archive_push = TRUE" in cursor.calls[0][0]
48
49
50 def test_fetch_crawler_rows_rejects_unknown_table():
51 connection = Connection([])
52
53 try:
54 fetch_crawler_rows(connection, "unsafe_table")
55 except ValueError as error:
56 assert "不支持" in str(error)
57 else:
58 raise AssertionError("应拒绝非白名单表")
59
60
61 def test_fetch_record_names_batches_queries():
62 connection = Connection([
63 [{"id": 1, "record_name": "甲"}, {"id": 2, "record_name": "乙"}],
64 [{"id": 3, "record_name": "丙"}],
65 ])
66
67 names = fetch_record_names(connection, [3, 1, 2, 2], batch_size=2)
68
69 assert names == {1: "甲", 2: "乙", 3: "丙"}
70
71
72 def test_excel_marks_only_exact_non_null_matches_green(tmp_path: Path):
73 crawler_rows = [
74 {"record_id": 1, "platform": "1", "platform_song_id": 101, "recording_id": "r1", "title": "相同"},
75 {"record_id": 2, "platform": "2", "platform_song_id": 102, "recording_id": "r2", "title": " 名称 "},
76 {"record_id": 3, "platform": "4", "platform_song_id": 103, "recording_id": None, "title": None},
77 ]
78 rows = combine_rows(crawler_rows, {1: "相同", 2: "名称", 3: None})
79 output = tmp_path / "check.xlsx"
80
81 write_excel(rows, output)
82
83 worksheet = load_workbook(output).active
84 assert [worksheet.cell(1, column).value for column in range(1, 7)] == [
85 "record_id", "record_name", "title", "platform", "platform_song_id", "recording_id",
86 ]
87 assert worksheet["A2"].fill.fgColor.rgb == "00C6EFCE"
88 assert worksheet["A3"].fill.fill_type is None
89 assert worksheet["A4"].fill.fill_type is None