示例#1
0
    def test__update_order_result_should_NOT_start_timer_when_no_request_remains(
            self):
        dummy_result = [
            {
                "uuid": "mango",
                "state": "done",
                "type": "buy",
                "created_at": "today",
                "price": 5000,
                "executed_volume": 0.00001,
            },
            {
                "uuid": "orange",
                "type": "sell",
                "state": "cancel",
                "created_at": "yesterday",
                "price": 2000,
                "executed_volume": 0.1234,
            },
        ]
        dummy_request_mango = {
            "uuid": "mango",
            "request": {
                "id": "mango_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "mango_result",
                "state": "done",
                "type": "buy"
            },
        }
        dummy_request_orange = {
            "uuid": "orange",
            "request": {
                "id": "orange_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "orange_result",
                "state": "done",
                "type": "sell"
            },
        }
        trader = UpbitTrader()
        trader._call_callback = MagicMock()
        trader._query_order_list = MagicMock(return_value=dummy_result)
        trader._stop_timer = MagicMock()
        trader._start_timer = MagicMock()
        trader.order_map["mango"] = dummy_request_mango
        trader.order_map["orange"] = dummy_request_orange

        trader._update_order_result(None)

        mango_result = trader._call_callback.call_args_list[0][0][1]
        self.assertEqual(mango_result["date_time"], "today")
        self.assertEqual(mango_result["id"], "mango_result")
        self.assertEqual(mango_result["price"], 5000)
        self.assertEqual(mango_result["type"], "buy")
        self.assertEqual(mango_result["state"], "done")
        self.assertEqual(mango_result["amount"], 0.00001)
        self.assertEqual(trader._call_callback.call_args_list[0][0][0],
                         dummy_request_mango["callback"])

        orange_result = trader._call_callback.call_args_list[1][0][1]
        self.assertEqual(orange_result["date_time"], "yesterday")
        self.assertEqual(orange_result["id"], "orange_result")
        self.assertEqual(orange_result["price"], 2000)
        self.assertEqual(orange_result["type"], "sell")
        self.assertEqual(orange_result["state"], "done")
        self.assertEqual(orange_result["amount"], 0.1234)
        self.assertEqual(trader._call_callback.call_args_list[1][0][0],
                         dummy_request_orange["callback"])

        self.assertEqual(len(trader.order_map), 0)
        trader._stop_timer.assert_called_once()
        trader._start_timer.assert_not_called()
        trader._query_order_list.assert_called_once_with(["mango", "orange"])
示例#2
0
    def test__update_order_result_should_call__call_callback_and_keep_waiting_request(
            self):
        dummy_result = [
            {
                "uuid": "mango",
                "state": "done",
                "created_at": "today",
                "price": 500,
                "executed_volume": 0.007,
            },
            {
                "uuid": "apple",
                "state": "cancel",
                "created_at": "yesterday",
                "price": 1500,
                "executed_volume": 0.54321,
            },
        ]
        dummy_request_mango = {
            "uuid": "mango",
            "request": {
                "id": "mango_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "mango_result",
                "state": "done",
                "type": "buy"
            },
        }
        dummy_request_banana = {
            "uuid": "banana",
            "request": {
                "id": "banana_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "banana_result",
                "state": "done",
                "type": "sell"
            },
        }
        dummy_request_apple = {
            "uuid": "apple",
            "request": {
                "id": "apple_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "apple_result",
                "state": "done",
                "type": "buy"
            },
        }
        trader = UpbitTrader()
        trader._call_callback = MagicMock()
        trader._query_order_list = MagicMock(return_value=dummy_result)
        trader._stop_timer = MagicMock()
        trader._start_timer = MagicMock()
        trader.order_map["mango"] = dummy_request_mango
        trader.order_map["banana"] = dummy_request_banana
        trader.order_map["apple"] = dummy_request_apple

        trader._update_order_result(None)

        mango_result = trader._call_callback.call_args_list[0][0][1]
        self.assertEqual(mango_result["date_time"], "today")
        self.assertEqual(mango_result["id"], "mango_result")
        self.assertEqual(mango_result["price"], 500)
        self.assertEqual(mango_result["type"], "buy")
        self.assertEqual(mango_result["state"], "done")
        self.assertEqual(mango_result["amount"], 0.007)
        self.assertEqual(trader._call_callback.call_args_list[0][0][0],
                         dummy_request_mango["callback"])

        apple_result = trader._call_callback.call_args_list[1][0][1]
        self.assertEqual(apple_result["date_time"], "yesterday")
        self.assertEqual(apple_result["id"], "apple_result")
        self.assertEqual(apple_result["price"], 1500)
        self.assertEqual(mango_result["type"], "buy")
        self.assertEqual(mango_result["state"], "done")
        self.assertEqual(apple_result["amount"], 0.54321)
        self.assertEqual(trader._call_callback.call_args_list[1][0][0],
                         dummy_request_apple["callback"])

        self.assertEqual(len(trader.order_map), 1)
        self.assertEqual(trader.order_map["banana"]["request"]["id"],
                         "banana_id")
        trader._stop_timer.assert_called_once()
        trader._start_timer.assert_called_once()
        trader._query_order_list.assert_called_once_with(
            ["mango", "banana", "apple"])