"""app.core.utils.string_utils 单元测试""" import pytest from app.core.utils.string_utils import parse_monetary_string, format_barcode class TestParseMonetaryString: """parse_monetary_string 金额/数量字符串解析测试""" # --- 基本类型 --- def test_none_returns_none(self): assert parse_monetary_string(None) is None def test_int_passthrough(self): assert parse_monetary_string(42) == 42.0 def test_float_passthrough(self): assert parse_monetary_string(3.14) == 3.14 def test_zero_int(self): assert parse_monetary_string(0) == 0.0 # --- 正常字符串 --- def test_plain_number(self): assert parse_monetary_string("123.45") == 123.45 def test_integer_string(self): assert parse_monetary_string("100") == 100.0 # --- 货币符号 --- def test_yen_prefix(self): assert parse_monetary_string("¥1234.56") == 1234.56 def test_dollar_prefix(self): assert parse_monetary_string("$99.9") == 99.9 def test_yuan_suffix(self): assert parse_monetary_string("100元") == 100.0 # --- 逗号处理 --- def test_comma_as_decimal_point(self): """逗号当小数点: "1,5" = 1.5""" assert parse_monetary_string("1,5") == 1.5 def test_comma_as_thousands_sep(self): """逗号当千位分隔符: "1,234.56" = 1234.56""" assert parse_monetary_string("1,234.56") == 1234.56 def test_multiple_commas_thousands(self): """多个逗号: "1,234,567" = 1234567""" assert parse_monetary_string("1,234,567") == 1234567.0 # --- 空值/无效值 --- def test_empty_string(self): assert parse_monetary_string("") is None def test_whitespace_only(self): assert parse_monetary_string(" ") is None def test_o_string(self): """OCR 常见误识别: 字母 o 当数字 0""" assert parse_monetary_string("o") is None def test_none_string(self): assert parse_monetary_string("none") is None def test_null_string(self): assert parse_monetary_string("null") is None def test_dash(self): assert parse_monetary_string("-") is None def test_double_dash(self): assert parse_monetary_string("--") is None def test_no_digits(self): assert parse_monetary_string("赠品") is None # --- 负数 --- def test_negative_number(self): assert parse_monetary_string("-5.5") == -5.5 # --- 非字符串非数字类型 --- def test_list_returns_none(self): assert parse_monetary_string([1, 2]) is None def test_dict_returns_none(self): assert parse_monetary_string({"a": 1}) is None class TestFormatBarcode: """format_barcode 条码格式化测试""" def test_none_returns_empty(self): assert format_barcode(None) == "" def test_normal_digit_string(self): assert format_barcode("6920584471055") == "6920584471055" def test_integer_input(self): assert format_barcode(6920584471055) == "6920584471055" def test_float_with_zero_decimal(self): assert format_barcode(6920584471055.0) == "6920584471055" def test_scientific_notation(self): assert format_barcode("6.920584e+12") == "6920584000000" def test_trailing_zeros_stripped(self): assert format_barcode("123456.0") == "123456" def test_long_barcode_with_trailing_zeros(self): """14位条码末尾是0时应截断到13位""" assert format_barcode("69205844710550") == "6920584471055" def test_long_barcode_without_trailing_zeros(self): """14位条码末尾不是0时不截断""" assert format_barcode("69205844710551") == "69205844710551" def test_non_digit_chars_removed(self): assert format_barcode("692-058-4471055") == "6920584471055" def test_empty_string(self): assert format_barcode("") == ""