Skip to content

Commit

Permalink
Make more pytest friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls committed Nov 12, 2024
1 parent 7f5ced2 commit ee08dd5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 deletions.
117 changes: 65 additions & 52 deletions tests/backends/graphQL/test_graphQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,63 +51,76 @@ def nest_responce(path: list[str], value: Any) -> dict:


class TestGraphQLServer:
@pytest.fixture(autouse=True)
def setup_tests(self, assertable_controller):
@pytest.fixture(scope="class", autouse=True)
def setup_class(self, assertable_controller):
self.controller = assertable_controller

@pytest.fixture(scope="class")
def client(self):
app = GraphQLBackend(self.controller)._server._app
self.client = TestClient(app)

def client_read(self, path: list[str], expected: Any):
query = f"query {{ {nest_query(path)} }}"
with self.controller.assertPerformed(pascal_2_snake(path), "READ"):
response = self.client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expected)

def client_write(self, path: list[str], value: Any):
mutation = f"mutation {{ {nest_mutation(path, value)} }}"
with self.controller.assertPerformed(pascal_2_snake(path), "WRITE"):
response = self.client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, value)

def client_exec(self, path: list[str]):
mutation = f"mutation {{ {nest_query(path)} }}"
with self.controller.assertPerformed(pascal_2_snake(path), "EXECUTE"):
response = self.client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == {path[-1]: True}

def test_read_int(self):
self.client_read(["ReadInt"], AttrR(Int())._value)

def test_read_write_int(self):
self.client_read(["ReadWriteInt"], AttrR(Int())._value)
self.client_write(["ReadWriteInt"], AttrR(Int())._value)

def test_read_write_float(self):
self.client_read(["ReadWriteFloat"], AttrR(Float())._value)
self.client_write(["ReadWriteFloat"], AttrR(Float())._value)

def test_read_bool(self):
self.client_read(["ReadBool"], AttrR(Bool())._value)

def test_write_bool(self):
self.client_write(["WriteBool"], AttrR(Bool())._value)
return TestClient(app)

@pytest.fixture(scope="class")
def client_read(self, client):
def _client_read(path: list[str], expected: Any):
query = f"query {{ {nest_query(path)} }}"
with self.controller.assertPerformed(pascal_2_snake(path), "READ"):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expected)

return _client_read

@pytest.fixture(scope="class")
def client_write(self, client):
def _client_write(path: list[str], value: Any):
mutation = f"mutation {{ {nest_mutation(path, value)} }}"
with self.controller.assertPerformed(pascal_2_snake(path), "WRITE"):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, value)

return _client_write

@pytest.fixture(scope="class")
def client_exec(self, client):
def _client_exec(path: list[str]):
mutation = f"mutation {{ {nest_query(path)} }}"
with self.controller.assertPerformed(pascal_2_snake(path), "EXECUTE"):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == {path[-1]: True}

return _client_exec

def test_read_int(self, client_read):
client_read(["ReadInt"], AttrR(Int())._value)

def test_read_write_int(self, client_read, client_write):
client_read(["ReadWriteInt"], AttrR(Int())._value)
client_write(["ReadWriteInt"], AttrR(Int())._value)

def test_read_write_float(self, client_read, client_write):
client_read(["ReadWriteFloat"], AttrR(Float())._value)
client_write(["ReadWriteFloat"], AttrR(Float())._value)

def test_read_bool(self, client_read):
client_read(["ReadBool"], AttrR(Bool())._value)

def test_write_bool(self, client_write):
client_write(["WriteBool"], AttrR(Bool())._value)

# # We need to discuss enums
# def test_string_enum(self):
# def test_string_enum(self, client_read, client_write):

def test_big_enum(self):
self.client_read(
["BigEnum"], AttrR(Int(), allowed_values=list(range(1, 18)))._value
)
def test_big_enum(self, client_read):
client_read(["BigEnum"], AttrR(Int(), allowed_values=list(range(1, 18)))._value)

def test_go(self):
self.client_exec(["Go"])
def test_go(self, client_exec):
client_exec(["Go"])

def test_read_child1(self):
self.client_read(["SubController01", "ReadInt"], AttrR(Int())._value)
def test_read_child1(self, client_read):
client_read(["SubController01", "ReadInt"], AttrR(Int())._value)

def test_read_child2(self):
self.client_read(["SubController02", "ReadInt"], AttrR(Int())._value)
def test_read_child2(self, client_read):
client_read(["SubController02", "ReadInt"], AttrR(Int())._value)
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ def controller():
return TestController()


@pytest.fixture
def assertable_controller(mocker):
return AssertableController(mocker)
@pytest.fixture(scope="class")
def assertable_controller(class_mocker: MockerFixture):
return AssertableController(class_mocker)


@pytest.fixture
Expand Down

0 comments on commit ee08dd5

Please sign in to comment.