|
1 | | -import copy |
2 | | -from typing import Any |
3 | | - |
4 | 1 | import pytest |
5 | 2 | from fastapi.testclient import TestClient |
6 | 3 |
|
7 | | -from fastcs.attributes import AttrR |
8 | 4 | from fastcs.backends.rest.backend import RestBackend |
9 | | -from fastcs.datatypes import Bool, Float, Int |
10 | | - |
11 | | - |
12 | | -def kebab_2_snake(input: list[str]) -> list[str]: |
13 | | - snake_list = copy.deepcopy(input) |
14 | | - snake_list[-1] = snake_list[-1].replace("-", "_") |
15 | | - return snake_list |
16 | 5 |
|
17 | 6 |
|
18 | 7 | class TestRestServer: |
19 | | - @pytest.fixture(scope="class", autouse=True) |
20 | | - def setup_class(self, assertable_controller): |
21 | | - self.controller = assertable_controller |
22 | | - |
23 | 8 | @pytest.fixture(scope="class") |
24 | | - def client(self): |
25 | | - app = RestBackend(self.controller)._server._app |
| 9 | + def client(self, assertable_controller): |
| 10 | + app = RestBackend(assertable_controller)._server._app |
26 | 11 | return TestClient(app) |
27 | 12 |
|
28 | | - @pytest.fixture(scope="class") |
29 | | - def client_read(self, client): |
30 | | - def _client_read(path: list[str], expected: Any): |
31 | | - route = "/" + "/".join(path) |
32 | | - with self.controller.assertPerformed(kebab_2_snake(path), "READ"): |
33 | | - response = client.get(route) |
34 | | - assert response.status_code == 200 |
35 | | - assert response.json()["value"] == expected |
36 | | - |
37 | | - return _client_read |
38 | | - |
39 | | - @pytest.fixture(scope="class") |
40 | | - def client_write(self, client): |
41 | | - def _client_write(path: list[str], value: Any): |
42 | | - route = "/" + "/".join(path) |
43 | | - with self.controller.assertPerformed(kebab_2_snake(path), "WRITE"): |
44 | | - response = client.put(route, json={"value": value}) |
45 | | - assert response.status_code == 204 |
46 | | - |
47 | | - return _client_write |
48 | | - |
49 | | - @pytest.fixture(scope="class") |
50 | | - def client_exec(self, client): |
51 | | - def _client_exec(path: list[str]): |
52 | | - route = "/" + "/".join(path) |
53 | | - with self.controller.assertPerformed(kebab_2_snake(path), "EXECUTE"): |
54 | | - response = client.put(route) |
| 13 | + def test_read_int(self, assertable_controller, client): |
| 14 | + expect = 0 |
| 15 | + with assertable_controller.assert_read_here(["read_int"]): |
| 16 | + response = client.get("/read-int") |
| 17 | + assert response.status_code == 200 |
| 18 | + assert response.json()["value"] == expect |
| 19 | + |
| 20 | + def test_read_write_int(self, assertable_controller, client): |
| 21 | + expect = 0 |
| 22 | + with assertable_controller.assert_read_here(["read_write_int"]): |
| 23 | + response = client.get("/read-write-int") |
| 24 | + assert response.status_code == 200 |
| 25 | + assert response.json()["value"] == expect |
| 26 | + new = 9 |
| 27 | + with assertable_controller.assert_write_here(["read_write_int"]): |
| 28 | + response = client.put("/read-write-int", json={"value": new}) |
| 29 | + assert client.get("/read-write-int").json()["value"] == new |
| 30 | + |
| 31 | + def test_read_write_float(self, assertable_controller, client): |
| 32 | + expect = 0 |
| 33 | + with assertable_controller.assert_read_here(["read_write_float"]): |
| 34 | + response = client.get("/read-write-float") |
| 35 | + assert response.status_code == 200 |
| 36 | + assert response.json()["value"] == expect |
| 37 | + new = 0.5 |
| 38 | + with assertable_controller.assert_write_here(["read_write_float"]): |
| 39 | + response = client.put("/read-write-float", json={"value": new}) |
| 40 | + assert client.get("/read-write-float").json()["value"] == new |
| 41 | + |
| 42 | + def test_read_bool(self, assertable_controller, client): |
| 43 | + expect = False |
| 44 | + with assertable_controller.assert_read_here(["read_bool"]): |
| 45 | + response = client.get("/read-bool") |
| 46 | + assert response.status_code == 200 |
| 47 | + assert response.json()["value"] == expect |
| 48 | + |
| 49 | + def test_write_bool(self, assertable_controller, client): |
| 50 | + with assertable_controller.assert_write_here(["write_bool"]): |
| 51 | + client.put("/write-bool", json={"value": True}) |
| 52 | + |
| 53 | + def test_string_enum(self, assertable_controller, client): |
| 54 | + expect = "" |
| 55 | + with assertable_controller.assert_read_here(["string_enum"]): |
| 56 | + response = client.get("/string-enum") |
| 57 | + assert response.status_code == 200 |
| 58 | + assert response.json()["value"] == expect |
| 59 | + new = "new" |
| 60 | + with assertable_controller.assert_write_here(["string_enum"]): |
| 61 | + response = client.put("/string-enum", json={"value": new}) |
| 62 | + assert client.get("/string-enum").json()["value"] == new |
| 63 | + |
| 64 | + def test_big_enum(self, assertable_controller, client): |
| 65 | + expect = 0 |
| 66 | + with assertable_controller.assert_read_here(["big_enum"]): |
| 67 | + response = client.get("/big-enum") |
| 68 | + assert response.status_code == 200 |
| 69 | + assert response.json()["value"] == expect |
| 70 | + |
| 71 | + def test_go(self, assertable_controller, client): |
| 72 | + with assertable_controller.assert_execute_here(["go"]): |
| 73 | + response = client.put("/go") |
55 | 74 | assert response.status_code == 204 |
56 | 75 |
|
57 | | - return _client_exec |
58 | | - |
59 | | - def test_read_int(self, client_read): |
60 | | - client_read(["read-int"], AttrR(Int())._value) |
61 | | - |
62 | | - def test_read_write_int(self, client_read, client_write): |
63 | | - client_read(["read-write-int"], AttrR(Int())._value) |
64 | | - client_write(["read-write-int"], AttrR(Int())._value) |
65 | | - |
66 | | - def test_read_write_float(self, client_read, client_write): |
67 | | - client_read(["read-write-float"], AttrR(Float())._value) |
68 | | - client_write(["read-write-float"], AttrR(Float())._value) |
69 | | - |
70 | | - def test_read_bool(self, client_read): |
71 | | - client_read(["read-bool"], AttrR(Bool())._value) |
72 | | - |
73 | | - def test_write_bool(self, client_write): |
74 | | - client_write(["write-bool"], AttrR(Bool())._value) |
75 | | - |
76 | | - # # We need to discuss enums |
77 | | - # def test_string_enum(self, client_read, client_write): |
78 | | - |
79 | | - def test_big_enum(self, client_read): |
80 | | - client_read( |
81 | | - ["big-enum"], AttrR(Int(), allowed_values=list(range(1, 18)))._value |
82 | | - ) |
83 | | - |
84 | | - def test_go(self, client_exec): |
85 | | - client_exec(["go"]) |
86 | | - |
87 | | - def test_read_child1(self, client_read): |
88 | | - client_read(["SubController01", "read-int"], AttrR(Int())._value) |
89 | | - |
90 | | - def test_read_child2(self, client_read): |
91 | | - client_read(["SubController02", "read-int"], AttrR(Int())._value) |
| 76 | + def test_read_child1(self, assertable_controller, client): |
| 77 | + expect = 0 |
| 78 | + with assertable_controller.assert_read_here(["SubController01", "read_int"]): |
| 79 | + response = client.get("/SubController01/read-int") |
| 80 | + assert response.status_code == 200 |
| 81 | + assert response.json()["value"] == expect |
| 82 | + |
| 83 | + def test_read_child2(self, assertable_controller, client): |
| 84 | + expect = 0 |
| 85 | + with assertable_controller.assert_read_here(["SubController02", "read_int"]): |
| 86 | + response = client.get("/SubController02/read-int") |
| 87 | + assert response.status_code == 200 |
| 88 | + assert response.json()["value"] == expect |
0 commit comments