|
| 1 | +from collections import OrderedDict |
| 2 | +from graphql.core.type import GraphQLString, GraphQLInt, GraphQLID, GraphQLNonNull |
| 3 | +from epoxy.argument import Argument |
| 4 | +from epoxy.registry import TypeRegistry |
| 5 | + |
| 6 | +make_args = lambda R: { |
| 7 | + 'a': R.Int, |
| 8 | + 'b_cool': R.String, |
| 9 | + 'c': R.ID.NonNull, |
| 10 | + 'd': Argument(R.String, default_value="hello world"), |
| 11 | + 'z': R.String(default_value="hello again", description="This is a description"), |
| 12 | + 'x': R.Int(default_value=7), |
| 13 | + 'y': Argument(GraphQLString), |
| 14 | + 'q': R.TestInputType, |
| 15 | + 'w': Argument(R.TestInputType) |
| 16 | +} |
| 17 | + |
| 18 | +make_ordered_dict_args = lambda R: OrderedDict([ |
| 19 | + ('a', R.Int), |
| 20 | + ('b_cool', R.String), |
| 21 | + ('c', R.ID.NonNull), |
| 22 | + ('d', Argument(R.String, default_value="hello world")), |
| 23 | + ('z', R.String(default_value="hello again", description="This is a description")), |
| 24 | + ('x', R.Int(default_value=7)), |
| 25 | + ('y', Argument(GraphQLString)), |
| 26 | + ('q', R.TestInputType), |
| 27 | + ('w', Argument(R.TestInputType)), |
| 28 | +]) |
| 29 | + |
| 30 | + |
| 31 | +def check_args(test_input_type, args): |
| 32 | + expected_keys = ['a', 'bCool', 'c', 'd', 'z', 'x', 'y', 'q', 'w'] |
| 33 | + keys = [a.name for a in args] |
| 34 | + |
| 35 | + assert keys == expected_keys |
| 36 | + |
| 37 | + a, b, c, d, z, x, y, q, w = args |
| 38 | + |
| 39 | + assert a.type is GraphQLInt |
| 40 | + assert b.type is GraphQLString |
| 41 | + assert isinstance(c.type, GraphQLNonNull) |
| 42 | + assert c.type.of_type is GraphQLID |
| 43 | + assert d.type is GraphQLString |
| 44 | + assert d.default_value == "hello world" |
| 45 | + assert z.type is GraphQLString |
| 46 | + assert z.default_value == "hello again" |
| 47 | + assert z.description == "This is a description" |
| 48 | + assert x.type is GraphQLInt |
| 49 | + assert x.default_value == 7 |
| 50 | + assert y.type is GraphQLString |
| 51 | + assert q.type is test_input_type |
| 52 | + assert w.type is test_input_type |
| 53 | + |
| 54 | + |
| 55 | +def test_args_will_magically_order(): |
| 56 | + R = TypeRegistry() |
| 57 | + |
| 58 | + class TestInputType(R.InputType): |
| 59 | + a = R.Int |
| 60 | + b = R.Int |
| 61 | + |
| 62 | + class Query(R.ObjectType): |
| 63 | + int = R.Int( |
| 64 | + args=make_args(R) |
| 65 | + ) |
| 66 | + int_from_field = R.Field(R.Int, args=make_args(R)) |
| 67 | + |
| 68 | + query_type = R.Query() |
| 69 | + check_args(TestInputType.T, query_type.get_fields()['int'].args) |
| 70 | + check_args(TestInputType.T, query_type.get_fields()['intFromField'].args) |
| 71 | + |
| 72 | + |
| 73 | +def test_args_can_also_use_ordered_dict(): |
| 74 | + R = TypeRegistry() |
| 75 | + |
| 76 | + class TestInputType(R.InputType): |
| 77 | + a = R.Int |
| 78 | + b = R.Int |
| 79 | + |
| 80 | + class Query(R.ObjectType): |
| 81 | + int = R.Int( |
| 82 | + args=make_ordered_dict_args(R) |
| 83 | + ) |
| 84 | + int_from_field = R.Field(R.Int, args=make_ordered_dict_args(R)) |
| 85 | + |
| 86 | + query_type = R.Query() |
| 87 | + check_args(TestInputType.T, query_type.get_fields()['int'].args) |
| 88 | + check_args(TestInputType.T, query_type.get_fields()['intFromField'].args) |
0 commit comments