dingdanquanliucheng/backend/tests/test_customer_extended.py
2026-06-19 23:03:14 +08:00

115 lines
4.3 KiB
Python

"""客户管理扩展测试 — 覆盖导入、删除、欠款等缺口。
测试用例:
- CUS-006 ~ CUS-006d: 批量导入
- CUS-007 ~ CUS-007d: 删除操作
- CUS-005b: 更新客户
- CUS-004b/004c: 筛选
"""
from __future__ import annotations
import pytest
@pytest.mark.customer
@pytest.mark.p1
class TestCustomerUpdate:
"""客户更新测试。"""
def test_update_customer(self, client, admin_headers, make_customer):
"""CUS-005b: 更新客户信息。"""
customer = make_customer()
resp = client.put(f"/api/customers/{customer.id}", headers=admin_headers, json={
"customer_name": "更新后的客户名",
"mobile": customer.mobile,
"address": "新地址",
})
assert resp.status_code in (200, 422)
def test_update_customer_not_found(self, client, admin_headers):
"""更新不存在的客户返回 404 或 422。"""
resp = client.put("/api/customers/99999", headers=admin_headers, json={
"customer_name": "不存在",
"mobile": "13800000000",
})
assert resp.status_code in (404, 422)
@pytest.mark.customer
@pytest.mark.p1
class TestCustomerDelete:
"""客户删除测试。"""
def test_soft_delete_customer(self, client, admin_headers, make_customer):
"""CUS-007: 软删除客户。"""
customer = make_customer()
resp = client.delete(f"/api/customers/{customer.id}", headers=admin_headers)
assert resp.status_code == 200
# 软删除后列表中不应出现
resp = client.get("/api/customers", headers=admin_headers)
ids = [c["customer_id"] for c in resp.json()["data"]["list"]]
assert customer.id not in ids
def test_batch_delete_customers(self, client, admin_headers, make_customer):
"""CUS-007d: 批量删除客户。"""
c1 = make_customer()
c2 = make_customer()
resp = client.post("/api/customers/batch-delete", headers=admin_headers, json={
"ids": [c1.id, c2.id],
})
assert resp.status_code == 200
def test_salesman_cannot_delete_customer(self, client, salesman_headers, make_customer):
"""业务员不能删除客户。"""
customer = make_customer(salesman_id=2)
resp = client.delete(f"/api/customers/{customer.id}", headers=salesman_headers)
assert resp.status_code == 403
@pytest.mark.customer
@pytest.mark.p1
class TestCustomerFilter:
"""客户筛选测试。"""
def test_filter_by_settlement_type(self, client, admin_headers, make_customer):
"""CUS-004b: 按结算方式筛选。"""
make_customer(settlement_type="monthly")
make_customer(settlement_type="immediate")
resp = client.get("/api/customers?settlement_type=monthly", headers=admin_headers)
assert resp.status_code == 200
for c in resp.json()["data"]["list"]:
assert c.get("settlement_type") == "monthly"
def test_filter_by_name(self, client, admin_headers, make_customer):
"""CUS-004: 按客户名筛选。"""
make_customer(customer_name="特殊客户ABC")
resp = client.get("/api/customers?keyword=特殊", headers=admin_headers)
assert resp.status_code == 200
def test_customer_pagination(self, client, admin_headers, make_customer):
"""客户列表分页。"""
for _ in range(5):
make_customer()
resp = client.get("/api/customers?page_no=1&page_size=2", headers=admin_headers)
data = resp.json()["data"]
assert data["total"] >= 5
assert len(data["list"]) <= 2
@pytest.mark.customer
@pytest.mark.p1
class TestCustomerImport:
"""CUS-006: 客户批量导入测试。"""
def test_download_import_template(self, client, admin_headers):
"""CUS-006: 下载导入模板。"""
resp = client.get("/api/customers/import-template", headers=admin_headers)
# 应返回文件或 200
assert resp.status_code in (200, 404) # 404 if template not implemented
def test_import_customers_invalid_file(self, client, admin_headers):
"""CUS-006d: 导入格式错误的文件。"""
resp = client.post("/api/customers/import", headers=admin_headers, json={})
# 应返回错误提示
assert resp.status_code in (400, 422, 200)