diff options
-rw-r--r-- | test_trade.py | 2 | ||||
-rw-r--r-- | trade.py | 17 |
2 files changed, 14 insertions, 5 deletions
diff --git a/test_trade.py b/test_trade.py index 8cbf0f4..4fc9c01 100644 --- a/test_trade.py +++ b/test_trade.py @@ -9,7 +9,7 @@ class TestTrade(unittest.TestCase): """ Set up a Trade instance for testing. """ - self.trade = Trade(amount=Decimal(10.0), total_cost=Decimal(100.0), date="2025-04-14", refid="abcd1234") + self.trade = Trade(amount=Decimal(10.0), total_cost=Decimal(100.0), timestamp="2025-04-14 00:00:00", refid="abcd1234") def test_initialization(self): """ @@ -1,3 +1,4 @@ +from datetime import datetime from decimal import Decimal from enum import Enum @@ -14,7 +15,7 @@ class Trade: """ def __init__( - self, amount: Decimal, total_cost: Decimal, date: str, refid: str | None = None + self, amount: Decimal, total_cost: Decimal, timestamp: str, refid: str | None = None ) -> None: """ Initialize a new Trade instance. @@ -22,7 +23,7 @@ class Trade: Args: amount (Decimal): The amount of cryptocurrency traded. total_cost (Decimal): The total cost of the trade. - date (str): The date of the trade, formatted as a string. + timestamp (str): The date or timestamp of the trade, formatted as a string. """ if amount < 0 and total_cost < 0: @@ -32,9 +33,17 @@ class Trade: else: raise ValueError("Amount and tota> cost must be same sign") + # force timestamp + try: + datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") + except ValueError: + datetime.strptime(timestamp, "%Y-%m-%d") + timestamp = timestamp + " 00:00:00" + + self.__amount: Decimal = Decimal(amount) self.__total_cost: Decimal = Decimal(total_cost) - self.__date: str = date + self.__timestamp: str = timestamp self.__refid: str | None = refid def remove_coins( @@ -94,7 +103,7 @@ class Trade: Returns: str: The trade date as a string. """ - return self.__date + return self.__timestamp.split(" ")[0] @property def price_per_coin(self) -> Decimal: |