104 lines
4.7 KiB
Python
104 lines
4.7 KiB
Python
"""
|
||
tests/test_order_delivery_query.py
|
||
|
||
用途:订单交期回复列表合并关键词搜索(keyword OR 语义)的单测。
|
||
依赖:conftest 的 app 夹具(testing + sqlite create_all)、query_service.list_lines。
|
||
逻辑:keyword 命中订单单号/客户编号/产品编号/品名等任一字段即返回该行;
|
||
订单维度命中时该订单全部项次返回;日期与状态筛选行为不回归。
|
||
"""
|
||
|
||
from datetime import date
|
||
from decimal import Decimal
|
||
|
||
from app.extensions import db
|
||
from app.models.order_delivery_reply import OrderDeliveryErpLine
|
||
from app.services.order_delivery_reply.query_service import list_lines
|
||
|
||
|
||
def _line(order_no, line_no, *, cust_no="C001", part_no="P-1", part_name="标准物料", cust_part="CP-1"):
|
||
"""构造一行最小快照(字段名/值供 keyword 断言用)。"""
|
||
row = OrderDeliveryErpLine(
|
||
order_no=order_no,
|
||
line_no=line_no,
|
||
order_date=date(2024, 6, 1),
|
||
customer_no=cust_no,
|
||
customer_short_name="恒远电子",
|
||
part_no=part_no,
|
||
part_name=part_name,
|
||
customer_part_no=cust_part,
|
||
qty=Decimal("100"),
|
||
delivered_qty=Decimal("0"),
|
||
erp_missing=False,
|
||
)
|
||
db.session.add(row)
|
||
return row
|
||
|
||
|
||
def test_keyword_matches_any_field_or_semantics(app):
|
||
"""keyword 命中订单单号/客户编号/品名任一字段即返回(OR)。"""
|
||
_line("8301-A", 1, part_name="驱动齿轮")
|
||
_line("8301-B", 1, cust_no="C999")
|
||
_line("8301-C", 1, part_no="P-XYZ")
|
||
db.session.commit()
|
||
|
||
# 断言:品名命中 → 只返回 8301-A
|
||
assert [i["order_no"] for i in list_lines({"keyword": "齿轮"})["items"]] == ["8301-A"]
|
||
# 断言:客户编号命中 → 只返回 8301-B
|
||
assert [i["order_no"] for i in list_lines({"keyword": "C999"})["items"]] == ["8301-B"]
|
||
# 断言:产品编号命中 → 只返回 8301-C(大小写不敏感)
|
||
assert [i["order_no"] for i in list_lines({"keyword": "p-xyz"})["items"]] == ["8301-C"]
|
||
# 断言:订单单号片段命中 → 8301-A
|
||
assert [i["order_no"] for i in list_lines({"keyword": "8301-A"})["items"]] == ["8301-A"]
|
||
# 断言:无命中 → 空
|
||
assert list_lines({"keyword": "不存在"})["items"] == []
|
||
# 断言:空关键词 = 全部
|
||
assert list_lines({})["total"] == 3
|
||
|
||
|
||
def test_keyword_and_date_status_filters_stack(app):
|
||
"""keyword 与 order_date/status 叠加时仍为 AND 语义。"""
|
||
_line("8301-D", 1, part_name="齿轮")
|
||
_line("8301-E", 1, part_name="齿轮")
|
||
db.session.commit()
|
||
|
||
from app.models.order_delivery_reply import OrderDeliveryBatch
|
||
|
||
db.session.add(OrderDeliveryBatch(line_id=2, seq=1, planned_date=date(2024, 7, 1), qty=1))
|
||
db.session.commit()
|
||
|
||
# 断言:关键词 + 日期叠加 → 只留同时满足的行
|
||
result = list_lines({"keyword": "齿轮", "order_date": "2024-06-01"})
|
||
assert result["total"] == 2
|
||
result = list_lines({"keyword": "齿轮", "order_date": "2024-06-02"})
|
||
assert result["total"] == 0
|
||
# 断言:status=not_fully_replied(未锁定)两行都算;有批次的行仍是未锁定(部分回复)
|
||
assert list_lines({"keyword": "齿轮", "status": "not_fully_replied"})["total"] == 2
|
||
|
||
|
||
def test_keyword_matches_salesperson_and_reply_person(app):
|
||
"""keyword 命中业务员(姓名/工号)与交期回复人员(姓名/工号)即返回(2026-09-23 增补)。"""
|
||
from app.models.order_delivery_reply import OrderDeliveryLineState
|
||
|
||
salesperson_line = _line("8301-P1", 1, cust_no="C-A")
|
||
salesperson_line.salesperson_name = "王瑞雪"
|
||
salesperson_line.salesperson_code = "26070045"
|
||
replied_line = _line("8301-P2", 2, cust_no="C-B")
|
||
replied_line.salesperson_name = "张小奇"
|
||
db.session.flush()
|
||
# 交期回复人员快照存于行状态表(notified_user_name / notified_employee_id)
|
||
db.session.add(OrderDeliveryLineState(
|
||
line_id=replied_line.id, notified_user_name="李生", notified_employee_id="10002",
|
||
))
|
||
db.session.commit()
|
||
|
||
# 断言:业务员姓名命中 → 只返回 8301-P1
|
||
assert [i["order_no"] for i in list_lines({"keyword": "王瑞雪"})["items"]] == ["8301-P1"]
|
||
# 断言:业务员工号命中 → 只返回 8301-P1
|
||
assert [i["order_no"] for i in list_lines({"keyword": "26070045"})["items"]] == ["8301-P1"]
|
||
# 断言:交期回复人员姓名命中(状态表 EXISTS 子查询)→ 只返回 8301-P2
|
||
assert [i["order_no"] for i in list_lines({"keyword": "李生"})["items"]] == ["8301-P2"]
|
||
# 断言:交期回复人员工号命中 → 只返回 8301-P2
|
||
assert [i["order_no"] for i in list_lines({"keyword": "10002"})["items"]] == ["8301-P2"]
|
||
# 断言:未命中任何人员 → 空
|
||
assert list_lines({"keyword": "不存在的名字"})["items"] == []
|