Coverage for hledger_lots/checks.py: 95%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-05-04 22:41 -0300

1from typing import List 

2 

3from .lib import AdjustedTxn 

4 

5 

6class MultipleBaseCurrencies(Exception): 

7 def __init__(self, currencies: set) -> None: 

8 self.currencies = currencies 

9 self.message = f"More than one base currency: {currencies}" 

10 

11 

12def check_short_sell_past(previous_buys: List[AdjustedTxn], sell: AdjustedTxn): 

13 previous_buys_qtty = sum([txn.qtty for txn in previous_buys]) 

14 if abs(sell.qtty) > abs(previous_buys_qtty): 

15 raise ValueError(f"Short sell not allowed for sell {sell}") 

16 

17 

18def check_short_sell_current(previous_buys: List[AdjustedTxn], sell_qtty: float): 

19 previous_buys_qtty = sum([txn.qtty for txn in previous_buys]) 

20 if sell_qtty > previous_buys_qtty: 

21 raise ValueError( 

22 f"Short sell not allowed for sell. You have {previous_buys_qtty:.4f}, which is less than you want to sell: {sell_qtty:.4f}" 

23 ) 

24 

25 

26def check_base_currency(txns: List[AdjustedTxn]): 

27 base_currencies = set(txn.base_cur for txn in txns) 

28 if len(base_currencies) > 1: 

29 raise MultipleBaseCurrencies(base_currencies) 

30 

31 

32def check_available(txns: List[AdjustedTxn], account: str, sell_qtty: float): 

33 sum_qtty = sum(txn.qtty for txn in txns if txn.acct == account) 

34 if sell_qtty > sum_qtty: 

35 raise ValueError( 

36 f"You can't sell {sell_qtty:,.2f} in {account} because there is only {sum_qtty:,.2f} available" 

37 )