chore: add tests

This commit is contained in:
Thomas Leonard 2022-09-15 14:53:21 +02:00
parent 9509841305
commit d1f6412bbb
4 changed files with 110 additions and 1 deletions

View File

@ -58,7 +58,7 @@ jobs:
if: ${{ matrix.python == '3.10' }} if: ${{ matrix.python == '3.10' }}
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
name: graphene-sqlalchemy-coverage name: graphene-coverage
path: coverage.xml path: coverage.xml
if-no-files-found: error if-no-files-found: error
- name: Upload coverage.xml to codecov - name: Upload coverage.xml to codecov

View File

@ -7,6 +7,7 @@ help:
install-dev: install-dev:
pip install -e ".[dev]" pip install -e ".[dev]"
.PHONY: test ## Run tests
test: test:
py.test graphene examples py.test graphene examples

View File

@ -216,3 +216,110 @@ class TestCustomGlobalID:
assert not result.errors assert not result.errors
assert result.data["user"]["id"] == self.user_list[1]["id"] assert result.data["user"]["id"] == self.user_list[1]["id"]
assert result.data["user"]["name"] == self.user_list[1]["name"] assert result.data["user"]["name"] == self.user_list[1]["name"]
class TestIncompleteCustomGlobalID:
def setup(self):
self.user_list = [
{"id": 1, "name": "First"},
{"id": 2, "name": "Second"},
{"id": 3, "name": "Third"},
{"id": 4, "name": "Fourth"},
]
self.users = {user["id"]: user for user in self.user_list}
def test_must_define_to_global_id(self):
"""
Test that if the `to_global_id` method is not defined, we can query the object, but we can't request its ID.
"""
class CustomGlobalIDType(BaseGlobalIDType):
graphene_type = Int
@classmethod
def resolve_global_id(cls, info, global_id):
_type = info.return_type.graphene_type._meta.name
return _type, global_id
class CustomNode(Node):
class Meta:
global_id_type = CustomGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
query = """query {
user(id: 2) {
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert not result.errors
assert result.data["user"]["name"] == self.user_list[1]["name"]
query = """query {
user(id: 2) {
id
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert result.errors is not None
assert len(result.errors) == 1
assert result.errors[0].path == ["user", "id"]
def test_must_define_resolve_global_id(self):
"""
Test that if the `resolve_global_id` method is not defined, we can't query the object by ID.
"""
class CustomGlobalIDType(BaseGlobalIDType):
graphene_type = Int
@classmethod
def to_global_id(cls, _type, _id):
return _id
class CustomNode(Node):
class Meta:
global_id_type = CustomGlobalIDType
class User(ObjectType):
class Meta:
interfaces = [CustomNode]
name = String()
@classmethod
def get_node(cls, _type, _id):
return self.users[_id]
class RootQuery(ObjectType):
user = CustomNode.Field(User)
self.schema = Schema(query=RootQuery, types=[User])
self.graphql_schema = self.schema.graphql_schema
query = """query {
user(id: 2) {
id
name
}
}"""
result = graphql_sync(self.graphql_schema, query)
assert result.errors is not None
assert len(result.errors) == 1
assert result.errors[0].path == ["user"]

View File

@ -55,6 +55,7 @@ def test_node_good():
assert "id" in MyNode._meta.fields assert "id" in MyNode._meta.fields
assert is_node(MyNode) assert is_node(MyNode)
assert not is_node(object) assert not is_node(object)
assert not is_node("node")
def test_node_query(): def test_node_query():