|
| 1 | +import copy |
| 2 | +import re |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import pytest |
| 6 | +from fastapi.testclient import TestClient |
| 7 | +from pytest_mock import MockerFixture |
| 8 | +from tests.conftest import AssertableController |
| 9 | + |
1 | 10 | from fastcs.attributes import AttrR |
| 11 | +from fastcs.backends.rest.backend import RestBackend |
2 | 12 | from fastcs.datatypes import Bool, Float, Int |
3 | 13 |
|
4 | 14 |
|
| 15 | +def pascal_2_snake(input: list[str]) -> list[str]: |
| 16 | + snake_list = copy.deepcopy(input) |
| 17 | + snake_list[-1] = re.sub(r"(?<!^)(?=[A-Z])", "_", snake_list[-1]).lower() |
| 18 | + return snake_list |
| 19 | + |
| 20 | + |
5 | 21 | class TestRestServer: |
6 | | - def test_read_int(self, rest_client): |
7 | | - response = rest_client.get("/ReadInt") |
8 | | - assert response.status_code == 200 |
9 | | - assert response.json() == {"value": AttrR(Int())._value} |
| 22 | + @pytest.fixture(autouse=True) |
| 23 | + def setup_tests(self, mocker: MockerFixture): |
| 24 | + self.controller = AssertableController(mocker) |
| 25 | + app = RestBackend(self.controller)._server._app |
| 26 | + self.client = TestClient(app) |
10 | 27 |
|
11 | | - def test_read_write_int(self, rest_client): |
12 | | - response = rest_client.get("/ReadWriteInt") |
| 28 | + def client_read(self, path: list[str], expected: Any): |
| 29 | + route = "/" + "/".join(path) |
| 30 | + with self.controller.assertPerformed(pascal_2_snake(path), "READ"): |
| 31 | + response = self.client.get(route) |
13 | 32 | assert response.status_code == 200 |
14 | | - assert response.json() == {"value": AttrR(Int())._value} |
15 | | - response = rest_client.put("/ReadWriteInt", json={"value": AttrR(Int())._value}) |
| 33 | + assert response.json()["value"] == expected |
| 34 | + |
| 35 | + def client_write(self, path: list[str], value: Any): |
| 36 | + route = "/" + "/".join(path) |
| 37 | + with self.controller.assertPerformed(pascal_2_snake(path), "WRITE"): |
| 38 | + response = self.client.put(route, json={"value": value}) |
16 | 39 | assert response.status_code == 204 |
17 | 40 |
|
18 | | - def test_read_write_float(self, rest_client): |
19 | | - response = rest_client.get("/ReadWriteFloat") |
20 | | - assert response.status_code == 200 |
21 | | - assert response.json() == {"value": AttrR(Float())._value} |
22 | | - response = rest_client.put( |
23 | | - "/ReadWriteFloat", json={"value": AttrR(Float())._value} |
24 | | - ) |
| 41 | + def client_exec(self, path: list[str]): |
| 42 | + route = "/" + "/".join(path) |
| 43 | + with self.controller.assertPerformed(pascal_2_snake(path), "EXECUTE"): |
| 44 | + response = self.client.put(route) |
25 | 45 | assert response.status_code == 204 |
26 | 46 |
|
27 | | - def test_read_bool(self, rest_client): |
28 | | - response = rest_client.get("/ReadBool") |
29 | | - assert response.status_code == 200 |
30 | | - assert response.json() == {"value": AttrR(Bool())._value} |
| 47 | + def test_read_int(self): |
| 48 | + self.client_read(["ReadInt"], AttrR(Int())._value) |
31 | 49 |
|
32 | | - def test_write_bool(self, rest_client): |
33 | | - response = rest_client.put("/WriteBool", json={"value": AttrR(Bool())._value}) |
34 | | - assert response.status_code == 204 |
| 50 | + def test_read_write_int(self): |
| 51 | + self.client_read(["ReadWriteInt"], AttrR(Int())._value) |
| 52 | + self.client_write(["ReadWriteInt"], AttrR(Int())._value) |
| 53 | + |
| 54 | + def test_read_write_float(self): |
| 55 | + self.client_read(["ReadWriteFloat"], AttrR(Float())._value) |
| 56 | + self.client_write(["ReadWriteFloat"], AttrR(Float())._value) |
| 57 | + |
| 58 | + def test_read_bool(self): |
| 59 | + self.client_read(["ReadBool"], AttrR(Bool())._value) |
| 60 | + |
| 61 | + def test_write_bool(self): |
| 62 | + self.client_write(["WriteBool"], AttrR(Bool())._value) |
35 | 63 |
|
36 | 64 | # # We need to discuss enums |
37 | 65 | # def test_string_enum(self, rest_client): |
38 | 66 |
|
39 | | - def test_big_enum(self, rest_client): |
40 | | - response = rest_client.get("/BigEnum") |
41 | | - assert response.status_code == 200 |
42 | | - assert response.json() == { |
43 | | - "value": AttrR(Int(), allowed_values=list(range(1, 18)))._value |
44 | | - } |
| 67 | + def test_big_enum(self): |
| 68 | + self.client_read( |
| 69 | + ["BigEnum"], AttrR(Int(), allowed_values=list(range(1, 18)))._value |
| 70 | + ) |
45 | 71 |
|
46 | | - def test_go(self, rest_client): |
47 | | - response = rest_client.put("/Go") |
48 | | - assert response.status_code == 204 |
| 72 | + def test_go(self): |
| 73 | + self.client_exec(["Go"]) |
| 74 | + |
| 75 | + def test_read_child1(self): |
| 76 | + self.client_read(["SubController01", "ReadInt"], AttrR(Int())._value) |
| 77 | + |
| 78 | + def test_read_child2(self): |
| 79 | + self.client_read(["SubController02", "ReadInt"], AttrR(Int())._value) |
0 commit comments