Browse Source

test: tested raises conditions

pull/14/head
QuentinN42 10 months ago
parent
commit
9a7132eee2
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 6
      auto_trading/ptf/in_memory.py
  2. 19
      tests/ptf/test_in_memory.py

6
auto_trading/ptf/in_memory.py

@ -11,9 +11,9 @@ class InMemoryPortfolio(PTF):
_balance: float
_stocks: Dict[str, float]
def __init__(self, change_rate_getter: Callable[[], Dict[str, float]]):
def __init__(self, change_rate_getter: Callable[[], Dict[str, float]], **kwargs):
"""Init the class with a pointer to the bot to retrieve the change rate."""
super().__init__()
super().__init__(**kwargs)
self.change_rate_getter = change_rate_getter
@property
@ -27,7 +27,7 @@ class InMemoryPortfolio(PTF):
def execute_short(self, order: Short) -> None:
"""Sell actions."""
if self._stocks[order.stock] < order.amount:
raise OrderFails("Not enough stock to sell.")
raise OrderFails("Not enough stock.")
if self.change_rate[order.stock] < order.price:
raise OrderFails("You shell it too high.")

19
tests/ptf/test_in_memory.py

@ -2,7 +2,7 @@ import pytest
from pandas import DataFrame # type: ignore
from auto_trading.ptf.in_memory import InMemoryPortfolio
from auto_trading.interfaces import PTFState as State
from auto_trading.errors import PTFException
from auto_trading.orders import Long, Short
@ -26,3 +26,20 @@ def test_execute(
ptf.execute_multiples([order])
assert ptf._stocks == stop_stocks
assert ptf._balance == stop_balance
@pytest.mark.parametrize(
"change_rate, stocks, balance, order, msg",
[
({"AAPL": 10}, {"AAPL": 0}, 0, Short("AAPL", 1, 10), "Not enough stock."),
({"AAPL": 10}, {"AAPL": 10}, 0, Short("AAPL", 1, 15), "You shell it too high."),
({"AAPL": 10}, {"AAPL": 0}, 0, Long("AAPL", 1, 10), "Not enough money."),
({"AAPL": 10}, {"AAPL": 0}, 100, Long("AAPL", 1, 5), "You buy it too low."),
],
)
def test_raises(change_rate, stocks, balance, order, msg):
ptf = InMemoryPortfolio(skip_errors=False, change_rate_getter=lambda: change_rate)
ptf._stocks = stocks
ptf._balance = balance
with pytest.raises(PTFException, match=f"Got and order exception : {msg}"):
ptf.execute_multiples([order])

Loading…
Cancel
Save