Пример #1
0
    def test__query_order_should_call_bithumb_api_call_with_correct_query(
            self):
        trader = BithumbTrader("BTC")
        expected_query = {
            "order_currency": trader.market,
            "payment_currency": trader.market_currency,
            "order_id": "apple-007",
        }
        trader.bithumb_api_call = MagicMock()
        trader._query_order()
        trader.bithumb_api_call.assert_not_called()

        trader.bithumb_api_call = MagicMock()
        trader._query_order("apple-007")
        trader.bithumb_api_call.assert_called_once_with(
            "/info/order_detail", expected_query)
Пример #2
0
    def test_cancel_request_should_call__call_callback_when_order_is_traded_already(
            self):
        trader = BithumbTrader()
        trader._call_callback = MagicMock()
        dummy_request = {
            "order_id": "mango_id",
            "callback": MagicMock(),
            "result": {
                "state": "requested",
                "request": {
                    "id": "mango_request_1234",
                    "type": "buy",
                    "price": "888000",
                    "amount": "0.0001234",
                },
                "type": "buy",
                "price": 888000.0,
                "amount": 0.0001234,
                "msg": "success",
            },
        }
        trader.order_map["mango_request_1234"] = dummy_request

        _cancel_order = MagicMock(return_value=None)
        trader._query_order = MagicMock(
            return_value={
                "data": {
                    "order_status": "Completed",
                    "order_id": "mango_id",
                    "state": "done",
                    "transaction_date": "1572497603668315",
                    "order_price": 888000,
                    "order_qty": 0.007,
                },
            })
        trader.cancel_request("mango_request_1234")
        trader._query_order.assert_called_once_with("mango_id")

        self.assertEqual(trader._call_callback.call_args_list[0][0][0],
                         dummy_request["callback"])
        mango_result = trader._call_callback.call_args_list[0][0][1]
        self.assertEqual(mango_result["date_time"], "2019-10-31T13:53:23")
        self.assertEqual(mango_result["price"], 888000)
        self.assertEqual(mango_result["type"], "buy")
        self.assertEqual(mango_result["state"], "done")
        self.assertEqual(mango_result["amount"], 0.007)
Пример #3
0
    def test__update_order_result_should_NOT_start_timer_when_no_request_remains(
            self):
        dummy_result = [
            {
                "data": {
                    "order_status": "Completed",
                    "order_id": "mango",
                    "state": "done",
                    "transaction_date": "1572497603668315",
                    "order_price": 500,
                    "order_qty": 0.007,
                },
            },
            {
                "data": {
                    "order_status": "Completed",
                    "order_id": "apple",
                    "state": "cancel",
                    "transaction_date": "1572498603668315",
                    "order_price": 1500,
                    "order_qty": 0.54321,
                },
            },
        ]
        dummy_request_mango = {
            "order_id": "mango_order",
            "request": {
                "id": "mango_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "mango_result",
                "state": "done",
                "type": "buy"
            },
        }
        dummy_request_apple = {
            "order_id": "apple_order",
            "request": {
                "id": "apple_id"
            },
            "callback": MagicMock(),
            "result": {
                "id": "apple_result",
                "state": "done",
                "type": "buy"
            },
        }
        trader = BithumbTrader()
        trader._call_callback = MagicMock()
        trader._query_order = MagicMock(side_effect=dummy_result)
        trader._stop_timer = MagicMock()
        trader._start_timer = MagicMock()
        trader.order_map["mango"] = dummy_request_mango
        trader.order_map["apple"] = dummy_request_apple

        trader._update_order_result(None)

        self.assertEqual(
            trader._query_order.call_args_list,
            [call("mango_order"), call("apple_order")],
        )

        self.assertEqual(len(trader.order_map), 0)
        trader._stop_timer.assert_called_once()
        trader._start_timer.assert_not_called()
Пример #4
0
    def test__update_order_result_should_call__call_callback_and_keep_waiting_request(
            self):
        dummy_result = [
            {
                "data": {
                    "order_status": "Completed",
                    "order_id": "mango",
                    "state": "done",
                    "transaction_date": "1572497603668315",
                    "order_price": 500,
                    "order_qty": 0.007,
                    "contract": [{
                        "transaction_date": "1572497603668315",
                    }],
                },
            },
            {
                "data": {
                    "order_status": "Waiting",
                    "order_id": "banana",
                    "state": "cancel",
                    "transaction_date": "1572498603668315",
                    "order_price": 1500,
                    "order_qty": 0.54321,
                    "contract": [{
                        "transaction_date": "1572498603668315",
                    }],
                },
            },
            {
                "data": {
                    "order_status": "Completed",
                    "order_id": "apple",
                    "state": "cancel",
                    "transaction_date": "1572498603668315",
                    "order_price": 1500,
                    "order_qty": 0.54321,
                    "contract": [{
                        "transaction_date": "1572498603668315",
                    }],
                },
            },
        ]
        dummy_request_mango = {
            "order_id": "mango_order",
            "request": {
                "id": "mango_id"
            },
            "callback": MagicMock(),
            "result": {
                "state": "done",
                "type": "buy"
            },
        }
        dummy_request_banana = {
            "order_id": "banana_order",
            "request": {
                "id": "banana_id"
            },
            "callback": MagicMock(),
            "result": {
                "state": "done",
                "type": "sell"
            },
        }
        dummy_request_apple = {
            "order_id": "apple_order",
            "request": {
                "id": "apple_id"
            },
            "callback": MagicMock(),
            "result": {
                "state": "done",
                "type": "buy"
            },
        }
        trader = BithumbTrader()
        trader._call_callback = MagicMock()
        trader._query_order = MagicMock(side_effect=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)

        self.assertEqual(
            trader._query_order.call_args_list,
            [call("mango_order"),
             call("banana_order"),
             call("apple_order")],
        )

        trader._call_callback.assert_called()

        mango_result = trader._call_callback.call_args_list[0][0][1]
        self.assertEqual(mango_result["date_time"], "2019-10-31T13:53:23")
        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"], "2019-10-31T14:10:03")
        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()