Esempio n. 1
0
    def test_retrieve_strategy_spec_view(self):
        strategy = StrategyFactory(
            specification={
                "specVersion": "v0.1.0",
                "parameters": [
                    {
                        "code": "param1",
                        "name": "Param1",
                        "type": "string",
                        "description": "",
                        "default": "test1",
                        "editable": True,
                    },
                    {
                        "code": "param2",
                        "name": "Param2",
                        "type": "string",
                        "description": "",
                        "default": "test2",
                        "editable": True,
                    },
                ],
            }
        )
        robot = RobotFactory(strategy=strategy, credential=self.admin_user_credential)
        robot.strategy_parameters = {
            "param1": "new1",
            "param2": "new2",
        }
        robot.save(update_fields=["strategy_parameters"])
        self.login(username=self.admin_user.username, password="******")

        response = self.get("api:robot-strategy-spec-view", pk=robot.pk)
        self.response_200(response)
        self.assertEqual(
            response.data,
            {
                "specVersion": "v0.1.0",
                "parameters": [
                    {
                        "code": "param1",
                        "name": "Param1",
                        "type": "string",
                        "description": "",
                        "default": "test1",
                        "value": "new1",
                        "editable": True,
                    },
                    {
                        "code": "param2",
                        "name": "Param2",
                        "type": "string",
                        "description": "",
                        "default": "test2",
                        "value": "new2",
                        "editable": True,
                    },
                ],
            },
        )
Esempio n. 2
0
 def test_create_valid_robot(self):
     credential = CredentialFactory(user=self.admin_user)
     strategy = StrategyFactory()
     self.client.credentials(HTTP_AUTHORIZATION="Token " +
                             self.admin_user_token.key)
     url = self.reverse("api:robot-list")
     data = {
         "data": {
             "type": "robots",
             "attributes": {
                 "name": "Robot-1",
                 "pair": "BTCUSDT",
                 "market_type": "linear_perpetual",
                 "enabled": True,
                 "credential": {
                     "type": "credentials",
                     "id": credential.pk
                 },
                 "strategy": {
                     "type": "strategies",
                     "id": strategy.pk
                 },
             },
         }
     }
     response = self.client.post(url, data=data)
     self.response_201(response)
Esempio n. 3
0
def run():
    name = "演示策略"
    spec = (pathlib.Path(str(settings.APPS_DIR)).joinpath(
        "scripts", "fake",
        "strategy-specification.json").read_text(encoding="utf-8"))
    StrategyFactory(
        name=name,
        specification=json.loads(spec),
    )
    print("Demo strategy was created.")
Esempio n. 4
0
    def test_retrieve_permission(self):
        strategy = StrategyFactory()

        # anonymous user
        response = self.get("api:strategy-detail", pk=strategy.pk)
        self.response_401(response)

        # normal user
        self.login(username=self.normal_user.username, password="******")
        response = self.get("api:strategy-detail", pk=strategy.pk)
        self.response_403(response)
Esempio n. 5
0
    def test_create_valid_robot(self):
        credential = CredentialFactory(user=self.admin_user)
        strategy = StrategyFactory()
        self.login(username=self.admin_user.username, password="******")

        data = {
            "name": "Valid",
            "pair": "BTCUSDT",
            "market_type": "linear_perpetual",
            "target_currency": "USDT",
            "credential": credential.pk,
            "strategy": strategy.pk,
        }
        response = self.post("api:robot-list", data=data)
        self.response_201(response)
Esempio n. 6
0
    def test_adjust_robot_strategy_parameters(self):
        strategy = StrategyFactory(
            specification={
                "specVersion":
                "v0.1.0",
                "parameters": [
                    {
                        "code": "code1",
                        "name": "Code1",
                        "type": "string",
                        "description": "",
                        "default": "old1",
                        "editable": True,
                    },
                    {
                        "code": "code2",
                        "name": "Code2",
                        "type": "string",
                        "description": "",
                        "default": "old2",
                        "editable": True,
                    },
                ],
            })
        robot = RobotFactory(strategy=strategy,
                             credential=self.admin_user_credential)
        self.login(username=self.admin_user.username, password="******")

        data = {
            "code1": "new1",
            "code2": "new2",
        }
        response = self.patch("api:robot-strategy-parameters",
                              data=data,
                              pk=robot.pk)
        self.response_200(response)
        robot.refresh_from_db()
        self.assertDictEqual(
            robot.strategy_parameters,
            {
                "code1": "new1",
                "code2": "new2",
            },
        )
Esempio n. 7
0
 def test_retrieve_strategy_parameters(self):
     strategy = StrategyFactory(
         specification={
             "specVersion": "v0.1.0",
             "parameters": [
                 {
                     "code": "param1",
                     "name": "Param1",
                     "type": "string",
                     "description": "",
                     "default": "test1",
                     "editable": True,
                 },
                 {
                     "code": "param2",
                     "name": "Param2",
                     "type": "string",
                     "description": "",
                     "default": "test2",
                     "editable": True,
                 },
             ],
         }
     )
     robot = RobotFactory(strategy=strategy, credential=self.admin_user_credential)
     response = self.get(
         "api:robot-strategy-parameters",
         pk=robot.pk,
         extra={"HTTP_X_API_KEY": self.x_api_key},
     )
     self.response_200(response)
     self.assertEqual(
         response.data,
         {
             "param1": "test1",
             "param2": "test2",
         },
     )
Esempio n. 8
0
 def test_retrieve_strategy(self):
     strategy = StrategyFactory()
     self.login(username=self.admin_user.username, password="******")
     response = self.get("api:strategy-detail", pk=strategy.pk)
     self.response_200(response)
Esempio n. 9
0
 def test_list_strategy(self):
     StrategyFactory.create_batch(5)
     self.login(username=self.admin_user.username, password="******")
     response = self.get(self.strategy_list_url)
     self.response_200(response)
     self.assertEqual(len(response.data["results"]), 5)