diff options
| author | uvok | 2025-04-18 17:44:34 +0200 | 
|---|---|---|
| committer | uvok | 2025-04-18 17:44:34 +0200 | 
| commit | 3346c665e47fcda5ef3216d66174eae815fcd3cd (patch) | |
| tree | 0b6ccd852e70d3b0142b2632639b764fdfabf1c0 | |
| parent | d12498351659dc47063b42df51d1756bcb2bd4fd (diff) | |
Addand use TradeNotFound
| -rw-r--r-- | exceptions.py | 3 | ||||
| -rw-r--r-- | test_ledger_process.py | 10 | ||||
| -rw-r--r-- | test_trade_queue.py | 3 | ||||
| -rw-r--r-- | trade_queue.py | 6 | 
4 files changed, 15 insertions, 7 deletions
| diff --git a/exceptions.py b/exceptions.py index ec210a2..dba1e48 100644 --- a/exceptions.py +++ b/exceptions.py @@ -1,2 +1,5 @@  class MismatchedTradeError(Exception):      pass + +class TradeNotFound(Exception): +    pass diff --git a/test_ledger_process.py b/test_ledger_process.py index fbbae1d..e5399d3 100644 --- a/test_ledger_process.py +++ b/test_ledger_process.py @@ -1,6 +1,6 @@  import unittest  from decimal import Decimal -from exceptions import MismatchedTradeError +from exceptions import MismatchedTradeError, TradeNotFound  from ledger_action import LedgerAction  from ledger_process import LedgerProcess @@ -52,8 +52,8 @@ class TestLedgerProcess(unittest.TestCase):          with self.assertRaises(MismatchedTradeError):              self.lp.process_ledger([crypto_trade])  # EUR row missing -    def test_unsupported_deposit(self): -        """Test handling unsupported deposits.""" +    def test_deposit_notfound(self): +        """Test handling deposit with no matching withdraw."""          deposit = LedgerAction(              type="deposit",              asset="BTC", @@ -62,7 +62,9 @@ class TestLedgerProcess(unittest.TestCase):              timestamp="2025-04-17 10:00:00",              refid="67890",          ) -        self.lp.process_ledger([deposit]) + +        with self.assertRaises(TradeNotFound): +            self.lp.process_ledger([deposit])  if __name__ == "__main__": diff --git a/test_trade_queue.py b/test_trade_queue.py index 6afd599..f43487d 100644 --- a/test_trade_queue.py +++ b/test_trade_queue.py @@ -2,6 +2,7 @@ from decimal import Decimal  import unittest  from datetime import datetime +from exceptions import TradeNotFound  from trade_queue import FIFOQueue @@ -157,7 +158,7 @@ class TestFIFOQueueRemove(unittest.TestCase):          Test trying to remove a trade when no match is found.          """          # No such date -        with self.assertRaises(ValueError) as context: +        with self.assertRaises(TradeNotFound) as context:              self.fifo_queue.remove(lambda t: t.date == "2024-04-17")          self.assertIn("No trade matches the given predicate.", str(context.exception))          # Ensure no trade is removed diff --git a/trade_queue.py b/trade_queue.py index c76017a..5e7fb41 100644 --- a/trade_queue.py +++ b/trade_queue.py @@ -4,6 +4,7 @@ from copy import deepcopy  from decimal import Decimal  from typing import Callable, Deque, List +from exceptions import TradeNotFound  from trade import Trade  # Set up a dedicated logger for FIFOQueue @@ -131,14 +132,15 @@ class FIFOQueue:              Trade: The removed trade.          Raises: -            ValueError: If no trade matches the predicate or multiple trades are found. +            TradeNotFound: If no trade matches the predicate or multiple trades are found. +            ValueError: If multiple trades match the predicate.          """          # Use filter to find matching trades          matching_trades = list(filter(predicate, self.__queue))          if len(matching_trades) == 0:              logger.error("No matching trade found for removal.") -            raise ValueError("No trade matches the given predicate.") +            raise TradeNotFound("No trade matches the given predicate.")          elif len(matching_trades) > 1:              logger.error("Multiple matching trades found for removal.")              raise ValueError( | 
