89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
测试数量计算逻辑
|
|
"""
|
|
|
|
import unittest
|
|
import sys
|
|
import os
|
|
import pandas as pd
|
|
from decimal import Decimal
|
|
|
|
# 添加项目根目录到路径
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from app.core.excel.validators import ProductValidator
|
|
|
|
|
|
class TestQuantityCalculation(unittest.TestCase):
|
|
"""测试数量计算逻辑"""
|
|
|
|
def setUp(self):
|
|
"""设置测试环境"""
|
|
self.validator = ProductValidator()
|
|
|
|
def test_quantity_calculation_from_amount(self):
|
|
"""测试通过单价和金额计算数量"""
|
|
# 测试数量为空,但单价和金额存在的情况
|
|
product = {
|
|
'barcode': '6901028075862',
|
|
'name': '可口可乐',
|
|
'quantity': None,
|
|
'price': 5.0,
|
|
'amount': 60.0,
|
|
'unit': '瓶'
|
|
}
|
|
|
|
# 验证产品
|
|
validated = self.validator.validate_product(product)
|
|
|
|
# 断言:数量应该被计算为金额/单价 = 60/5 = 12
|
|
self.assertAlmostEqual(validated['quantity'], 12.0, places=2)
|
|
|
|
def test_quantity_calculation_with_string_values(self):
|
|
"""测试字符串形式的单价和金额"""
|
|
# 测试数量为空,单价和金额为字符串的情况
|
|
product = {
|
|
'barcode': '6901028075862',
|
|
'name': '可口可乐',
|
|
'quantity': None,
|
|
'price': '5.0',
|
|
'amount': '60.0',
|
|
'unit': '瓶'
|
|
}
|
|
|
|
# 验证产品
|
|
validated = self.validator.validate_product(product)
|
|
|
|
# 断言:数量应该被计算为金额/单价 = 60/5 = 12
|
|
self.assertAlmostEqual(validated['quantity'], 12.0, places=2)
|
|
|
|
def test_quantity_calculation_with_format_issues(self):
|
|
"""测试格式问题的情况"""
|
|
# 测试数量为空,单价和金额有格式问题的情况
|
|
product = {
|
|
'barcode': '6901028075862',
|
|
'name': '可口可乐',
|
|
'quantity': None,
|
|
'price': '5,0', # 使用逗号作为小数点
|
|
'amount': '¥60.0', # 带货币符号
|
|
'unit': '瓶'
|
|
}
|
|
|
|
# 验证产品
|
|
validated = self.validator.validate_product(product)
|
|
|
|
# 断言:数量应该被计算为金额/单价 = 60/5 = 12
|
|
self.assertAlmostEqual(validated['quantity'], 12.0, places=2)
|
|
|
|
def test_specification_parsing(self):
|
|
"""测试规格解析逻辑"""
|
|
# 这部分测试需要导入规格解析器
|
|
# 由于需要引入额外的代码,此处仅作为示例
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |