Skip to content

Commit d7bb2cf

Browse files
committed
Update tests
1 parent 90f9e1f commit d7bb2cf

File tree

12 files changed

+294
-18
lines changed

12 files changed

+294
-18
lines changed

investing_algorithm_framework/configuration/setup/default_template_creators.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ def create(self) -> None:
9393
if filename in [
9494
'manage.py-template',
9595
'settings.py-template',
96-
'context.py-template'
96+
'context.py-template',
97+
'extensions.py-template',
98+
'data_providers.py-template',
99+
'strategies.py-template',
100+
'order_executors.py-template'
97101
]:
98-
99102
# Read the file
100103
with open(destination_path, 'r') as file:
101-
102104
file_data = file.read()
103105

104106
# Replace the placeholder with the

investing_algorithm_framework/management/commands/run_algorithm.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from investing_algorithm_framework.core.context import Context
55
from investing_algorithm_framework.configuration.config_constants import \
66
SETTINGS_CONTEXT_CONFIGURATION
7-
from investing_algorithm_framework.configuration import settings
87

98

109
class RunAlgorithmCommand(BaseCommand):
@@ -17,14 +16,14 @@ def add_arguments(self, parser) -> None:
1716
pass
1817

1918
def handle(self, *args, **options) -> Any:
19+
# Call the context
20+
context = Context()
2021

21-
# configure settings
22-
settings.configure()
22+
# configure the configuration
23+
context.config.configure()
2324

2425
# Load the context configuration
25-
__import__(settings[SETTINGS_CONTEXT_CONFIGURATION])
26+
__import__(context.config[SETTINGS_CONTEXT_CONFIGURATION])
2627

27-
# Create an investing_algorithm_framework context of the
28-
# investing_algorithm_framework and run it
29-
context = Context()
28+
# run the context
3029
context.start()

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
requests==2.22.0
21
wrapt==1.11.2
32
colorama==0.4.3
43
SQLAlchemy==1.3.13

tests/configuration/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from investing_algorithm_framework.configuration import ContextConfiguration
3+
from investing_algorithm_framework.configuration.config_constants import \
4+
SETTINGS_CONTEXT_CONFIGURATION, SETTINGS_LOGGING_CONFIG, \
5+
SETTINGS_MAX_CONCURRENT_WORKERS, SETTINGS_PROJECT_NAME, BASE_DIR
6+
from tests.resources.utils import random_string
7+
8+
9+
def test() -> None:
10+
config = ContextConfiguration()
11+
config.configure('tests.resources.standard_settings')
12+
assert config.configured
13+
14+
assert config[BASE_DIR] is not None
15+
assert config[SETTINGS_CONTEXT_CONFIGURATION] is not None
16+
assert config[SETTINGS_LOGGING_CONFIG] is not None
17+
assert config[SETTINGS_MAX_CONCURRENT_WORKERS] is not None
18+
assert config[SETTINGS_PROJECT_NAME] is not None
19+
20+
new_attribute = random_string(10)
21+
new_attribute_value = random_string(10)
22+
23+
config.set(new_attribute, new_attribute_value)
24+
25+
assert config[new_attribute] is not None
26+
assert config[new_attribute] == new_attribute_value
27+
28+
with pytest.raises(Exception):
29+
config.set(new_attribute, random_string(10))

tests/core/executors/test_executor.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,30 @@ def test() -> None:
8888
assert test_executor_observer.update_count == 3
8989
assert test_worker_observer.update_count == 7
9090

91+
executor = Executor(
92+
workers=[test_worker_one]
93+
)
94+
executor.add_observer(test_executor_observer)
95+
96+
# Start the executor
97+
executor.start()
98+
99+
sleep(2)
100+
101+
# Observers must have been updated by the executor
102+
assert test_executor_observer.update_count == 4
103+
assert test_worker_observer.update_count == 8
104+
105+
106+
class TestExceptionWorker(Worker):
107+
108+
def work(self, **kwargs: Dict[str, Any]) -> None:
109+
raise Exception("hello")
110+
111+
112+
def test_exception() -> None:
113+
test_worker = TestExceptionWorker()
114+
executor = Executor(workers=[test_worker])
115+
test_executor_observer = TestObserver()
116+
executor.add_observer(test_executor_observer)
117+
executor.start()

tests/core/resolvers/test_database_resolver.py

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

44
from tests.resources import BaseTestMixin
55
from tests.resources import utils
6-
from investing_algorithm_framework.configuration import settings
6+
from investing_algorithm_framework.configuration import ContextConfiguration
77
from investing_algorithm_framework.core.extensions import db
88

99

@@ -17,8 +17,9 @@ class TestDatabaseResolverConfiguration(BaseTestMixin):
1717
def setup_method(self) -> None:
1818
self.initialize_environment()
1919

20-
def test_configuration(self):
21-
settings.configure()
20+
def test_configuration(self) -> None:
21+
config_config = ContextConfiguration()
22+
config_config.configure()
2223
db.configure()
2324

2425
# Check if all properties are configured
@@ -29,16 +30,17 @@ def test_configuration(self):
2930
assert os.path.isfile(db.database_path) == True
3031

3132
def teardown_method(self) -> None:
32-
33-
if os.path.isfile(db.database_path):
34-
os.remove(db.database_path)
33+
pass
34+
# if os.path.isfile(db.database_path):
35+
# os.remove(db.database_path)
3536

3637

3738
class TestDatabaseResolverModel(BaseTestMixin):
3839

3940
def setup_method(self) -> None:
4041
self.initialize_environment()
41-
settings.configure()
42+
config_config = ContextConfiguration()
43+
config_config.configure()
4244
db.configure()
4345
db.initialize_tables()
4446

tests/management/test_create_standard_algo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ def test(self):
6060
'configuration',
6161
'settings.py')
6262
)
63+
assert os.path.isfile(
64+
os.path.join(
65+
tempdir,
66+
self.project_name,
67+
'configuration',
68+
'extensions.py')
69+
)
6370

6471
# Check if all data provider files are present
6572
assert os.path.isfile(

tests/templates/states/test_data_providing_state.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Dict, Any
2+
3+
import pytest
14
from time import sleep
25
from investing_algorithm_framework.templates.states.data_providing_state \
36
import DataProvidingState
@@ -6,6 +9,7 @@
69
from investing_algorithm_framework.core.utils import TimeUnit
710
from investing_algorithm_framework.core.context import Context
811
from investing_algorithm_framework.core.events import Observer
12+
from investing_algorithm_framework.core.workers import Worker
913

1014

1115
class TestObserver(Observer):
@@ -45,6 +49,21 @@ def provide_data(self) -> None:
4549
pass
4650

4751

52+
class WrongDataProviderOne:
53+
54+
def provide_data(self) -> None:
55+
pass
56+
57+
58+
class WrongDataProviderTwo(Worker):
59+
60+
def work(self, **kwargs: Dict[str, Any]) -> None:
61+
pass
62+
63+
def provide_data(self) -> None:
64+
pass
65+
66+
4867
def test() -> None:
4968
observer = TestObserver()
5069
data_provider_one = TestDataProviderOne()
@@ -73,3 +92,13 @@ def test() -> None:
7392

7493
assert len(data_providing_state.registered_data_providers) == 3
7594
assert observer.updated == 5
95+
96+
with pytest.raises(Exception):
97+
data_providing_state.register_data_providers([
98+
WrongDataProviderOne()
99+
])
100+
101+
with pytest.raises(Exception):
102+
data_providing_state.register_data_providers([
103+
WrongDataProviderTwo()
104+
])
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import pytest
2+
from time import sleep
3+
from investing_algorithm_framework.templates.states.ordering_state \
4+
import OrderingState
5+
from investing_algorithm_framework.templates.order_executors \
6+
import OrderExecutor, ScheduledOrderExecutor, RelationalOrderExecutor
7+
from investing_algorithm_framework.core.utils import TimeUnit
8+
from investing_algorithm_framework.core.context import Context
9+
from investing_algorithm_framework.core.events import Observer
10+
from investing_algorithm_framework.core.workers import Worker
11+
12+
13+
class TestObserver(Observer):
14+
updated: int = 0
15+
16+
def update(self, observable, **kwargs) -> None:
17+
TestObserver.updated += 1
18+
19+
20+
class CustomOrderingState(OrderingState):
21+
22+
def get_transition_state_class(self):
23+
return None
24+
25+
26+
class TestOrderExecutor(OrderExecutor):
27+
28+
id = 'TestOrderExecutorOne'
29+
30+
def execute_orders(self) -> None:
31+
pass
32+
33+
34+
class TestScheduledOrderExecutor(ScheduledOrderExecutor):
35+
36+
id = 'TestOrderExecutorTwo'
37+
time_interval = 1
38+
time_unit = TimeUnit.SECOND
39+
40+
def execute_orders(self) -> None:
41+
pass
42+
43+
44+
class TestRelationalOrderExecutor(RelationalOrderExecutor):
45+
id = 'TestOrderExecutorThree'
46+
run_after = TestScheduledOrderExecutor
47+
48+
def execute_orders(self) -> None:
49+
pass
50+
51+
52+
class WrongOrderExecutorOne:
53+
54+
def execute_orders(self) -> None:
55+
pass
56+
57+
58+
class WrongOrderExecutorTwo(Worker):
59+
60+
def work(self, **kwargs) -> None:
61+
pass
62+
63+
def execute_orders(self) -> None:
64+
pass
65+
66+
67+
def test() -> None:
68+
observer = TestObserver()
69+
order_executor_one = TestOrderExecutor()
70+
order_executor_one.add_observer(observer)
71+
order_executor_two = TestScheduledOrderExecutor()
72+
order_executor_two.add_observer(observer)
73+
order_executor_three = TestRelationalOrderExecutor()
74+
order_executor_three.add_observer(observer)
75+
76+
context = Context()
77+
CustomOrderingState.register_order_executors(
78+
[order_executor_one, order_executor_two]
79+
)
80+
ordering_state = CustomOrderingState(context)
81+
ordering_state.start()
82+
83+
assert len(ordering_state.registered_order_executors) == 2
84+
assert observer.updated == 2
85+
86+
sleep(1)
87+
88+
CustomOrderingState.register_order_executors(
89+
[order_executor_one, order_executor_two, order_executor_three]
90+
)
91+
ordering_state = CustomOrderingState(context)
92+
ordering_state.start()
93+
94+
assert len(ordering_state.registered_order_executors) == 3
95+
assert observer.updated == 5
96+
97+
with pytest.raises(Exception):
98+
ordering_state.register_order_executors([
99+
WrongOrderExecutorOne()
100+
])
101+
102+
with pytest.raises(Exception):
103+
ordering_state.register_order_executors([
104+
WrongOrderExecutorTwo()
105+
])

0 commit comments

Comments
 (0)