|
| 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