Skip to content
Snippets Groups Projects
  • Florian Schröder's avatar
    c86635e6
    Add test for tutorial connectivity and game item tests · c86635e6
    Florian Schröder authored
    Expanded the tests in the 'test_study_server.py' file to include testing for tutorial connectivity. Added tests in 'test_start.py' to test the functionality of PlateDispenser and Sink game items. This included tests to validate the handling of dirty and clean plates and testing item drops and pick-ups. Various adjustments were made to players' positions to accommodate the tests.
    c86635e6
    History
    Add test for tutorial connectivity and game item tests
    Florian Schröder authored
    Expanded the tests in the 'test_study_server.py' file to include testing for tutorial connectivity. Added tests in 'test_start.py' to test the functionality of PlateDispenser and Sink game items. This included tests to validate the handling of dirty and clean plates and testing item drops and pick-ups. Various adjustments were made to players' positions to accommodate the tests.
test_study_server.py 3.87 KiB
import json
from unittest import mock

from fastapi import status
from fastapi.testclient import TestClient
from requests import Response

import cooperative_cuisine.study_server as study_server_module
from cooperative_cuisine.study_server import app


def test_valid_post_requests():
    test_response = Response()
    test_response.status_code = status.HTTP_200_OK
    test_response.encoding = "utf8"
    test_response._content = json.dumps(
        {
            "player_info": {
                "0": {
                    "player_id": "0",
                    "client_id": "ksjdhfkjsdfn",
                    "player_hash": "shdfbmsndfb",
                }
            },
            "env_id": "123456789",
            "recipe_graphs": [],
        }
    ).encode()
    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/start_study/124/1")

    assert res.status_code == status.HTTP_200_OK

    mock_call.assert_called_once()

    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/get_game_connection/124")

    assert res.status_code == status.HTTP_200_OK
    assert res.json()["player_info"] == {
        "0": {
            "player_id": "0",
            "client_id": "ksjdhfkjsdfn",
            "player_hash": "shdfbmsndfb",
        }
    }

    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/level_done/124")

    assert res.status_code == status.HTTP_200_OK


def test_invalid_post_requests():
    test_response = ""
    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/level_done/125")

    assert res.status_code == status.HTTP_409_CONFLICT

    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/get_game_connection/125")

    assert res.status_code == status.HTTP_409_CONFLICT


def test_game_server_crashed():
    test_response = Response()
    test_response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR

    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/start_study/124/1")

    assert res.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR


def test_tutorial():
    test_response = Response()
    test_response.status_code = status.HTTP_200_OK
    test_response.encoding = "utf8"
    test_response._content = json.dumps(
        {
            "player_info": {
                "0": {
                    "player_id": "0",
                    "client_id": "ksjdhfkjsdfn",
                    "player_hash": "shdfbmsndfb",
                }
            },
            "env_id": "123456789",
            "recipe_graphs": [],
        }
    ).encode()
    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/connect_to_tutorial/124")

    assert res.status_code == status.HTTP_200_OK

    mock_call.assert_called_once()

    with mock.patch.object(
        study_server_module, "request_game_server", return_value=test_response
    ) as mock_call:
        with TestClient(app) as client:
            res = client.post("/disconnect_from_tutorial/124")

    assert res.status_code == status.HTTP_200_OK


# TOOD test bots