feat: 完成登录与栅格下载桌面端
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import csv
|
||||
import hashlib
|
||||
import io
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import Workbook
|
||||
|
||||
from app.features.download.fields import columns_for
|
||||
from app.features.download.ids import parse_table, parse_upload
|
||||
from app.features.download.journal import CsvJournal
|
||||
from app.platform.constants import APP_SECRET
|
||||
from app.features.download.rows import build_rows
|
||||
from app.platform.sign import build_sign, wfs_sig
|
||||
from app.platform.client import month_end, rsa_encrypt
|
||||
|
||||
PUBLIC_KEY = (
|
||||
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwjDm1HXDw8QH5ZtGMQIl2h/I8E+"
|
||||
"chOQA8aQ8xCR/+aHnROaN/ZU5Vmd2Zz7g6cAacR9BSm60+iSCYtvEGJKl0WqvbPGJkc8tedjNF1QqgWqkkuE6Udgw2OkEKJCxDg6PrAniR7Cc0io9G8bW4P8JDJjSbbafvMPDDFbVVUWJxxwIDAQAB"
|
||||
)
|
||||
|
||||
|
||||
def test_wfs_signature_matches_capture():
|
||||
assert wfs_sig("HRwhGIb3") == "b1c5be69ecdd1247d669ddaff4224050"
|
||||
assert wfs_sig("HRxs2eTA") == "ee021c49a89f7560c54a36835ec23d3a"
|
||||
|
||||
|
||||
def test_request_sign_is_sorted_md5():
|
||||
signed = build_sign({"cityCode": "JM", "dataType": "GRID"}, timestamp="1", nonce="abc")
|
||||
raw = "cityCodeJMdataTypeGRIDnonceStrabctimestamp1version1.0.0"
|
||||
expected = hashlib.md5((raw + APP_SECRET).encode()).hexdigest().upper()
|
||||
assert signed["sign"] == expected
|
||||
assert signed["appKey"]
|
||||
|
||||
|
||||
def test_month_end():
|
||||
assert month_end("2026-09") == "20260930"
|
||||
assert month_end("2026-02") == "20260228"
|
||||
|
||||
|
||||
def test_rsa_block_size():
|
||||
encrypted = rsa_encrypt(PUBLIC_KEY, "demo")
|
||||
assert len(encrypted) == 172
|
||||
|
||||
|
||||
def test_grid_rows_repeat_metrics_per_cell():
|
||||
columns = columns_for("NR", None)
|
||||
rows = build_rows(
|
||||
"541927061",
|
||||
{"grid20_id": "541927061", "city_name": "江门", "ssb_rsrp_yd": -83.38, "mro_total_yd": 3080},
|
||||
[
|
||||
{"cgi": "126160-1", "mro_total": 2067, "ssb_rsrp": -80.37, "rsrp_lt_neg105": 7},
|
||||
{"cgi": "126160-2", "mro_total": 886, "ssb_rsrp": -91.11, "rsrp_lt_neg105": 13},
|
||||
],
|
||||
columns,
|
||||
)
|
||||
labels = [column.label for column in columns]
|
||||
assert len(rows) == 2
|
||||
assert rows[0][labels.index("20米栅格ID")] == "541927061"
|
||||
assert rows[1][labels.index("20米栅格ID")] == "541927061"
|
||||
assert rows[0][labels.index("小区ID")] == "126160-1"
|
||||
assert rows[1][labels.index("小区ID")] == "126160-2"
|
||||
assert rows[0][labels.index("中国移动SSB-RSRP(dBm)")] == "-83.38"
|
||||
assert "中国移动SSB-RSRP(dBm)" in labels
|
||||
assert "RSRP(dBm)" in labels
|
||||
|
||||
|
||||
def test_missing_cells_keep_one_blank_row():
|
||||
columns = columns_for("LTE", ["samples"])
|
||||
rows = build_rows("10001", {"city_name": "江门", "mro_total_yd": 3}, [], columns)
|
||||
labels = [column.label for column in columns]
|
||||
assert len(rows) == 1
|
||||
assert rows[0][labels.index("20米栅格ID")] == "10001"
|
||||
assert rows[0][labels.index("小区ID")] == ""
|
||||
assert rows[0][labels.index("中国移动MR总数")] == "3"
|
||||
assert "中国移动RSRP(dBm)" not in labels
|
||||
|
||||
|
||||
def test_missing_grid_still_writes_requested_id():
|
||||
columns = columns_for("NR", [])
|
||||
rows = build_rows("42", None, None, columns)
|
||||
labels = [column.label for column in columns]
|
||||
assert len(rows) == 1
|
||||
assert rows[0][labels.index("20米栅格ID")] == "42"
|
||||
assert rows[0][labels.index("小区ID")] == ""
|
||||
|
||||
|
||||
def test_parse_ids_from_header_and_duplicates():
|
||||
rows = parse_table([["20米栅格ID", "备注"], ["541927061", "a"], ["541927061", "b"], ["12", "c"], ["", ""]])
|
||||
assert rows == ["541927061", "12"]
|
||||
|
||||
|
||||
def test_parse_plain_lines_and_xlsx(tmp_path: Path):
|
||||
text = tmp_path / "ids.txt"
|
||||
text.write_text("541927061\n544536471\n", encoding="utf-8")
|
||||
assert parse_upload(text) == ["541927061", "544536471"]
|
||||
book = Workbook()
|
||||
sheet = book.active
|
||||
sheet.append(["栅格ID"])
|
||||
sheet.append([544536471])
|
||||
path = tmp_path / "ids.xlsx"
|
||||
book.save(path)
|
||||
book.close()
|
||||
assert parse_upload(path) == ["544536471"]
|
||||
|
||||
|
||||
def test_journal_discards_uncommitted_tail(tmp_path: Path):
|
||||
csv_path = tmp_path / "out.csv"
|
||||
progress = tmp_path / "progress.jsonl"
|
||||
journal = CsvJournal(csv_path, progress, ["栅格", "小区"])
|
||||
journal.open()
|
||||
assert journal.write_rows("1", [["1", "a"], ["1", "b"]]) == 2
|
||||
journal.close()
|
||||
with csv_path.open("ab") as handle:
|
||||
handle.write("垃圾,数据\r\n".encode("utf-8"))
|
||||
journal.open()
|
||||
assert "1" in journal.completed
|
||||
assert "垃圾" not in csv_path.read_text(encoding="utf-8")
|
||||
assert journal.write_rows("1", [["1", "again"]]) == 0
|
||||
assert journal.write_rows("2", [["2", ""]]) == 1
|
||||
journal.close()
|
||||
body = list(csv.reader(io.StringIO(csv_path.read_text(encoding="utf-8-sig"))))
|
||||
assert body == [["栅格", "小区"], ["1", "a"], ["1", "b"], ["2", ""]]
|
||||
|
||||
|
||||
def test_journal_writes_do_not_interleave(tmp_path: Path):
|
||||
csv_path = tmp_path / "out.csv"
|
||||
journal = CsvJournal(csv_path, tmp_path / "progress.jsonl", ["组", "序号", "内容"])
|
||||
journal.open()
|
||||
|
||||
def write_group(name: str) -> None:
|
||||
for index in range(20):
|
||||
journal.write_rows(f"{name}-{index}", [[name, str(index), "甲" * 30]])
|
||||
|
||||
threads = [threading.Thread(target=write_group, args=(name,)) for name in ("甲", "乙")]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
journal.close()
|
||||
rows = list(csv.reader(io.StringIO(csv_path.read_text(encoding="utf-8-sig"))))
|
||||
assert len(rows) == 41
|
||||
assert all(len(row) == 3 for row in rows)
|
||||
assert {row[0] for row in rows[1:]} == {"甲", "乙"}
|
||||
Reference in New Issue
Block a user