"""discount.calculate_total 的单元测试。

覆盖：常规金额、优惠顺序、超额券、负数与非法输入、分币精度（ROUND_HALF_UP）。
运行方式：python3 -m unittest test_discount -v
"""

import unittest
from decimal import Decimal

from discount import calculate_total


class RegularAmountTests(unittest.TestCase):
    """常规金额：字符串与数字输入，正常折扣与券。"""

    def test_string_input(self):
        self.assertEqual(calculate_total("100", "0.8", "10"), Decimal("70.00"))

    def test_numeric_input(self):
        self.assertEqual(calculate_total(100, 0.8, 10), Decimal("70.00"))

    def test_mixed_string_and_numeric(self):
        self.assertEqual(calculate_total("199.9", "0.75", 30), Decimal("119.93"))

    def test_zero_discount_rate(self):
        self.assertEqual(calculate_total("88.88", "0", "0"), Decimal("0.00"))

    def test_full_rate_no_coupon(self):
        self.assertEqual(calculate_total("123.45", "1", "0"), Decimal("123.45"))


class DiscountOrderTests(unittest.TestCase):
    """优惠顺序：必须先折扣后减券，而非先减券后折扣。"""

    def test_discount_then_coupon(self):
        # 100 * 0.8 - 20 = 60；若先减券后折扣会得到 (100-20)*0.8 = 64。
        self.assertEqual(calculate_total("100", "0.8", "20"), Decimal("60.00"))

    def test_order_affects_result(self):
        self.assertNotEqual(calculate_total("100", "0.8", "20"), Decimal("64.00"))


class ExcessCouponTests(unittest.TestCase):
    """超额券：折扣后金额不足以抵扣券时，结果最低为 0。"""

    def test_coupon_exceeds_discounted_amount(self):
        self.assertEqual(calculate_total("100", "0.8", "200"), Decimal("0.00"))

    def test_coupon_equals_discounted_amount(self):
        self.assertEqual(calculate_total("50", "0.1", "5"), Decimal("0.00"))

    def test_partial_coupon_positive_result(self):
        self.assertEqual(calculate_total("50", "0.1", "3"), Decimal("2.00"))


class NegativeAndInvalidTests(unittest.TestCase):
    """负数与非法 / 非有限输入。"""

    def test_negative_amount_raises(self):
        with self.assertRaises(ValueError):
            calculate_total("-5", "0.8", "1")

    def test_negative_coupon_raises(self):
        with self.assertRaises(ValueError):
            calculate_total("100", "0.8", "-1")

    def test_negative_discount_rate_raises(self):
        with self.assertRaises(ValueError):
            calculate_total("100", "-0.1", "0")

    def test_discount_rate_above_one_raises(self):
        with self.assertRaises(ValueError):
            calculate_total("100", "1.5", "0")

    def test_non_numeric_string_raises(self):
        with self.assertRaises(ValueError):
            calculate_total("abc", "0.8", "0")

    def test_empty_string_raises(self):
        with self.assertRaises(ValueError):
            calculate_total("", "0.8", "0")

    def test_nan_raises(self):
        with self.assertRaises(ValueError):
            calculate_total(float("nan"), "0.8", "0")

    def test_infinity_raises(self):
        with self.assertRaises(ValueError):
            calculate_total(float("inf"), "0.8", "0")

    def test_none_raises(self):
        with self.assertRaises(ValueError):
            calculate_total(None, "0.8", "0")


class CentPrecisionTests(unittest.TestCase):
    """分币精度：十进制计算并 ROUND_HALF_UP 到两位。"""

    def test_half_up_rounding_1005(self):
        # 浮点会得到 1.0；ROUND_HALF_UP 应为 1.01。
        self.assertEqual(calculate_total("1.005", "1", "0"), Decimal("1.01"))

    def test_half_up_rounding_2675(self):
        # 经典二进制浮点陷阱：2.675 用 float 会向下取整为 2.67。
        self.assertEqual(calculate_total("2.675", "1", "0"), Decimal("2.68"))

    def test_half_up_rounding_0105(self):
        self.assertEqual(calculate_total("0.105", "1", "0"), Decimal("0.11"))

    def test_decimal_avoids_float_error(self):
        # 0.1 - 0.05 用 float 会出现 0.049999...，十进制应为 0.05。
        self.assertEqual(calculate_total("0.1", "1", "0.05"), Decimal("0.05"))

    def test_result_has_two_places(self):
        result = calculate_total("70", "0.8", "6")
        self.assertEqual(str(result), "50.00")

    def test_float_input_uses_decimal_string(self):
        self.assertEqual(calculate_total(1.15, 1, 0), Decimal("1.15"))


if __name__ == "__main__":
    unittest.main(verbosity=2)
