Beispiel #1
0
def test_exit_run_on_no_data(mock_call_exchange):
    GIVEN("a handler")
    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=45678)
    handler = MarketHandler(
        market_id=45678,
        external_api=external_api,
        market_start_time="2019-01-01T00:00:00.000Z",
    )
    external_api.set_mediator(mediator=handler)
    mock_call_exchange.return_value = {}
    WHEN("we call run and no data is return from the api")
    for i in range(9):
        try:
            maybe_s = "s" if i > 0 else ""
            THEN(f"the handler will run {i + 1} time{maybe_s} without error")
            handler.run()
        except SystemExit:
            fail("Unexpected SystemExit")

    WHEN("we call run and no data is returned a 10th time")
    with raises(SystemExit) as system_exit:
        handler.run()
    THEN("the system will exit")
    assert system_exit.type == SystemExit
    assert system_exit.value.code == 0
Beispiel #2
0
def test_system_single(mock_post_instructions, mock_call_exchange):
    GIVEN("a market handler and the directory and file name of a test file")
    directory = "./data/29184567"
    market_id = 1.156230797
    file_name = f"{market_id}.txt"

    file = HistoricalExternalAPIFileHander(directory=directory, file=file_name)
    file_data = file.get_file_as_list()
    market_start_time = file.get_market_start_time()

    WHEN("we run the market handler for each record of the file")

    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=market_id)
    handler = MarketHandler(
        market_id=market_id,
        external_api=external_api,
        market_start_time=market_start_time,
    )
    external_api.set_mediator(mediator=handler)
    mock_post_instructions.side_effect = __mark_orders_successful
    orders = []
    fix_probability_ids = []
    for record in file_data:
        mock_call_exchange.return_value = [record]
        with freeze_time(record.get("process_time"), tz_offset=11):
            handler.run()
        fix_probability_ids = handler.data._get_fixed_probability_ids()
        orders = handler.get_orders()
        if fix_probability_ids or orders:
            THEN(
                "the data handler will not provide data to the models for fixed prob items"
            )
            ids_for_models = handler.data._get_ids_for_model_data()
            for probability_id in fix_probability_ids:
                assert probability_id not in ids_for_models
            THEN(
                "the data handler has fixed the probability of the correct item"
            )
            for order in orders:
                assert order.get("id") in fix_probability_ids
        THEN("the fixed probabilities and orders have the same length")
        assert len(fix_probability_ids) == len(orders)

    THEN("an order has been made")
    assert orders != []
    THEN("the id of the order is in the fix probability ids")
    for order in orders:
        assert order.get("id") in fix_probability_ids
    assert len(fix_probability_ids) == len(orders)
Beispiel #3
0
 def create_new_markets(self, schedule):
     scheduled_markets = []
     for market in schedule:
         market_id = float(market.get("marketId"))
         external_api = ExternalAPIMarketHandler(
             market_id=market_id, headers=self.external_api.get_headers())
         market_handler = MarketHandler(
             market_id=market_id,
             market_start_time=market.get("marketStartTime"),
             external_api=external_api,
         )
         external_api.set_mediator(mediator=market_handler)
         scheduled_markets.append(market_handler)
     return scheduled_markets
Beispiel #4
0
def test_system_multiple_set_prob(mock_post_instructions, mock_set_prob,
                                  mock_call_exchange):
    GIVEN("a market handler and the directory and file name of a test file")
    directory = "./data/29116016"
    market_id = 1.154592424
    file_name = f"{market_id}.txt"
    file = HistoricalExternalAPIFileHander(directory=directory, file=file_name)
    file_data = file.get_file_as_list()
    market_start_time = file.get_market_start_time()

    WHEN("we run the market handler for each record of the file")

    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=market_id)
    handler = MarketHandler(
        market_id=market_id,
        external_api=external_api,
        market_start_time=market_start_time,
    )
    external_api.set_mediator(mediator=handler)
    mock_post_instructions.side_effect = __mark_orders_successful

    fixed_probabilities = []
    call_count = 0
    for record in file_data:
        mock_call_exchange.return_value = [record]
        call_count = mock_set_prob.call_count
        with freeze_time(record.get("process_time"), tz_offset=11):
            handler.run()
        if mock_set_prob.call_count != call_count:
            THEN("the correct orders were passed to the method")
            args, kwargs = mock_set_prob.call_args
            fixed_probabilities.extend(kwargs.get("items"))
            orders = handler.get_orders()
            assert args == ()
            assert fixed_probabilities == orders

    THEN("multiple orders have been made")
    orders = handler.get_orders()
    assert orders != []
    assert len(orders) > 1
    THEN(
        "multiple calls to fix the probability of items being ordered were made"
    )
    assert call_count > 1
Beispiel #5
0
def test_exit_run_on_closed(mock_open_url):
    GIVEN("a handler")
    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=123456)
    handler = MarketHandler(
        market_id=123456,
        external_api=external_api,
        market_start_time="2019-01-13T04:19:00.000Z",
    )
    external_api.set_mediator(mediator=handler)
    closed_market_dict = __get_closed_market_dict()
    mock_open_url.return_value = closed_market_dict
    WHEN("we call run but the market has closed")
    with raises(SystemExit) as system_exit:
        with freeze_time(closed_market_dict.get("process_time"), tz_offset=11):
            handler.run()
    THEN("the system will exit")
    assert system_exit.type == SystemExit
    assert system_exit.value.code == 0
Beispiel #6
0
def test_system_set_prob(mock_post_instructions, mock_set_prob,
                         mock_call_exchange):
    GIVEN("a market handler and the directory and file name of a test file")
    directory = "./data/29184567"
    market_id = 1.156230797
    file_name = f"{market_id}.txt"
    file = HistoricalExternalAPIFileHander(directory=directory, file=file_name)
    file_data = file.get_file_as_list()
    market_start_time = file.get_market_start_time()

    WHEN("we run the market handler for each record of the file")

    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=market_id)

    handler = MarketHandler(
        market_id=market_id,
        external_api=external_api,
        market_start_time=market_start_time,
    )
    external_api.set_mediator(mediator=handler)
    mock_post_instructions.side_effect = __mark_orders_successful
    for record in file_data:
        mock_call_exchange.return_value = [record]
        with freeze_time(record.get("process_time"), tz_offset=11):
            handler.run()

    THEN("an order has been made")
    orders = handler.get_orders()
    assert orders != []
    THEN(
        "a single call to fix the probability of the items being ordered was made"
    )
    assert mock_set_prob.call_count == 1

    THEN("the correct orders were passed to the method")
    args, kwargs = mock_set_prob.call_args
    assert args == ()
    assert kwargs.get("items") == orders
Beispiel #7
0
def test_place_order(mock_notify, mock_open_url):

    GIVEN("a set of orders and a market handler")
    orders = get_test_orders()
    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=123456)
    handler = MarketHandler(
        market_id=123456,
        external_api=external_api,
        market_start_time="2019-01-01T00:00:00.000Z",
    )
    external_api.set_mediator(mediator=MockMediator())
    mock_open_url.return_value = get_test_orders_post_response()
    WHEN("we place the orders")
    handler.notify(event="new orders", data=orders)
    THEN("the notify method was called with the correct parameters")
    args, kwargs = mock_notify.call_args
    assert args == ()
    data = kwargs.get("data")
    assert data.get("response") == get_test_orders_post_response().get(
        "instructionReports")
    assert data.get("orders") == orders
    assert kwargs.get("event") == "orders posted"