import asyncio import json from datetime import date 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": "[]"}))