98 lines
4.0 KiB
Python
98 lines
4.0 KiB
Python
import asyncio
|
|
import json
|
|
from datetime import date, datetime
|
|
|
|
import pytest
|
|
|
|
from app.nqi import adhoc
|
|
from app.nqi.client import NqiError
|
|
|
|
HEAD = {
|
|
"tablename": "appdbv3.v_a_common_cfg_lte_cellant_d",
|
|
"tablename_cn": "4G小区工参",
|
|
"columnname": "cgi",
|
|
"columnname_cn": "CGI",
|
|
"fieldtype": "基础信息",
|
|
"datatype": "character varying",
|
|
"columntype": 1,
|
|
"supporteddimension": None,
|
|
"supportedtimedimension": None,
|
|
}
|
|
|
|
|
|
def test_result_json_carries_table_params_like_the_page():
|
|
payload = json.loads(adhoc._result_json(HEAD, [HEAD]))
|
|
assert list(payload) == ["result", "tableParams", "columnname"]
|
|
assert payload["tableParams"] == {"supporteddimension": None, "supportedtimedimension": ""}
|
|
assert payload["columnname"] == ""
|
|
assert payload["result"][0]["feild"] == "cgi" and payload["result"][0]["table"] == HEAD["tablename"]
|
|
|
|
|
|
def test_where_skips_time_when_table_has_no_time_column():
|
|
gongcan = [{**HEAD, "time_field": "starttime", "city_field": "city"}, {**HEAD, "columnname": "cell_name"}]
|
|
kpi = [{**HEAD, "columnname": "starttime", "time_field": "starttime"}, {**HEAD, "columnname": "cgi"}]
|
|
assert not adhoc._has_time_column(gongcan) and adhoc._has_time_column(kpi)
|
|
start, end = adhoc.time_window("prev_day", 1, date(2026, 11, 11))
|
|
assert [item["feild"] for item in adhoc._where(gongcan[0], start, end, "江门", timed=False)] == ["city"]
|
|
timed = adhoc._where(kpi[0], start, end, "江门")
|
|
assert [(item["feild"], item["symbol"], item["val"]) for item in timed] == [
|
|
("starttime", ">=", "2026-11-10 00:00:00"),
|
|
("starttime", "<", "2026-11-10 23:59:59"),
|
|
("city", "in", "江门"),
|
|
]
|
|
|
|
|
|
def test_count_sends_indexcount_and_rejects_error_payload(monkeypatch):
|
|
sent = []
|
|
replies = [{"count": 37604, "indexcount": "0"}, {"stat": "0", "message": "null"}, {}]
|
|
|
|
async def fake_post(client, url, form):
|
|
sent.append((url.rsplit("/", 1)[-1], form))
|
|
return replies[len(sent) - 1]
|
|
|
|
monkeypatch.setattr(adhoc, "_post_json", fake_post)
|
|
assert asyncio.run(adhoc._count(None, {"where": "[]"})) == (37604, "0")
|
|
assert sent[0][0] == "getTableCount" and sent[0][1] == {"where": "[]", "indexcount": "0"}
|
|
for _ in range(2):
|
|
with pytest.raises(NqiError):
|
|
asyncio.run(adhoc._count(None, {"where": "[]"}))
|
|
|
|
|
|
def test_select_rows_inserts_the_list_before_city_and_batches(monkeypatch):
|
|
monkeypatch.setattr(adhoc, "IN_BATCH", 2)
|
|
table = "appdbv3.a_overview_ispm_lte_cell_w"
|
|
table_columns = [
|
|
{**HEAD, "tablename": table, "columnname": "starttime", "time_field": "starttime", "city_field": "city"},
|
|
{**HEAD, "tablename": table, "columnname": "cgi"},
|
|
{**HEAD, "tablename": table, "columnname": "upoctudl_avg"},
|
|
]
|
|
seen = []
|
|
|
|
async def fake_columns(client, name):
|
|
assert name == table
|
|
return table_columns
|
|
|
|
async def session(client):
|
|
return None
|
|
|
|
async def fetch(client, head, chosen, where):
|
|
seen.append(([column["columnname"] for column in chosen], where))
|
|
return [{"cgi": "ok"}]
|
|
|
|
monkeypatch.setattr(adhoc, "_columns", fake_columns)
|
|
monkeypatch.setattr(adhoc, "ensure_session", session)
|
|
monkeypatch.setattr(adhoc, "_fetch_all", fetch)
|
|
start, end = datetime(2026, 10, 5), datetime(2026, 10, 11, 23, 59, 59)
|
|
rows = asyncio.run(adhoc.select_rows(None, table, start, end, "江门", "cgi", ["460-00-1-1", "460-00-1-1", "bad,cgi", "460-00-1-2", "460-00-1-3"], ("cgi", "upoctudl_avg")))
|
|
assert rows == [{"cgi": "ok"}, {"cgi": "ok"}]
|
|
assert [item[0] for item in seen] == [["cgi", "upoctudl_avg"], ["cgi", "upoctudl_avg"]]
|
|
assert [(item["feild"], item["symbol"], item["val"]) for item in seen[0][1]] == [
|
|
("starttime", ">=", "2026-10-05 00:00:00"),
|
|
("starttime", "<", "2026-10-11 23:59:59"),
|
|
("cgi", "in", "460-00-1-1,460-00-1-2"),
|
|
("city", "in", "江门"),
|
|
]
|
|
assert seen[1][1][-2]["val"] == "460-00-1-3"
|
|
assert asyncio.run(adhoc.select_rows(None, table, start, end, "江门", "cgi", [])) == []
|
|
assert len(seen) == 2
|