Skip to content

Commit 47ffc41

Browse files
committed
Fix failing tests
1 parent c7abed4 commit 47ffc41

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

investing_algorithm_framework/app/app.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ def on_run(self, app, algorithm: Algorithm):
3939

4040
class App:
4141

42-
def __init__(self, state_handler=None, web=False):
42+
def __init__(self, state_handler=None):
4343
self._flask_app: Optional[Flask] = None
4444
self.container = None
45-
self._web = web
4645
self._algorithm: Optional[Algorithm] = None
4746
self._started = False
4847
self._tasks = []
@@ -134,13 +133,8 @@ def initialize_config(self):
134133

135134
config = configuration_service.get_config()
136135

137-
if APP_MODE not in config or config[APP_MODE] is None:
138-
if self._web:
139-
configuration_service.add_value(APP_MODE, AppMode.WEB.value)
140-
else:
141-
configuration_service.add_value(
142-
APP_MODE, AppMode.DEFAULT.value
143-
)
136+
if APP_MODE not in config:
137+
configuration_service.add_value(APP_MODE, AppMode.DEFAULT.value)
144138

145139
def initialize(self):
146140
"""
@@ -198,10 +192,13 @@ def initialize(self):
198192

199193
for strategy in self.algorithm.strategies:
200194

201-
for market_data_source in strategy.market_data_sources:
202-
market_data_source_service.add(market_data_source)
195+
if strategy.market_data_sources is not None:
196+
for market_data_source in strategy.market_data_sources:
197+
market_data_source_service.add(market_data_source)
198+
199+
config = self.container.configuration_service().get_config()
203200

204-
if self._web:
201+
if config[APP_MODE] == AppMode.WEB.value:
205202
self._configuration_service.add_value(
206203
APP_MODE, AppMode.WEB.value
207204
)
@@ -322,6 +319,15 @@ def _initialize_app_for_backtest(
322319
configuration_service.add_value(
323320
BACKTESTING_END_DATE, backtest_date_range.end_date
324321
)
322+
configuration_service.add_value(
323+
DATABASE_NAME, "backtest-database.sqlite3"
324+
)
325+
configuration_service.add_value(
326+
DATABASE_DIRECTORY_PATH,
327+
os.path.join(
328+
configuration_service.config[RESOURCE_DIRECTORY], "backtest_databases"
329+
)
330+
)
325331

326332
if pending_order_check_interval is not None:
327333
configuration_service.add_value(
@@ -330,7 +336,7 @@ def _initialize_app_for_backtest(
330336
)
331337

332338
# Create resource dir if not exits
333-
self._create_resource_directory_if_not_exists()
339+
self._create_resources_if_not_exists()
334340

335341
def _create_backtest_database_if_not_exists(self):
336342
"""
@@ -644,10 +650,6 @@ def add_portfolio_configuration(self, portfolio_configuration):
644650
.portfolio_configuration_service()
645651
portfolio_configuration_service.add(portfolio_configuration)
646652

647-
@property
648-
def web(self):
649-
return self._web
650-
651653
@property
652654
def running(self):
653655
return self.algorithm.running

investing_algorithm_framework/create_app.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
from .app import App
55
from .dependency_container import setup_dependency_container
6+
from .domain import APP_MODE, AppMode
67

78
logger = logging.getLogger("investing_algorithm_framework")
89

910

1011
def create_app(
1112
config: dict = None,
12-
web=False,
13-
state_handler=None
13+
state_handler=None,
14+
web: bool = False
1415
) -> App:
1516
"""
1617
Factory method to create an app instance.
@@ -26,7 +27,7 @@ def create_app(
2627
# Load the environment variables
2728
load_dotenv()
2829

29-
app = App(web=web, state_handler=state_handler)
30+
app = App(state_handler=state_handler)
3031
app = setup_dependency_container(
3132
app,
3233
["investing_algorithm_framework"],
@@ -38,5 +39,8 @@ def create_app(
3839
if config is not None:
3940
app.set_config_with_dict(config)
4041

42+
if web:
43+
app.set_config("APP_MODE", AppMode.WEB.value)
44+
4145
logger.info("Investing algoritm framework app created")
4246
return app

tests/resources/test_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def setUp(self) -> None:
7070
for market_credential in self.market_credentials:
7171
self.app.add_market_credential(market_credential)
7272

73+
self.app.initialize_config()
74+
7375
if self.initialize:
7476
self.app.initialize()
7577

tests/services/test_portfolio_sync_service.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from datetime import datetime
2-
31
from investing_algorithm_framework import PortfolioConfiguration, Algorithm, \
4-
MarketCredential, OperationalException, RESERVED_BALANCES, APP_MODE, \
5-
Order, SYMBOLS, AppMode
2+
MarketCredential, OperationalException, RESERVED_BALANCES
63
from tests.resources import TestBase
74

85

@@ -36,6 +33,7 @@ def test_sync_unallocated(self):
3633
)
3734
self.market_service.balances = {"EUR": 1000}
3835
self.app.add_algorithm(Algorithm())
36+
self.app.initialize_config()
3937
self.app.initialize()
4038

4139
portfolio = self.app.container.portfolio_service()\
@@ -76,12 +74,11 @@ def test_sync_unallocated_with_no_balance(self):
7674
self.app.add_algorithm(Algorithm())
7775

7876
with self.assertRaises(OperationalException) as context:
77+
self.app.initialize_config()
7978
self.app.initialize()
8079

8180
self.assertEqual(
82-
"The initial balance of the portfolio configuration is more than" " the available balance on the exchange. Please make sure"
83-
" that the initial balance of the portfolio configuration"
84-
" is less than the available balance on the exchange.",
81+
"The initial balance of the portfolio configuration (1000.0 EUR) is more than the available balance on the exchange. Please make sure that the initial balance of the portfolio configuration is less than the available balance on the exchange 0.0 EUR.",
8582
str(context.exception)
8683
)
8784

@@ -105,6 +102,7 @@ def test_sync_unallocated_with_reserved(self):
105102
)
106103
self.market_service.balances = {"EUR": 1200}
107104
self.app.add_algorithm(Algorithm())
105+
self.app.initialize_config()
108106
self.app.initialize()
109107

110108
portfolio = self.app.container.portfolio_service() \
@@ -129,6 +127,7 @@ def test_sync_unallocated_with_initial_size(self):
129127
)
130128
self.market_service.balances = {"EUR": 1200}
131129
self.app.add_algorithm(Algorithm())
130+
self.app.initialize_config()
132131
self.app.initialize()
133132

134133
portfolio = self.app.container.portfolio_service() \

0 commit comments

Comments
 (0)