Skip to content

Commit

Permalink
Apply upstream feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls committed Nov 21, 2024
1 parent e7bfc55 commit 31f82e6
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 79 deletions.
4 changes: 4 additions & 0 deletions src/fastcs/backends/graphQL/graphQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def _add_dev_attributes(

if node is not None:
for attr_name, attribute in single_mapping.attributes.items():
# camelCase is convension for field names
attr_name = attr_name.title().replace("_", "")
attr_name = attr_name[0].lower() + attr_name[1:]

match attribute:
# mutation for server changes https://graphql.org/learn/queries/
Expand Down Expand Up @@ -214,7 +216,9 @@ def _add_dev_commands(

if node is not None:
for name, method in single_mapping.command_methods.items():
# camelCase is convension for field names
cmd_name = name.title().replace("_", "")
cmd_name = name[0].lower() + name[1:]
node.fields_dict["mutation"].append(
strawberry.mutation(
_wrap_command(
Expand Down
190 changes: 111 additions & 79 deletions tests/backends/graphQL/test_graphQL.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import copy
import json
import re
from typing import Any

import pytest
from fastapi.testclient import TestClient

from fastcs.attributes import AttrR
from fastcs.backends.graphQL.backend import GraphQLBackend
from fastcs.datatypes import Bool, Float, Int


def pascal_2_snake(input: list[str]) -> list[str]:
snake_list = copy.deepcopy(input)
snake_list[-1] = re.sub(r"(?<!^)(?=[A-Z])", "_", snake_list[-1]).lower()
return snake_list


def nest_query(path: list[str]) -> str:
Expand Down Expand Up @@ -51,76 +42,117 @@ def nest_responce(path: list[str], value: Any) -> dict:


class TestGraphQLServer:
@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
def client(self, assertable_controller):
app = GraphQLBackend(assertable_controller)._server._app
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, client_read, client_write):

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

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

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

def test_read_child2(self, client_read):
client_read(["SubController02", "ReadInt"], AttrR(Int())._value)
def test_read_int(self, assertable_controller, client):
expect = 0
path = ["readInt"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["read_int"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

def test_read_write_int(self, assertable_controller, client):
expect = 0
path = ["readWriteInt"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["read_write_int"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

new = 9
mutation = f"mutation {{ {nest_mutation(path, new)} }}"
with assertable_controller.assert_write_here(["read_write_int"]):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, new)

def test_read_write_float(self, assertable_controller, client):
expect = 0
path = ["readWriteFloat"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["read_write_float"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

new = 0.5
mutation = f"mutation {{ {nest_mutation(path, new)} }}"
with assertable_controller.assert_write_here(["read_write_float"]):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, new)

def test_read_bool(self, assertable_controller, client):
expect = False
path = ["readBool"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["read_bool"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

def test_write_bool(self, assertable_controller, client):
value = True
path = ["writeBool"]
mutation = f"mutation {{ {nest_mutation(path, value)} }}"
with assertable_controller.assert_write_here(["write_bool"]):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, value)

def test_string_enum(self, assertable_controller, client):
expect = ""
path = ["stringEnum"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["string_enum"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

new = "new"
mutation = f"mutation {{ {nest_mutation(path, new)} }}"
with assertable_controller.assert_write_here(["string_enum"]):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, new)

def test_big_enum(self, assertable_controller, client):
expect = 0
path = ["bigEnum"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["big_enum"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

def test_go(self, assertable_controller, client):
path = ["go"]
mutation = f"mutation {{ {nest_query(path)} }}"
with assertable_controller.assert_execute_here(path):
response = client.post("/graphql", json={"query": mutation})
assert response.status_code == 200
assert response.json()["data"] == {path[-1]: True}

def test_read_child1(self, assertable_controller, client):
expect = 0
path = ["SubController01", "readInt"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["SubController01", "read_int"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

def test_read_child2(self, assertable_controller, client):
expect = 0
path = ["SubController02", "readInt"]
query = f"query {{ {nest_query(path)} }}"
with assertable_controller.assert_read_here(["SubController02", "read_int"]):
response = client.post("/graphql", json={"query": query})
assert response.status_code == 200
assert response.json()["data"] == nest_responce(path, expect)

0 comments on commit 31f82e6

Please sign in to comment.