test_crm_product_column_facet.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """CRM 产品列头 facet / productNoIn·productNameIn 单测。"""
  2. from __future__ import annotations
  3. from sqlalchemy.dialects import mysql
  4. from app.services.crm.product_list_filters import build_product_column_filters
  5. def _compile(clause) -> str:
  6. return str(
  7. clause.compile(dialect=mysql.dialect(), compile_kwargs={"literal_binds": True})
  8. ).lower()
  9. def test_product_no_in_adds_in_clause():
  10. filters = build_product_column_filters(product_no_in="A001,B002")
  11. assert len(filters) == 1
  12. sql = _compile(filters[0])
  13. assert "product_no" in sql
  14. assert "in (" in sql
  15. assert "a001" in sql
  16. assert "b002" in sql
  17. def test_product_name_in_adds_in_clause():
  18. filters = build_product_column_filters(product_name_in="甲醇,乙醇")
  19. assert len(filters) == 1
  20. sql = _compile(filters[0])
  21. assert "product_name" in sql
  22. assert "in (" in sql
  23. assert "甲醇" in sql
  24. assert "乙醇" in sql
  25. def test_product_no_in_skips_fuzzy():
  26. filters = build_product_column_filters(product_no_in="A001", product_no="ignored")
  27. assert len(filters) == 1
  28. sql = _compile(filters[0])
  29. assert "in (" in sql
  30. assert "like" not in sql
  31. def test_product_name_in_skips_fuzzy():
  32. filters = build_product_column_filters(product_name_in="甲醇", product_name="ignored")
  33. assert len(filters) == 1
  34. sql = _compile(filters[0])
  35. assert "in (" in sql
  36. assert "like" not in sql
  37. assert "name_pinyin_abbr" not in sql
  38. def test_product_no_fuzzy_still_works():
  39. filters = build_product_column_filters(product_no="A00")
  40. assert len(filters) == 1
  41. sql = _compile(filters[0])
  42. assert "product_no" in sql
  43. assert "like" in sql
  44. def test_facet_field_whitelist():
  45. from app.crud.crm.product import _PRODUCT_COLUMN_FACET_FIELDS
  46. assert set(_PRODUCT_COLUMN_FACET_FIELDS.keys()) == {
  47. "productNo",
  48. "productName",
  49. "categoryName",
  50. "model",
  51. }