Skip to content

Commit

Permalink
Update tests to 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkn committed Jan 12, 2024
1 parent 6e6164e commit ddb0652
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions btlib/btlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class Strategy(ABC):

@abstractmethod
def init(self):
pass
""" Abstract method for initializing resources for the strategy """

@abstractmethod
def next(self, i: int, record: Dict[Hashable, Any]):
pass
""" Abstract method defining the core functionality of the strategy """

def __init__(self):
self.data = pd.DataFrame()
Expand Down
3 changes: 0 additions & 3 deletions tests/test_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,3 @@ def test_backtest_multiple_assets(self):
pd.testing.assert_series_equal(result.returns, returns)
self.assertEqual(result.trades, trades)
self.assertEqual(result.open_positions, open_positions)

if __name__ == '__main__':
unittest.main()
3 changes: 0 additions & 3 deletions tests/test_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,3 @@ def test_update_position_with_valid_values(self):
self.assertAlmostEqual(position.profit_loss, 500.0)
self.assertAlmostEqual(position.change_pct, 50.0)
self.assertAlmostEqual(position.current_value, 1500.0)

if __name__ == '__main__':
unittest.main()
11 changes: 9 additions & 2 deletions tests/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

class CustomStrategy(Strategy):
def init(self):
pass
""" Implementation for initialization """

def next(self, i, record):
pass
""" Implementation for the core strategy logic """

class TestStrategy(unittest.TestCase):

Expand Down Expand Up @@ -42,3 +43,9 @@ def test_open_sets_size_to_cash_divided_by_price_if_size_is_none(self):
strategy.cash = 100
self.assertTrue(strategy.open(10, None))
self.assertEqual(strategy.open_positions[0].position_size, 10)

# The close() method returns False if price is NaN or less than or equal to zero
def test_close_returns_false_if_price_or_size_invalid(self):
strategy = CustomStrategy()
self.assertFalse(strategy.close(nan))
self.assertFalse(strategy.close(0))

0 comments on commit ddb0652

Please sign in to comment.