logistics/tests/test_logistics_service.py

177 lines
6.5 KiB
Python

"""物流费用计算服务测试文件"""
import pytest
from logistics_service import LogisticsService
from logisticsClass.logisticsBaseClass import PortType
class TestLogisticsService:
"""物流服务测试类"""
def test_detect_country_uk(self):
"""测试英国邮编识别"""
assert LogisticsService._detect_country("PA2 9BF") == "UK"
assert LogisticsService._detect_country("SW1A 1AA") == "UK"
assert LogisticsService._detect_country("BT1 1AA") == "UK"
def test_detect_country_us(self):
"""测试美国邮编识别"""
assert LogisticsService._detect_country("10001") == "US"
assert LogisticsService._detect_country("10001-1234") == "US"
assert LogisticsService._detect_country("90210") == "US"
def test_detect_country_au(self):
"""测试澳洲邮编识别"""
assert LogisticsService._detect_country("2000") == "AU"
assert LogisticsService._detect_country("3000") == "AU"
def test_parse_packages(self):
"""测试包裹解析"""
packages_data = [
{"name": "包裹1", "length": 63, "width": 59, "height": 48, "weight": 8000},
{"name": "包裹2", "length": 50, "width": 40, "height": 30, "weight": 5000},
]
packages = LogisticsService._parse_packages(packages_data)
assert len(packages.packages) == 2
assert packages.packages[0].fst_size == 63
assert packages.packages[1].fst_size == 50
def test_parse_packages_default_name(self):
"""测试包裹解析-默认名称"""
packages_data = [
{"length": 63, "width": 59, "height": 48, "weight": 8000},
]
packages = LogisticsService._parse_packages(packages_data)
assert len(packages.packages) == 1
def test_calculate_uk(self):
"""测试英国物流费用计算"""
packages = [{"length": 63, "width": 59, "height": 48, "weight": 8000}]
result = LogisticsService.calculate_uk("PA2 9BF", packages)
assert result["country"] == "UK"
assert result["postcode"] == "PA2 9BF"
assert result["optimal_channel"] is not None
assert result["optimal_fee"] is not None
assert result["currency"] == "GBP"
assert len(result["all_channels"]) > 0
def test_calculate_uk_london(self):
"""测试英国伦敦邮编"""
packages = [{"length": 30, "width": 20, "height": 10, "weight": 2000}]
result = LogisticsService.calculate_uk("SW1A 1AA", packages)
assert result["country"] == "UK"
assert result["optimal_channel"] is not None
def test_calculate_us(self):
"""测试美国物流费用计算"""
packages = [{"length": 63, "width": 59, "height": 48, "weight": 8000}]
result = LogisticsService.calculate_us("10001", packages)
assert result["country"] == "US"
assert result["postcode"] == "10001"
assert result["optimal_channel"] is not None
assert result["optimal_fee"] is not None
assert result["currency"] == "USD"
def test_calculate_us_west(self):
"""测试美国美西邮编"""
packages = [{"length": 50, "width": 40, "height": 30, "weight": 5000}]
result = LogisticsService.calculate_us("90001", packages)
assert result["country"] == "US"
assert result["optimal_channel"] is not None
def test_calculate_auto_detect(self):
"""测试自动识别国家"""
packages = [{"length": 63, "width": 59, "height": 48, "weight": 8000}]
# 英国
result_uk = LogisticsService.calculate("PA2 9BF", packages)
assert result_uk["country"] == "UK"
# 美国
result_us = LogisticsService.calculate("10001", packages)
assert result_us["country"] == "US"
def test_calculate_multiple_packages(self):
"""测试多包裹计算"""
packages = [
{"length": 63, "width": 59, "height": 48, "weight": 8000},
{"length": 50, "width": 40, "height": 30, "weight": 5000},
{"length": 40, "width": 30, "height": 20, "weight": 3000},
]
result = LogisticsService.calculate_uk("PA2 9BF", packages)
assert result["package_count"] == 3
assert result["total_weight"] == 16.0 # kg
def test_get_company_detail(self):
"""测试获取指定物流公司详情"""
packages = [{"length": 63, "width": 59, "height": 48, "weight": 8000}]
detail = LogisticsService.get_company_detail("PA2 9BF", packages, "智谷-DPD")
assert detail["company"] == "智谷-DPD"
assert detail["currency"] == "GBP"
assert "detail" in detail
assert "total" in detail
def test_invalid_postcode(self):
"""测试无效邮编"""
packages = [{"length": 63, "width": 59, "height": 48, "weight": 8000}]
with pytest.raises(ValueError):
LogisticsService.calculate("invalid", packages)
def test_result_structure(self):
"""测试返回结果结构"""
packages = [{"length": 63, "width": 59, "height": 48, "weight": 8000}]
result = LogisticsService.calculate_uk("PA2 9BF", packages)
# 验证必需字段
required_fields = [
"country", "postcode", "optimal_channel", "optimal_fee",
"currency", "all_channels", "package_count", "total_weight"
]
for field in required_fields:
assert field in result, f"缺少字段: {field}"
# 验证渠道详情结构
for company, info in result["all_channels"].items():
assert "fee" in info
assert "currency" in info
assert "type" in info
assert "available" in info
class TestPackageCalculation:
"""包裹计算测试类"""
def test_small_package(self):
"""测试小包裹"""
packages = [{"length": 20, "width": 15, "height": 10, "weight": 500}]
result = LogisticsService.calculate_uk("SW1A 1AA", packages)
assert result["optimal_fee"] is not None
def test_large_package(self):
"""测试大包裹"""
packages = [{"length": 200, "width": 100, "height": 80, "weight": 50000}]
result = LogisticsService.calculate_uk("PA2 9BF", packages)
# 大包裹可能被某些渠道拒绝
assert result is not None
def test_heavy_package(self):
"""测试重包裹"""
packages = [{"length": 50, "width": 40, "height": 30, "weight": 80000}]
result = LogisticsService.calculate_us("10001", packages)
assert result is not None
if __name__ == "__main__":
pytest.main([__file__, "-v"])