60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
|
|
from app.nqi.adhoc import search_by_field
|
|
|
|
|
|
def test_field_search_groups_hits_by_table(monkeypatch):
|
|
seen = {}
|
|
|
|
async def fake_session(client):
|
|
seen["session"] = True
|
|
|
|
async def fake_post(client, url, form):
|
|
seen["url"] = url
|
|
seen["form"] = form
|
|
return {
|
|
"CFG_ADHOC_CONF_SEARCH": [
|
|
{
|
|
"tablename": "appdbv3.a_common_pm_lte",
|
|
"tablename_cn": "4G小区性能KPI报表",
|
|
"columnname": "uplastttioctdl",
|
|
"columnname_cn": "用户面下行尾包字节数",
|
|
"datatype": "character varying",
|
|
"fieldtype": "公共信息(小区级粒度)",
|
|
},
|
|
{
|
|
"tablename": "appdbv3.a_common_pm_lte",
|
|
"tablename_cn": "4G小区性能KPI报表",
|
|
"columnname": "uplastttioctdl",
|
|
"columnname_cn": "用户面下行尾包字节数",
|
|
"datatype": "character varying",
|
|
},
|
|
{
|
|
"tablename": "appdbv3.a_other",
|
|
"tablename_cn": "另一张表",
|
|
"columnname": "cgi",
|
|
"columnname_cn": "CGI",
|
|
"datatype": "character varying",
|
|
},
|
|
{"tablename": "", "columnname_cn": "丢弃"},
|
|
]
|
|
}
|
|
|
|
monkeypatch.setattr("app.nqi.adhoc.ensure_session", fake_session)
|
|
monkeypatch.setattr("app.nqi.adhoc._post_json", fake_post)
|
|
tables = asyncio.run(search_by_field(object(), " 尾包字节 "))
|
|
assert seen["session"] is True
|
|
assert seen["url"].endswith("/pro-adhoc/adhocquery/search")
|
|
assert seen["form"]["key"] == "尾包字节"
|
|
assert seen["form"]["field"] == ["columnname_cn", "columnname", "fieldtype"]
|
|
assert [item["tablename"] for item in tables] == ["appdbv3.a_common_pm_lte", "appdbv3.a_other"]
|
|
assert tables[0]["fields"] == [{"name": "用户面下行尾包字节数", "column": "uplastttioctdl", "type": "character varying"}]
|
|
assert tables[1]["name"] == "另一张表"
|
|
|
|
|
|
def test_field_search_rejects_a_blank_keyword():
|
|
with pytest.raises(ValueError):
|
|
asyncio.run(search_by_field(object(), " "))
|