Skip to content

Commit

Permalink
Merge pull request #20 from lonelyteapot/add-tests
Browse files Browse the repository at this point in the history
Add tests for all implemented features
  • Loading branch information
lonelyteapot authored Jul 24, 2022
2 parents 3b48c15 + 8c880cc commit 951bf64
Show file tree
Hide file tree
Showing 14 changed files with 742 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics
- name: Run pytest
run: poetry run pytest
run: poetry run pytest --cov=pygraphic --cov-report=term-missing:skip-covered --cov-report=xml tests
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload coverage report
uses: codecov/codecov-action@v3.1.0
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
"python.analysis.typeCheckingMode": "basic",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": ["tests"],
"python.testing.pytestArgs": [
"tests",
"--cov=pygraphic",
"--cov-report=term-missing"
],
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
# pygraphic
<p>
<a href="https://pypi.org/project/pygraphic" target="_blank">
<img src="https://img.shields.io/pypi/status/pygraphic" alt="Status">
</a>
<a href="https://pypi.org/project/pygraphic" target="_blank">
<img src="https://img.shields.io/pypi/v/pygraphic" alt="Version">
</a>
<a href="https://github.com/lonelyteapot/pygraphic/actions/workflows/test.yml?query=branch%3Amain" target="_blank">
<img src="https://img.shields.io/github/workflow/status/lonelyteapot/pygraphic/Unit%20tests/main?label=tests" alt="Tests">
</a>
<a href="https://codecov.io/gh/lonelyteapot/pygraphic" target="_blank">
<img src="https://img.shields.io/codecov/c/github/lonelyteapot/pygraphic" alt="Coverage">
</a>
</p>

Client-side GraphQL query generator based on [pydantic].

Expand Down
78 changes: 77 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ isort = "^5.10.1"
pytest = "^7.1.2"
requests = "^2.28.1"
types-requests = "^2.28.3"
pytest-cov = "^3.0.0"

[tool.poetry-dynamic-versioning]
enable = true
Expand Down
53 changes: 53 additions & 0 deletions tests/test_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from pydantic import Field

from pygraphic import GQLQuery


def test_creation_of_query_with_field_arguments():
class Query(GQLQuery):
i: int = Field(ia=0)
f: float = Field(fa=0.0)
s: str = Field(sa="")
b: bool = Field(ba=False)

Query(i=0, f=0.0, s="", b=False)


def test_generation_of_query_with_one_field_argument():
class Query(GQLQuery):
i: int = Field(ia=0)
f: float = Field(fa=0.0)
s: str = Field(sa="foo")
b: bool = Field(ba=False)

gql = Query.get_query_string(include_name=False)
assert gql == "\n".join(
(
"query {",
" i(ia: 0)",
" f(fa: 0.0)",
' s(sa: "foo")',
" b(ba: false)",
"}",
)
)


def test_generation_of_query_with_two_field_arguments():
class Query(GQLQuery):
i: int = Field(ia=0, ib=1)
f: float = Field(fa=0.0, fb=1.1)
s: str = Field(sa="foo", sb="bar")
b: bool = Field(ba=False, bb=True)

gql = Query.get_query_string(include_name=False)
assert gql == "\n".join(
(
"query {",
" i(ia: 0, ib: 1)",
" f(fa: 0.0, fb: 1.1)",
' s(sa: "foo", sb: "bar")',
" b(ba: false, bb: true)",
"}",
)
)
55 changes: 55 additions & 0 deletions tests/test_case_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
from pydantic import Field

from pygraphic import GQLQuery, GQLVariables


@pytest.mark.skip("Unnamed queries with parameters are not yet implemented")
def test_field_name_conversion():
class Query(GQLQuery):
snake_case: bool

gql = Query.get_query_string(include_name=False)
assert gql == "query {\n snakeCase\n}"


@pytest.mark.skip("Unnamed queries with parameters are not yet implemented")
def test_field_argument_conversion():
class Query(GQLQuery):
i: int = Field(snake_case=False)

gql = Query.get_query_string(include_name=False)
assert gql == "query {\n i(snakeCase: false)\n}"


@pytest.mark.skip("Unnamed queries with parameters are not yet implemented")
def test_query_generation_variable_conversion():
class Variables(GQLVariables):
snake_case: bool

class Query(GQLQuery, variables=Variables):
pass

gql = Query.get_query_string(include_name=False)
assert gql == "query($snakeCase: Boolean!) {\n}"


def test_variable_generation_conversion():
class Variables(GQLVariables):
snake_case: bool

variables = Variables(snake_case=False)
json = variables.json()
assert json == '{"snakeCase": false}'


@pytest.mark.skip("Unnamed queries with parameters are not yet implemented")
def test_passing_variable_in_field_argument_conversion():
class Variables(GQLVariables):
snake_case: bool

class Query(GQLQuery, variables=Variables):
i: int = Field(camel_case=Variables.snake_case)

gql = Query.get_query_string(include_name=False)
assert gql == "query($snakeCase: Boolean!) {\n i(camelCase: $snakeCase)\n}"
80 changes: 80 additions & 0 deletions tests/test_custom_scalar_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from datetime import datetime
from uuid import UUID

import pytest
from pydantic import Field

from pygraphic import GQLQuery, GQLType, GQLVariables
from pygraphic.exceptions import QueryGenerationError
from pygraphic.types import register_graphql_type


def test_generation_of_query_with_custom_scalar_types():
class Query(GQLQuery):
uuid: UUID
datetime: datetime

gql = Query.get_query_string(include_name=False)
assert gql == "query {\n uuid\n datetime\n}"


def test_parsing_of_query_with_custom_scalar_types():
class Query(GQLQuery):
uuid: UUID
datetime: datetime

data = {
"uuid": "7a81e34d-ebe1-47eb-bae0-68ecc3ef174d",
"datetime": "2001-07-01 08:30:00",
}
result = Query.parse_obj(data)
assert result.uuid == UUID(hex="7a81e34d-ebe1-47eb-bae0-68ecc3ef174d")
assert result.datetime == datetime(year=2001, month=7, day=1, hour=8, minute=30)


def test_passing_custom_scalar_types_as_field_arguments():
uu = UUID(hex="7a81e34d-ebe1-47eb-bae0-68ecc3ef174d")
dt = datetime(year=2001, month=7, day=1, hour=8, minute=30)

class Query(GQLQuery):
foo: int = Field(uuid=uu)
bar: int = Field(datetime=dt)

gql = Query.get_query_string(include_name=False)
assert gql == "\n".join(
(
"query {",
' foo(uuid: "7a81e34d-ebe1-47eb-bae0-68ecc3ef174d")',
' bar(datetime: "2001-07-01 08:30:00")',
"}",
)
)


@pytest.mark.skip("Unnamed queries with parameters are not yet implemented")
def test_fail_of_using_custom_scalar_types_in_query_variables_without_registration():
class Variables(GQLVariables):
uuid: UUID
datetime: datetime

class Query(GQLQuery, variables=Variables):
pass

with pytest.raises(TypeError):
Query.get_query_string(include_name=False)


@pytest.mark.skip("Unnamed queries with parameters are not yet implemented")
def test_using_custom_scalar_types_in_query_variables():
class Variables(GQLVariables):
uuid: UUID
datetime: datetime

class Query(GQLQuery, variables=Variables):
pass

register_graphql_type("UUID", UUID)
register_graphql_type("DateTime", datetime)

gql = Query.get_query_string(include_name=False)
assert gql == "query($uuid: UUID!, $datetime: DateTime!) {\n}"
Loading

0 comments on commit 951bf64

Please sign in to comment.