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.assertEqual(self.trade.price_per_coin, 10.0) def test_remove_coins_valid(self): """ Test removing a valid amount of coins. """ coin_price_before = self.trade.price_per_coin self.trade.remove_coins(5.0) coin_price_after = self.trade.price_per_coin self.assertEqual(coin_price_before, coin_price_after) self.assertEqual(self.trade.amount, 5.0) self.assertEqual(self.trade.total_cost, 50.0) self.assertEqual(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.00, 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()