Exemple #1
0
 def convert(
     self,
     internal_model: PlayerInternal = Provide[Container.internal_model],
     api_info: PlayerAPIInfo = Provide[Container.api_info],
 ) -> PlayerMeasurementResponse:
     return PlayerMeasurementResponse(
         links=HALBase.from_routes_with_requested(internal_model.routes,
                                                  api_info.name,
                                                  api_info.method).links,
         white=MeasurementInfoToDTO.from_model(self.white_info).convert(),
         black=MeasurementInfoToDTO.from_model(self.black_info).convert(),
     )
Exemple #2
0
 def convert(
     self,
     internal_model: PlayerInternal = Provide[Container.internal_model],
     api_info: PlayerAPIInfo = Provide[Container.api_info],
 ) -> PlayerGameResponse:
     return PlayerGameResponse(
         links=HALBase.from_routes_with_requested(internal_model.routes,
                                                  api_info.name,
                                                  api_info.method).links,
         fens=self.fens,
         sans=self.sans,
         result=self.result,
     )
Exemple #3
0
 def convert(
     self,
     internal_model: PlayerInternal = Provide[Container.internal_model],
     api_info: PlayerAPIInfo = Provide[Container.api_info],
 ) -> PlayerHTTPStatusErrorResponse:
     return PlayerHTTPStatusErrorResponse(
         links=HALBase.from_routes_with_requested(internal_model.routes,
                                                  api_info.name,
                                                  api_info.method).links,
         message=self.model.message,
         location="body",
         param=", ".join(self.dto_dict.keys()),
         value=list(self.dto_dict.values()),
         error=self.model.error,
     )
Exemple #4
0
async def test_game(async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [2],
                "legal_moves": [[]],
            },
        )
    ])

    response = await async_client.post(
        url="/player/game",
        json={
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json() == {
        "links": {
            rel: {
                "href": url.href
            }
            for rel, url in HALBase.from_routes_with_requested(
                {
                    "trajectory": "/player/trajectory",
                    "game": "/player/game",
                    "measurement": "/player/measurement",
                },
                "game",
                "post",
            ).links.items()
        },
        "fens": [FEN.starting()],
        "sans": [],
        "result": "1-0",
    }
Exemple #5
0
async def test_measurement(async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [2, 3, 1],
                "legal_moves": [
                    [],
                    [],
                    [
                        "e4e5",
                        "e4e6",
                        "e4e7",
                        "f4e5",
                        "f4g5",
                        "f4h6",
                        "g4e5",
                        "g4f6",
                        "g4h6",
                        "h4g5",
                        "h5h6",
                    ],
                ],
            },
        ),
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [2],
                "legal_moves": [[]],
            },
        ),
    ])

    respx.post("http://fake-env/model/next-fen").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_fens": [FEN.first()]},
        )
    ])

    respx.post("http://fake-ai/ai/next-san").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_sans": [SAN.first()]},
        )
    ])

    response = await async_client.post(
        url="/player/measurement",
        json={
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
            "playtime": 3,
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json() == {
        "links": {
            rel: {
                "href": url.href
            }
            for rel, url in HALBase.from_routes_with_requested(
                {
                    "trajectory": "/player/trajectory",
                    "game": "/player/game",
                    "measurement": "/player/measurement",
                },
                "measurement",
                "post",
            ).links.items()
        },
        "white": {
            "score": 1.5,
            "win": 1,
            "draw": 1,
            "lose": 1
        },
        "black": {
            "score": 1.5,
            "win": 1,
            "draw": 1,
            "lose": 1
        },
    }
Exemple #6
0
async def test_trajectory(async_client: AsyncClient) -> None:
    respx.post("http://fake-env/model/fen-status").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={
                "statuses": [1],
                "legal_moves": [[
                    "e4e5",
                    "e4e6",
                    "e4e7",
                    "f4e5",
                    "f4g5",
                    "f4h6",
                    "g4e5",
                    "g4f6",
                    "g4h6",
                    "h4g5",
                    "h5h6",
                ]],
            },
        )
    ] * 8)

    respx.post("http://fake-env/model/next-fen").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_fens": [FEN.first()]},
        ),
        Response(
            status.HTTP_200_OK,
            json={"next_fens": [FEN.starting()]},
        ),
    ] * 3)

    respx.post("http://fake-ai/ai/next-san").mock(side_effect=[
        Response(
            status.HTTP_200_OK,
            json={"next_sans": [SAN.first()]},
        ),
    ] * 6)

    response = await async_client.post(
        url="/player/trajectory",
        json={
            "fens": [FEN.starting(), FEN.first()],
            "white": {
                "url": "http://fake-ai"
            },
            "black": {
                "url": "http://fake-ai"
            },
            "step": 3,
        },
    )

    assert response.status_code == status.HTTP_200_OK
    assert response.json() == {
        "links": {
            rel: {
                "href": url.href
            }
            for rel, url in HALBase.from_routes_with_requested(
                {
                    "trajectory": "/player/trajectory",
                    "game": "/player/game",
                    "measurement": "/player/measurement",
                },
                "trajectory",
                "post",
            ).links.items()
        },
        "fens": (([[FEN.starting()] * 2] * 2) + ([[FEN.first()] * 2] * 2)),
        "sans": [[SAN.first()]] * 4,
        "results": [[0, 0]] * 4,
    }