"""CRM 产品列头 facet / productNoIn·productNameIn 单测。""" from __future__ import annotations from sqlalchemy.dialects import mysql from app.services.crm.product_list_filters import build_product_column_filters def _compile(clause) -> str: return str( clause.compile(dialect=mysql.dialect(), compile_kwargs={"literal_binds": True}) ).lower() def test_product_no_in_adds_in_clause(): filters = build_product_column_filters(product_no_in="A001,B002") assert len(filters) == 1 sql = _compile(filters[0]) assert "product_no" in sql assert "in (" in sql assert "a001" in sql assert "b002" in sql def test_product_name_in_adds_in_clause(): filters = build_product_column_filters(product_name_in="甲醇,乙醇") assert len(filters) == 1 sql = _compile(filters[0]) assert "product_name" in sql assert "in (" in sql assert "甲醇" in sql assert "乙醇" in sql def test_product_no_in_skips_fuzzy(): filters = build_product_column_filters(product_no_in="A001", product_no="ignored") assert len(filters) == 1 sql = _compile(filters[0]) assert "in (" in sql assert "like" not in sql def test_product_name_in_skips_fuzzy(): filters = build_product_column_filters(product_name_in="甲醇", product_name="ignored") assert len(filters) == 1 sql = _compile(filters[0]) assert "in (" in sql assert "like" not in sql assert "name_pinyin_abbr" not in sql def test_product_no_fuzzy_still_works(): filters = build_product_column_filters(product_no="A00") assert len(filters) == 1 sql = _compile(filters[0]) assert "product_no" in sql assert "like" in sql def test_facet_field_whitelist(): from app.crud.crm.product import _PRODUCT_COLUMN_FACET_FIELDS assert set(_PRODUCT_COLUMN_FACET_FIELDS.keys()) == { "productNo", "productName", "categoryName", "model", }