示例#1
0
    def test__send_text_message_shoul_call_sendMessage_api_correctly(self):
        tcb = TelegramController()
        tcb.post_worker = MagicMock()
        tcb.TOKEN = "banana"
        tcb.CHAT_ID = "to_banana"
        tcb._send_http = MagicMock()
        tcb._send_text_message("hello~ banana")
        tcb.post_worker.post_task.assert_called_once_with(ANY)
        task = tcb.post_worker.post_task.call_args[0][0]
        tcb.post_worker.post_task.call_args[0][0]["runnable"](task)

        tcb._send_http.assert_called_once_with(
            "https://api.telegram.org/banana/sendMessage?chat_id=to_banana&text=hello%7E%20banana"
        )
示例#2
0
    def test__query_score_should_call_next_setup_message_correctly_when_step_1(
            self):
        tcb = TelegramController()
        tcb.in_progress_step = 1
        tcb.operator = MagicMock()
        tcb._send_text_message = MagicMock()
        tcb._send_image_message = MagicMock()
        tcb._query_score("1")

        tcb.operator.get_score.assert_called_once()
        tcb._send_text_message.assert_called_once_with("조회중입니다",
                                                       tcb.main_keyboard)
        self.assertEqual(tcb.in_progress, None)
        self.assertEqual(tcb.in_progress_step, 0)
        self.assertEqual(tcb.operator.get_score.call_args[0][1], (60 * 6, -1))
        callback = tcb.operator.get_score.call_args[0][0]
        callback(None)
        tcb._send_text_message.assert_called_with("수익률 조회중 문제가 발생하였습니다.",
                                                  tcb.main_keyboard)
        callback((100, 200, 0.5, 0.9, "test.jpg", 0, 0, 0, ("12-01", "12-05",
                                                            "12-08")))
        tcb._send_text_message.assert_called_with(
            "12-05 - 12-08\n자산 100 -> 200\n구간 수익률 100.0\n12-01~\n누적 수익률 0.5\n비교 수익률 0.9\n",
            tcb.main_keyboard,
        )
        tcb._send_image_message.assert_called_with("test.jpg")
示例#3
0
    def test__query_score_should_call_send_error_message_when_not_running(
            self):
        tcb = TelegramController()
        tcb._send_text_message = MagicMock()
        tcb._query_score("1")

        tcb._send_text_message.assert_called_once_with("자동 거래 운영중이 아닙니다",
                                                       tcb.main_keyboard)
        self.assertEqual(tcb.in_progress, None)
        self.assertEqual(tcb.in_progress_step, 0)
示例#4
0
 def test__start_trading_should_reset_with_wrong_input_when_step_1(self):
     tcb = TelegramController()
     tcb.in_progress_step = 1
     tcb._send_text_message = MagicMock()
     tcb._start_trading("5000.5")
     wrong_message = "자동 거래가 시작되지 않았습니다.\n처음부터 다시 시작해주세요"
     tcb._send_text_message.assert_called_once_with(wrong_message,
                                                    tcb.main_keyboard)
     self.assertEqual(tcb.in_progress, None)
     self.assertEqual(tcb.in_progress_step, 0)
示例#5
0
    def test__query_state_should_call__send_text_message_with_correct_message(
            self):
        tcb = TelegramController()
        tcb._send_text_message = MagicMock()
        tcb._query_state("state")
        tcb._send_text_message.assert_called_with("자동 거래 시작 전입니다")

        tcb.operator = "mango"
        tcb._query_state("state")
        tcb._send_text_message.assert_called_with("자동 거래 운영 중입니다")
示例#6
0
 def test__query_score_should_reset_with_wrong_input_when_step_1(self):
     tcb = TelegramController()
     tcb.in_progress_step = 1
     tcb.operator = MagicMock()
     tcb._send_text_message = MagicMock()
     tcb._query_score("7")
     wrong_message = "다시 시작해 주세요"
     tcb._send_text_message.assert_called_once_with(wrong_message,
                                                    tcb.main_keyboard)
     self.assertEqual(tcb.in_progress, None)
     self.assertEqual(tcb.in_progress_step, 0)
示例#7
0
    def test__start_trading_should_call_next_setup_message_correctly_when_step_1(
            self):
        tcb = TelegramController()
        tcb.in_progress_step = 1
        tcb._send_text_message = MagicMock()
        tcb._start_trading("5000")

        tcb._send_text_message.assert_called_once_with(
            tcb.setup_list[1]["guide"], tcb.setup_list[1]["keyboard"])
        self.assertEqual(tcb.in_progress, tcb._start_trading)
        self.assertEqual(tcb.in_progress_step, 2)
示例#8
0
    def test__query_score_should_call_next_setup_message_correctly_when_step_0(
            self):
        tcb = TelegramController()
        tcb.in_progress_step = 0
        tcb.operator = MagicMock()
        tcb._send_text_message = MagicMock()
        tcb._query_score("1")

        tcb._send_text_message.assert_called_once_with(
            tcb.score_query_list[0]["guide"],
            tcb.score_query_list[0]["keyboard"])
        self.assertEqual(tcb.in_progress, tcb._query_score)
        self.assertEqual(tcb.in_progress_step, 1)
示例#9
0
    def test__start_trading_should_call_next_setup_message_correctly_when_step_5(
            self, mock_start, mock_initialize):
        tcb = TelegramController()
        tcb.in_progress_step = 5
        tcb._send_text_message = MagicMock()
        tcb.strategy = MagicMock()
        tcb.trader = MagicMock()
        tcb._start_trading("y")

        tcb._send_text_message.assert_called_once_with(ANY, tcb.main_keyboard)
        self.assertIsNotNone(tcb.operator)
        self.assertEqual(tcb.in_progress, None)
        self.assertEqual(tcb.in_progress_step, 0)
示例#10
0
    def test__start_trading_should_call_next_setup_message_correctly_when_step_3(
            self):
        tcb = TelegramController()
        tcb.in_progress_step = 3
        tcb._send_text_message = MagicMock()
        tcb.currency = "BTC"
        tcb._start_trading("upbit")

        tcb._send_text_message.assert_called_once_with(
            tcb.setup_list[3]["guide"], tcb.setup_list[3]["keyboard"])
        self.assertIsNotNone(tcb.trader)
        self.assertIsNotNone(tcb.data_provider)
        self.assertEqual(tcb.in_progress, tcb._start_trading)
        self.assertEqual(tcb.in_progress_step, 4)
示例#11
0
    def test__start_trading_should_call_next_setup_message_correctly_when_step_4(
            self):
        tcb = TelegramController()
        tcb.in_progress_step = 4
        tcb._send_text_message = MagicMock()
        tcb.trader = MagicMock()
        tcb.currency = "mango"
        tcb.trader.NAME = "mango trader"
        tcb.strategy = MagicMock()
        tcb.strategy.NAME = "mango strategy"
        tcb.budget = 500
        tcb._start_trading("SMA")

        tcb._send_text_message.assert_called_once_with(
            f"화폐: mango\n전략: {StrategySma0.NAME}\n거래소: mango trader\n예산: 500\n자동 거래를 시작할까요?",
            tcb.setup_list[4]["keyboard"],
        )
        self.assertIsNotNone(tcb.strategy)
        self.assertEqual(tcb.in_progress, tcb._start_trading)
        self.assertEqual(tcb.in_progress_step, 5)
示例#12
0
    def test__execute_command_should_call_action_correctly(self):
        tcb = TelegramController()
        tcb._send_text_message = MagicMock()
        tcb.main_keyboard = "mango keyboard"
        dummy_command_list = [
            {
                "guide": "{0:15}자동 거래 시작".format("1. 시작"),
                "cmd": ["시작", "1", "1. 시작"],
                "action": MagicMock(),
            },
            {
                "guide": "{0:15}자동 거래 중지".format("2. 중지"),
                "cmd": ["중지", "2", "2. 중지"],
                "action": MagicMock(),
            },
        ]
        tcb.command_list = dummy_command_list
        tcb._execute_command("안녕~")
        tcb._send_text_message.assert_called_once_with(
            "자동 거래 시작 전입니다.\n명령어를 입력해주세요.\n\n1. 시작          자동 거래 시작\n2. 중지          자동 거래 중지\n",
            "mango keyboard",
        )

        tcb._execute_command("1")
        dummy_command_list[0]["action"].assert_called_with("1")

        tcb._execute_command("시작")
        dummy_command_list[0]["action"].assert_called_with("시작")

        tcb._execute_command("2")
        dummy_command_list[1]["action"].assert_called_with("2")

        tcb._execute_command("중지")
        dummy_command_list[1]["action"].assert_called_with("중지")

        dummy_in_progress = MagicMock()
        tcb.in_progress = dummy_in_progress
        tcb._execute_command("2. 중지")
        dummy_in_progress.assert_called_once_with("2. 중지")
示例#13
0
    def test__stop_trading_should_reset_all_variable_related_operating(self):
        tcb = TelegramController()
        tcb.operator = MagicMock()
        tcb.strategy = "mango_strategy"
        tcb.data_provider = "mango_data_provider"
        tcb.trader = "mango_trader"
        tcb.budget = "mango_budget"
        tcb.operator.stop = MagicMock(
            return_value={
                "summary": (100, 200, 0.5, 0.9, "test.jpg", 0, 0, 0,
                            ("12-01", "12-05", "12-08"))
            })
        tcb._send_text_message = MagicMock()

        tcb._stop_trading("2")
        tcb._send_text_message.assert_called_once_with(
            "자동 거래가 중지되었습니다\n12-05 - 12-08\n자산 100 -> 200\n수익률 0.5\n비교 수익률 0.9\n",
            tcb.main_keyboard)
        self.assertEqual(tcb.operator, None)
        self.assertEqual(tcb.strategy, None)
        self.assertEqual(tcb.data_provider, None)
        self.assertEqual(tcb.trader, None)
        self.assertEqual(tcb.budget, None)