summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruvok2025-04-14 20:40:23 +0200
committeruvok2025-04-14 20:40:23 +0200
commitbc3400ee34ac945f708e483980348ba95b5a8c9a (patch)
treedfb45362590ce3363f2d5ea71cfe5b89c7bdbb95
parent8ca506bcf6475e0d0514089d02bb6855f848a908 (diff)
Add trade class tests
-rw-r--r--test_trade.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/test_trade.py b/test_trade.py
new file mode 100644
index 0000000..691ca1a
--- /dev/null
+++ b/test_trade.py
@@ -0,0 +1,55 @@
+import unittest
+from decimal import Decimal
+
+from trade import Trade
+
+class TestTrade(unittest.TestCase):
+ def setUp(self) -> None:
+ """
+ Set up a Trade instance for testing.
+ """
+ self.trade = Trade(amount=10.0, total_cost=100.0, date="2025-04-14")
+
+ def test_initialization(self):
+ """
+ Test that the Trade instance initializes correctly.
+ """
+ self.assertEqual(self.trade.amount, 10.0)
+ self.assertEqual(self.trade.total_cost, 100.0)
+ self.assertEqual(self.trade.date, "2025-04-14")
+ self.assertAlmostEqual(self.trade.price_per_coin, 10.0)
+
+ def test_remove_coins_valid(self):
+ """
+ Test removing a valid amount of coins.
+ """
+ self.trade.remove_coins(5.0)
+ self.assertEqual(self.trade.amount, 5.0)
+ self.assertAlmostEqual(self.trade.total_cost, 50.0)
+ self.assertAlmostEqual(self.trade.price_per_coin, 10.0)
+
+ def test_remove_coins_exceeds_amount(self):
+ """
+ Test removing more coins than available.
+ """
+ with self.assertRaises(ValueError):
+ self.trade.remove_coins(15.0)
+
+ def test_price_per_coin_division_by_zero(self):
+ """
+ Test the price_per_coin property when the amount is zero.
+ """
+ self.trade.remove_coins(10.0) # Reduce amount to zero
+ with self.assertRaises(ZeroDivisionError):
+ _ = self.trade.price_per_coin
+
+ def test_repr(self):
+ """
+ Test the __repr__ method for correct string representation.
+ """
+ expected_repr = "Trade(amount=10.0, price_per_coin=10.00, total_cost=100.00, date=2025-04-14)"
+ self.assertEqual(repr(self.trade), expected_repr)
+
+
+if __name__ == "__main__":
+ unittest.main()