Skip to content

Commit 262fde4

Browse files
committed
Apply testing feedback from upstream
1 parent 5c9731a commit 262fde4

File tree

2 files changed

+93
-89
lines changed

2 files changed

+93
-89
lines changed

tests/backends/rest/test_rest.py

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,88 @@
1-
import copy
2-
from typing import Any
3-
41
import pytest
52
from fastapi.testclient import TestClient
63

7-
from fastcs.attributes import AttrR
84
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
165

176

187
class TestRestServer:
19-
@pytest.fixture(scope="class", autouse=True)
20-
def setup_class(self, assertable_controller):
21-
self.controller = assertable_controller
22-
238
@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
2611
return TestClient(app)
2712

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")
5574
assert response.status_code == 204
5675

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

tests/conftest.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,24 @@ def __init__(self, mocker: MockerFixture) -> None:
102102
self.mocker = mocker
103103

104104
@contextmanager
105-
def assertPerformed(
106-
self, path: list[str], action: Literal["READ", "WRITE", "EXECUTE"]
107-
):
105+
def assert_read_here(self, path: list[str]):
106+
yield from self._assert_method(path, "get")
107+
108+
@contextmanager
109+
def assert_write_here(self, path: list[str]):
110+
yield from self._assert_method(path, "process")
111+
112+
@contextmanager
113+
def assert_execute_here(self, path: list[str]):
114+
yield from self._assert_method(path, "")
115+
116+
def _assert_method(self, path: list[str], method: Literal["get", "process", ""]):
117+
"""
118+
This context manager can be used to confirm that a fastcs
119+
controller's respective attribute or command methods are called
120+
a single time within a context block
121+
"""
108122
queue = copy.deepcopy(path)
109-
match action:
110-
case "READ":
111-
method = "get"
112-
case "WRITE":
113-
method = "process"
114-
case "EXECUTE":
115-
method = ""
116123

117124
# Navigate to subcontroller
118125
controller = self

0 commit comments

Comments
 (0)