Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Jan 11, 2025
1 parent c7abed4 commit 47ffc41
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
36 changes: 19 additions & 17 deletions investing_algorithm_framework/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ def on_run(self, app, algorithm: Algorithm):

class App:

def __init__(self, state_handler=None, web=False):
def __init__(self, state_handler=None):
self._flask_app: Optional[Flask] = None
self.container = None
self._web = web
self._algorithm: Optional[Algorithm] = None
self._started = False
self._tasks = []
Expand Down Expand Up @@ -134,13 +133,8 @@ def initialize_config(self):

config = configuration_service.get_config()

if APP_MODE not in config or config[APP_MODE] is None:
if self._web:
configuration_service.add_value(APP_MODE, AppMode.WEB.value)
else:
configuration_service.add_value(
APP_MODE, AppMode.DEFAULT.value
)
if APP_MODE not in config:
configuration_service.add_value(APP_MODE, AppMode.DEFAULT.value)

def initialize(self):
"""
Expand Down Expand Up @@ -198,10 +192,13 @@ def initialize(self):

for strategy in self.algorithm.strategies:

for market_data_source in strategy.market_data_sources:
market_data_source_service.add(market_data_source)
if strategy.market_data_sources is not None:
for market_data_source in strategy.market_data_sources:
market_data_source_service.add(market_data_source)

config = self.container.configuration_service().get_config()

if self._web:
if config[APP_MODE] == AppMode.WEB.value:
self._configuration_service.add_value(
APP_MODE, AppMode.WEB.value
)
Expand Down Expand Up @@ -322,6 +319,15 @@ def _initialize_app_for_backtest(
configuration_service.add_value(
BACKTESTING_END_DATE, backtest_date_range.end_date
)
configuration_service.add_value(
DATABASE_NAME, "backtest-database.sqlite3"
)
configuration_service.add_value(
DATABASE_DIRECTORY_PATH,
os.path.join(
configuration_service.config[RESOURCE_DIRECTORY], "backtest_databases"
)
)

if pending_order_check_interval is not None:
configuration_service.add_value(
Expand All @@ -330,7 +336,7 @@ def _initialize_app_for_backtest(
)

# Create resource dir if not exits
self._create_resource_directory_if_not_exists()
self._create_resources_if_not_exists()

def _create_backtest_database_if_not_exists(self):
"""
Expand Down Expand Up @@ -644,10 +650,6 @@ def add_portfolio_configuration(self, portfolio_configuration):
.portfolio_configuration_service()
portfolio_configuration_service.add(portfolio_configuration)

@property
def web(self):
return self._web

@property
def running(self):
return self.algorithm.running
Expand Down
10 changes: 7 additions & 3 deletions investing_algorithm_framework/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

from .app import App
from .dependency_container import setup_dependency_container
from .domain import APP_MODE, AppMode

logger = logging.getLogger("investing_algorithm_framework")


def create_app(
config: dict = None,
web=False,
state_handler=None
state_handler=None,
web: bool = False
) -> App:
"""
Factory method to create an app instance.
Expand All @@ -26,7 +27,7 @@ def create_app(
# Load the environment variables
load_dotenv()

app = App(web=web, state_handler=state_handler)
app = App(state_handler=state_handler)
app = setup_dependency_container(
app,
["investing_algorithm_framework"],
Expand All @@ -38,5 +39,8 @@ def create_app(
if config is not None:
app.set_config_with_dict(config)

if web:
app.set_config("APP_MODE", AppMode.WEB.value)

logger.info("Investing algoritm framework app created")
return app
2 changes: 2 additions & 0 deletions tests/resources/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def setUp(self) -> None:
for market_credential in self.market_credentials:
self.app.add_market_credential(market_credential)

self.app.initialize_config()

if self.initialize:
self.app.initialize()

Expand Down
13 changes: 6 additions & 7 deletions tests/services/test_portfolio_sync_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from datetime import datetime

from investing_algorithm_framework import PortfolioConfiguration, Algorithm, \
MarketCredential, OperationalException, RESERVED_BALANCES, APP_MODE, \
Order, SYMBOLS, AppMode
MarketCredential, OperationalException, RESERVED_BALANCES
from tests.resources import TestBase


Expand Down Expand Up @@ -36,6 +33,7 @@ def test_sync_unallocated(self):
)
self.market_service.balances = {"EUR": 1000}
self.app.add_algorithm(Algorithm())
self.app.initialize_config()
self.app.initialize()

portfolio = self.app.container.portfolio_service()\
Expand Down Expand Up @@ -76,12 +74,11 @@ def test_sync_unallocated_with_no_balance(self):
self.app.add_algorithm(Algorithm())

with self.assertRaises(OperationalException) as context:
self.app.initialize_config()
self.app.initialize()

self.assertEqual(
"The initial balance of the portfolio configuration 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.",
"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.",
str(context.exception)
)

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

portfolio = self.app.container.portfolio_service() \
Expand All @@ -129,6 +127,7 @@ def test_sync_unallocated_with_initial_size(self):
)
self.market_service.balances = {"EUR": 1200}
self.app.add_algorithm(Algorithm())
self.app.initialize_config()
self.app.initialize()

portfolio = self.app.container.portfolio_service() \
Expand Down

0 comments on commit 47ffc41

Please sign in to comment.