def test_create_two_robots_same_position_different_grids(): Grids.reset() response = client.post("/grid/create/") body = json.loads(response.text) first_grid_id = body["grid_id"] response = client.post("/grid/create/") body = json.loads(response.text) second_grid_id = body["grid_id"] first_response = client.post( "/robot/create/", json={ "grid_id": first_grid_id, "position_x": 2, "position_y": 2, "facing": "up", }, ) second_response = client.post( "/robot/create/", json={ "grid_id": second_grid_id, "position_x": 2, "position_y": 2, "facing": "up", }, ) assert first_response.status_code == 200 assert second_response.status_code == 200 assert first_grid_id != second_grid_id
def test_insert_element_grid_not_foud(): Grids.reset() element = MagicMock() with pytest.raises(HTTPException) as e: Grids.insert_element(0, 1, 2, element) assert e.value.status_code == 404 assert e.value.detail == "grid not found"
def test_create_robot_in_same_space(): Grids.reset() response = client.post("/grid/create/") body = json.loads(response.text) client.post( "/robot/create/", json={ "grid_id": body["grid_id"], "position_x": 2, "position_y": 2, "facing": "up", }, ) new_response = client.post( "/robot/create/", json={ "grid_id": body["grid_id"], "position_x": 2, "position_y": 2, "facing": "up", }, ) new_body = json.loads(new_response.text) assert new_response.status_code == 400 assert new_body["detail"] == "space occupied"
def test_robot_kill_dinosaur_and_take_it_place(): Grids.reset() grid = client.post("/grid/create/") body = json.loads(grid.text) grid_id = body["grid_id"] robot = client.post( "/robot/create/", json={ "grid_id": grid_id, "position_x": 2, "position_y": 2, "facing": "up", }, ) body = json.loads(robot.text) robot_id = body["robot_id"] client.post( "/dinosaur/create/", json={ "grid_id": grid_id, "position_x": 1, "position_y": 2, }, ) response = client.post( "/robot/instruction/", json={ "grid_id": grid_id, "robot_id": robot_id, "instruction": "move", "direction": "forward", }, ) body = json.loads(response.text) assert response.status_code == 400 assert body["detail"] == "space occupied" client.post( "/robot/instruction/", json={ "grid_id": grid_id, "robot_id": robot_id, "instruction": "attack", }, ) response = client.post( "/robot/instruction/", json={ "grid_id": grid_id, "robot_id": robot_id, "instruction": "move", "direction": "forward", }, ) body = json.loads(response.text) assert response.status_code == 200
def test_insert_element_space_occupied(): Grids.reset() grid_id = Grids.create_grid() element = MagicMock() Grids.insert_element(grid_id, 1, 2, element) with pytest.raises(HTTPException) as e: Grids.insert_element(grid_id, 1, 2, element) assert e.value.status_code == 400 assert e.value.detail == "space occupied"
def test_update_robot_tracker(): Grids.reset() robot = MagicMock() robot.id.return_value = 1 Grids.update_robot_tracker(0, robot) assert Grids.robots_tracker == {0: {robot.id: robot}}
def test_get_element(): Grids.reset() grid_id = Grids.create_grid() element = MagicMock() Grids.insert_element(grid_id, 10, 10, element) assert Grids.grids[grid_id][10][10] == element new_element = Grids.get_element(grid_id, 10, 10) assert new_element == element
def test_delete_element(): Grids.reset() grid_id = Grids.create_grid() element = MagicMock() Grids.insert_element(grid_id, 10, 10, element) assert Grids.grids[grid_id][10][10] == element Grids.delete_element(grid_id, 10, 10) assert Grids.grids[grid_id][10][10] is None
def test_not_retrieving_invalid_robot(): Grids.reset() response = client.post("/grid/create/") assert response.status_code == 200 body = json.loads(response.text) new_response = client.post( "/robot/instruction/", json={ "grid_id": body["grid_id"], "robot_id": 0, "instruction": "move", "direction": "forward", }, ) assert new_response.status_code == 404
def test_create_robot(): Grids.reset() response = client.post("/grid/create/") assert response.status_code == 200 body = json.loads(response.text) new_response = client.post( "/robot/create/", json={ "grid_id": body["grid_id"], "position_x": 2, "position_y": 2, "facing": "up", }, ) assert new_response.status_code == 200
def test_robot_cant_move_outside_grid(): Grids.reset() grid = client.post("/grid/create/") body = json.loads(grid.text) grid_id = body["grid_id"] robot = client.post( "/robot/create/", json={ "grid_id": grid_id, "position_x": 1, "position_y": 1, "facing": "left", }, ) body = json.loads(robot.text) robot_id = body["robot_id"] response = client.post( "/robot/instruction/", json={ "grid_id": grid_id, "robot_id": robot_id, "instruction": "move", "direction": "forward", }, ) assert response.status_code == 200 response = client.post( "/robot/instruction/", json={ "grid_id": grid_id, "robot_id": robot_id, "instruction": "move", "direction": "forward", }, ) body = json.loads(response.text) assert response.status_code == 400 assert body["detail"] == "new position is invalid"
def test_delete_element_invalid_position(): Grids.reset() grid_id = Grids.create_grid() with pytest.raises(HTTPException) as e: Grids.delete_element(grid_id, -1, 10) assert e.value.status_code == 400 assert e.value.detail == "position is invalid" with pytest.raises(HTTPException) as e: Grids.delete_element(grid_id, 50, 10) assert e.value.status_code == 400 assert e.value.detail == "position is invalid" with pytest.raises(HTTPException) as e: Grids.delete_element(grid_id, 10, -1) assert e.value.status_code == 400 assert e.value.detail == "position is invalid" with pytest.raises(HTTPException) as e: Grids.delete_element(grid_id, 10, 50) assert e.value.status_code == 400 assert e.value.detail == "position is invalid"
def test_insert_element_invalid_position(): Grids.reset() grid_id = Grids.create_grid() element = MagicMock() with pytest.raises(HTTPException) as e: Grids.insert_element(grid_id, -1, 10, element) assert e.value.status_code == 400 assert e.value.detail == "new position is invalid" with pytest.raises(HTTPException) as e: Grids.insert_element(grid_id, 50, 10, element) assert e.value.status_code == 400 assert e.value.detail == "new position is invalid" with pytest.raises(HTTPException) as e: Grids.insert_element(grid_id, 10, -1, element) assert e.value.status_code == 400 assert e.value.detail == "new position is invalid" with pytest.raises(HTTPException) as e: Grids.insert_element(grid_id, 10, 50, element) assert e.value.status_code == 400 assert e.value.detail == "new position is invalid"
def test_delete_element_grid_not_found(): Grids.reset() with pytest.raises(HTTPException) as e: Grids.delete_element(0, 1, 2) assert e.value.status_code == 404 assert e.value.detail == "grid not found"
def test_get_new_robot_id(): Grids.reset() robot_id = Grids.get_new_robot_id(0) assert Grids.robot_counter[0] == 1 assert robot_id == 0
def test_create_grid(): Grids.reset() grid_id = Grids.create_grid() assert len(Grids.grids[grid_id]) == 50