|
1 |
| -import graphene |
2 |
| -from pydantic import BaseModel, create_model |
| 1 | +import typing as T |
| 2 | +import uuid |
| 3 | +import datetime |
3 | 4 |
|
| 5 | +import pytest |
| 6 | +import graphene |
| 7 | +from pydantic import create_model |
4 | 8 |
|
5 |
| -from graphene_pydantic.types import PydanticObjectType |
| 9 | +# from graphene_pydantic.types import PydanticObjectType |
6 | 10 | from graphene_pydantic.converters import convert_pydantic_field
|
7 | 11 | from graphene_pydantic.registry import get_global_registry
|
8 | 12 |
|
9 | 13 |
|
10 | 14 | def _get_field_from_spec(name, type_spec_or_default):
|
11 | 15 | kwargs = {name: type_spec_or_default}
|
12 |
| - m = create_model('model', **kwargs) |
| 16 | + m = create_model("model", **kwargs) |
13 | 17 | return m.__fields__[name]
|
14 | 18 |
|
15 | 19 |
|
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, ...)) |
20 | 28 | assert field is not None
|
21 | 29 | 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) |
23 | 32 | 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