Skip to content

Commit 0366dde

Browse files
author
Rami Chowdhury
committed
Add more tests
1 parent 08d351d commit 0366dde

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

tests/test_converters.py

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,82 @@
1-
import graphene
2-
from pydantic import BaseModel, create_model
1+
import typing as T
2+
import uuid
3+
import datetime
34

5+
import pytest
6+
import graphene
7+
from pydantic import create_model
48

5-
from graphene_pydantic.types import PydanticObjectType
9+
# from graphene_pydantic.types import PydanticObjectType
610
from graphene_pydantic.converters import convert_pydantic_field
711
from graphene_pydantic.registry import get_global_registry
812

913

1014
def _get_field_from_spec(name, type_spec_or_default):
1115
kwargs = {name: type_spec_or_default}
12-
m = create_model('model', **kwargs)
16+
m = create_model("model", **kwargs)
1317
return m.__fields__[name]
1418

1519

16-
def test_convert_string():
17-
field = convert_pydantic_field(
18-
_get_field_from_spec('prop', (str, ...)),
19-
get_global_registry())
20+
def _convert_field_from_spec(name, type_spec_or_default):
21+
return convert_pydantic_field(
22+
_get_field_from_spec(name, type_spec_or_default), get_global_registry()
23+
)
24+
25+
26+
def test_required_string():
27+
field = _convert_field_from_spec("s", (str, ...))
2028
assert field is not None
2129
assert isinstance(field, graphene.Field)
22-
assert isinstance(field.type, graphene.NonNull) # ... means required
30+
# The ellipsis in the type spec means required
31+
assert isinstance(field.type, graphene.NonNull)
2332
assert field.type.of_type == graphene.String
33+
34+
35+
def test_default_values():
36+
field = _convert_field_from_spec("s", "hi")
37+
assert field is not None
38+
assert isinstance(field, graphene.Field)
39+
# there's a default value, so it's not required
40+
assert not isinstance(field.type, graphene.NonNull)
41+
assert field.type == graphene.String
42+
assert field.default_value == "hi"
43+
44+
45+
@pytest.mark.parametrize(
46+
"input, expected",
47+
[
48+
((bool, False), graphene.Boolean),
49+
((float, 0.1), graphene.Float),
50+
((int, 6), graphene.Int),
51+
((str, "hi"), graphene.String),
52+
((uuid.UUID, uuid.uuid4()), graphene.UUID),
53+
((datetime.date, datetime.date(2019, 1, 1)), graphene.Date),
54+
((datetime.datetime, datetime.datetime(2019, 1, 1, 1, 37)), graphene.DateTime),
55+
],
56+
)
57+
def test_builtin_scalars(input, expected):
58+
field = _convert_field_from_spec("attr", input)
59+
assert isinstance(field, graphene.Field)
60+
assert field.type == expected
61+
assert field.default_value == input[1]
62+
63+
64+
@pytest.mark.parametrize(
65+
"input, expected",
66+
[
67+
((T.List[int], [1, 2]), graphene.List),
68+
# ((a, b), graphene.Enum),
69+
# ((a, b), graphene.Dynamic),
70+
],
71+
)
72+
def test_builtin_composites(input, expected):
73+
field = _convert_field_from_spec("attr", input)
74+
assert isinstance(field, graphene.Field)
75+
assert isinstance(field.type, expected)
76+
assert field.default_value == input[1]
77+
78+
79+
def test_union():
80+
field = _convert_field_from_spec("attr", (T.Union[int, float, str], 5.0))
81+
assert issubclass(field.type, graphene.Union)
82+
assert field.default_value == 5.0

0 commit comments

Comments
 (0)