|
1 | 1 | import copy |
2 | 2 | import json |
3 | | -import re |
4 | 3 | from typing import Any |
5 | 4 |
|
6 | 5 | import pytest |
7 | 6 | from fastapi.testclient import TestClient |
8 | 7 |
|
9 | | -from fastcs.attributes import AttrR |
10 | 8 | from fastcs.backends.graphQL.backend import GraphQLBackend |
11 | | -from fastcs.datatypes import Bool, Float, Int |
12 | | - |
13 | | - |
14 | | -def pascal_2_snake(input: list[str]) -> list[str]: |
15 | | - snake_list = copy.deepcopy(input) |
16 | | - snake_list[-1] = re.sub(r"(?<!^)(?=[A-Z])", "_", snake_list[-1]).lower() |
17 | | - return snake_list |
18 | 9 |
|
19 | 10 |
|
20 | 11 | def nest_query(path: list[str]) -> str: |
@@ -51,76 +42,117 @@ def nest_responce(path: list[str], value: Any) -> dict: |
51 | 42 |
|
52 | 43 |
|
53 | 44 | class TestGraphQLServer: |
54 | | - @pytest.fixture(scope="class", autouse=True) |
55 | | - def setup_class(self, assertable_controller): |
56 | | - self.controller = assertable_controller |
57 | | - |
58 | 45 | @pytest.fixture(scope="class") |
59 | | - def client(self): |
60 | | - app = GraphQLBackend(self.controller)._server._app |
| 46 | + def client(self, assertable_controller): |
| 47 | + app = GraphQLBackend(assertable_controller)._server._app |
61 | 48 | return TestClient(app) |
62 | 49 |
|
63 | | - @pytest.fixture(scope="class") |
64 | | - def client_read(self, client): |
65 | | - def _client_read(path: list[str], expected: Any): |
66 | | - query = f"query {{ {nest_query(path)} }}" |
67 | | - with self.controller.assertPerformed(pascal_2_snake(path), "READ"): |
68 | | - response = client.post("/graphql", json={"query": query}) |
69 | | - assert response.status_code == 200 |
70 | | - assert response.json()["data"] == nest_responce(path, expected) |
71 | | - |
72 | | - return _client_read |
73 | | - |
74 | | - @pytest.fixture(scope="class") |
75 | | - def client_write(self, client): |
76 | | - def _client_write(path: list[str], value: Any): |
77 | | - mutation = f"mutation {{ {nest_mutation(path, value)} }}" |
78 | | - with self.controller.assertPerformed(pascal_2_snake(path), "WRITE"): |
79 | | - response = client.post("/graphql", json={"query": mutation}) |
80 | | - assert response.status_code == 200 |
81 | | - assert response.json()["data"] == nest_responce(path, value) |
82 | | - |
83 | | - return _client_write |
84 | | - |
85 | | - @pytest.fixture(scope="class") |
86 | | - def client_exec(self, client): |
87 | | - def _client_exec(path: list[str]): |
88 | | - mutation = f"mutation {{ {nest_query(path)} }}" |
89 | | - with self.controller.assertPerformed(pascal_2_snake(path), "EXECUTE"): |
90 | | - response = client.post("/graphql", json={"query": mutation}) |
91 | | - assert response.status_code == 200 |
92 | | - assert response.json()["data"] == {path[-1]: True} |
93 | | - |
94 | | - return _client_exec |
95 | | - |
96 | | - def test_read_int(self, client_read): |
97 | | - client_read(["ReadInt"], AttrR(Int())._value) |
98 | | - |
99 | | - def test_read_write_int(self, client_read, client_write): |
100 | | - client_read(["ReadWriteInt"], AttrR(Int())._value) |
101 | | - client_write(["ReadWriteInt"], AttrR(Int())._value) |
102 | | - |
103 | | - def test_read_write_float(self, client_read, client_write): |
104 | | - client_read(["ReadWriteFloat"], AttrR(Float())._value) |
105 | | - client_write(["ReadWriteFloat"], AttrR(Float())._value) |
106 | | - |
107 | | - def test_read_bool(self, client_read): |
108 | | - client_read(["ReadBool"], AttrR(Bool())._value) |
109 | | - |
110 | | - def test_write_bool(self, client_write): |
111 | | - client_write(["WriteBool"], AttrR(Bool())._value) |
112 | | - |
113 | | - # # We need to discuss enums |
114 | | - # def test_string_enum(self, client_read, client_write): |
115 | | - |
116 | | - def test_big_enum(self, client_read): |
117 | | - client_read(["BigEnum"], AttrR(Int(), allowed_values=list(range(1, 18)))._value) |
118 | | - |
119 | | - def test_go(self, client_exec): |
120 | | - client_exec(["Go"]) |
121 | | - |
122 | | - def test_read_child1(self, client_read): |
123 | | - client_read(["SubController01", "ReadInt"], AttrR(Int())._value) |
124 | | - |
125 | | - def test_read_child2(self, client_read): |
126 | | - client_read(["SubController02", "ReadInt"], AttrR(Int())._value) |
| 50 | + def test_read_int(self, assertable_controller, client): |
| 51 | + expect = 0 |
| 52 | + path = ["readInt"] |
| 53 | + query = f"query {{ {nest_query(path)} }}" |
| 54 | + with assertable_controller.assert_read_here(["read_int"]): |
| 55 | + response = client.post("/graphql", json={"query": query}) |
| 56 | + assert response.status_code == 200 |
| 57 | + assert response.json()["data"] == nest_responce(path, expect) |
| 58 | + |
| 59 | + def test_read_write_int(self, assertable_controller, client): |
| 60 | + expect = 0 |
| 61 | + path = ["readWriteInt"] |
| 62 | + query = f"query {{ {nest_query(path)} }}" |
| 63 | + with assertable_controller.assert_read_here(["read_write_int"]): |
| 64 | + response = client.post("/graphql", json={"query": query}) |
| 65 | + assert response.status_code == 200 |
| 66 | + assert response.json()["data"] == nest_responce(path, expect) |
| 67 | + |
| 68 | + new = 9 |
| 69 | + mutation = f"mutation {{ {nest_mutation(path, new)} }}" |
| 70 | + with assertable_controller.assert_write_here(["read_write_int"]): |
| 71 | + response = client.post("/graphql", json={"query": mutation}) |
| 72 | + assert response.status_code == 200 |
| 73 | + assert response.json()["data"] == nest_responce(path, new) |
| 74 | + |
| 75 | + def test_read_write_float(self, assertable_controller, client): |
| 76 | + expect = 0 |
| 77 | + path = ["readWriteFloat"] |
| 78 | + query = f"query {{ {nest_query(path)} }}" |
| 79 | + with assertable_controller.assert_read_here(["read_write_float"]): |
| 80 | + response = client.post("/graphql", json={"query": query}) |
| 81 | + assert response.status_code == 200 |
| 82 | + assert response.json()["data"] == nest_responce(path, expect) |
| 83 | + |
| 84 | + new = 0.5 |
| 85 | + mutation = f"mutation {{ {nest_mutation(path, new)} }}" |
| 86 | + with assertable_controller.assert_write_here(["read_write_float"]): |
| 87 | + response = client.post("/graphql", json={"query": mutation}) |
| 88 | + assert response.status_code == 200 |
| 89 | + assert response.json()["data"] == nest_responce(path, new) |
| 90 | + |
| 91 | + def test_read_bool(self, assertable_controller, client): |
| 92 | + expect = False |
| 93 | + path = ["readBool"] |
| 94 | + query = f"query {{ {nest_query(path)} }}" |
| 95 | + with assertable_controller.assert_read_here(["read_bool"]): |
| 96 | + response = client.post("/graphql", json={"query": query}) |
| 97 | + assert response.status_code == 200 |
| 98 | + assert response.json()["data"] == nest_responce(path, expect) |
| 99 | + |
| 100 | + def test_write_bool(self, assertable_controller, client): |
| 101 | + value = True |
| 102 | + path = ["writeBool"] |
| 103 | + mutation = f"mutation {{ {nest_mutation(path, value)} }}" |
| 104 | + with assertable_controller.assert_write_here(["write_bool"]): |
| 105 | + response = client.post("/graphql", json={"query": mutation}) |
| 106 | + assert response.status_code == 200 |
| 107 | + assert response.json()["data"] == nest_responce(path, value) |
| 108 | + |
| 109 | + def test_string_enum(self, assertable_controller, client): |
| 110 | + expect = "" |
| 111 | + path = ["stringEnum"] |
| 112 | + query = f"query {{ {nest_query(path)} }}" |
| 113 | + with assertable_controller.assert_read_here(["string_enum"]): |
| 114 | + response = client.post("/graphql", json={"query": query}) |
| 115 | + assert response.status_code == 200 |
| 116 | + assert response.json()["data"] == nest_responce(path, expect) |
| 117 | + |
| 118 | + new = "new" |
| 119 | + mutation = f"mutation {{ {nest_mutation(path, new)} }}" |
| 120 | + with assertable_controller.assert_write_here(["string_enum"]): |
| 121 | + response = client.post("/graphql", json={"query": mutation}) |
| 122 | + assert response.status_code == 200 |
| 123 | + assert response.json()["data"] == nest_responce(path, new) |
| 124 | + |
| 125 | + def test_big_enum(self, assertable_controller, client): |
| 126 | + expect = 0 |
| 127 | + path = ["bigEnum"] |
| 128 | + query = f"query {{ {nest_query(path)} }}" |
| 129 | + with assertable_controller.assert_read_here(["big_enum"]): |
| 130 | + response = client.post("/graphql", json={"query": query}) |
| 131 | + assert response.status_code == 200 |
| 132 | + assert response.json()["data"] == nest_responce(path, expect) |
| 133 | + |
| 134 | + def test_go(self, assertable_controller, client): |
| 135 | + path = ["go"] |
| 136 | + mutation = f"mutation {{ {nest_query(path)} }}" |
| 137 | + with assertable_controller.assert_execute_here(path): |
| 138 | + response = client.post("/graphql", json={"query": mutation}) |
| 139 | + assert response.status_code == 200 |
| 140 | + assert response.json()["data"] == {path[-1]: True} |
| 141 | + |
| 142 | + def test_read_child1(self, assertable_controller, client): |
| 143 | + expect = 0 |
| 144 | + path = ["SubController01", "readInt"] |
| 145 | + query = f"query {{ {nest_query(path)} }}" |
| 146 | + with assertable_controller.assert_read_here(["SubController01", "read_int"]): |
| 147 | + response = client.post("/graphql", json={"query": query}) |
| 148 | + assert response.status_code == 200 |
| 149 | + assert response.json()["data"] == nest_responce(path, expect) |
| 150 | + |
| 151 | + def test_read_child2(self, assertable_controller, client): |
| 152 | + expect = 0 |
| 153 | + path = ["SubController02", "readInt"] |
| 154 | + query = f"query {{ {nest_query(path)} }}" |
| 155 | + with assertable_controller.assert_read_here(["SubController02", "read_int"]): |
| 156 | + response = client.post("/graphql", json={"query": query}) |
| 157 | + assert response.status_code == 200 |
| 158 | + assert response.json()["data"] == nest_responce(path, expect) |
0 commit comments