From 3df59767ead52728f923a4f1695d13b0741c0079 Mon Sep 17 00:00:00 2001 From: Paul de Raaij Date: Sun, 26 Jul 2026 08:08:43 -0400 Subject: [PATCH] feat: add addition and subtraction to Money and Price --- domain/marketplace/money.py | 6 ++++++ tests/test_money.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/domain/marketplace/money.py b/domain/marketplace/money.py index fdf17e1..f269633 100644 --- a/domain/marketplace/money.py +++ b/domain/marketplace/money.py @@ -5,3 +5,9 @@ from decimal import Decimal @dataclass(frozen=True) class Money: amount: Decimal + + def __add__(self, other): + return Money(self.amount + other.amount) + + def __sub__(self, other): + return Money(self.amount - other.amount) diff --git a/tests/test_money.py b/tests/test_money.py index 601e2d6..ed51dc0 100644 --- a/tests/test_money.py +++ b/tests/test_money.py @@ -22,3 +22,18 @@ def test_money_equality(): money1 = Money(amount=Decimal("50.00")) money2 = Money(amount=Decimal("50.00")) assert money1 == money2 + + +def test_money_add(): + money1 = Money(amount=Decimal("2.00")) + money2 = Money(amount=Decimal("3.00")) + + assert (money1 + money2) == Money(amount=Decimal("5.00")) + assert money1 == Money(amount=Decimal("2.00")) + assert money2 == Money(amount=Decimal("3.00")) + + +def test_money_substract(): + money1 = Money(amount=Decimal("5.00")) + money2 = Money(amount=Decimal("3.00")) + assert (money1 - money2) == Money(amount=Decimal("2.00"))