-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from lonelyteapot/add-tests
Add tests for all implemented features
- Loading branch information
Showing
14 changed files
with
742 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)", | ||
"}", | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
Oops, something went wrong.