Beispiel #1
0
def test_inputvalue__get_name():
    inputvalue_object = InputValueObject(
        None,
        ('argname', String()),
        None,
    )
    assert inputvalue_object.get_name() == 'argname'
Beispiel #2
0
def test_inputvalue__get_type():
    inputvalue_object = InputValue(
        None,
        ('name', String()),
        None,
    )
    assert inputvalue_object.get_type() == String
    inputvalue_object = InputValue(
        None,
        ('names', List(String)),
        None,
    )
    assert inputvalue_object.get_type() == List(String)
    inputvalue_object = InputValue(
        None,
        ('name', String(null=False)),
        None,
    )
    assert inputvalue_object.get_type() == NonNull(String())
def test_string_coerce_input():
    assert String.coerce_input(None) is None
    assert String.coerce_input('abc') == 'abc'
    assert String.coerce_input(u'äbc🐍') == u'äbc🐍'

    with pytest.raises(ValueError):
        String.coerce_input(True)
    with pytest.raises(ValueError):
        String.coerce_input(4.9)
Beispiel #4
0
def test_inputvalue__get_type():
    inputvalue_object = InputValueObject(
        None,
        ('name', String()),
        None,
    )
    assert inputvalue_object.get_type() == String
    inputvalue_object = InputValueObject(
        None,
        ('names', List(String)),
        None,
    )
    assert inputvalue_object.get_type() == List(String)
Beispiel #5
0
    def register_query_root(self, BaseQueryRoot):
        class QueryRoot(BaseQueryRoot):
            def get___schema(self):
                return self.__class__

            def get___type(self, name):
                schema = SchemaObject(None, self.__class__, None)
                for type_ in schema.get_types():
                    if type_.object_name == name:
                        return type_
                return None

        QueryRoot._declared_fields = copy.deepcopy(
            BaseQueryRoot._declared_fields)
        QueryRoot._declared_fields['__schema'] = RelatedField(SchemaObject)
        QueryRoot._declared_fields['__type'] = RelatedField(TypeObject,
                                                            name=String())

        self.query_root = QueryRoot
        return QueryRoot
Beispiel #6
0
class Episode(Object):
    name = CharField(description='The name of an episode')
    number = IntegerField()
    characters = ManyRelatedField(
        'test_app.schema.Character',
        arguments={
            'types': List(String(null=False)),
        },
    )
    next = RelatedField('self')

    def get_next(self):
        return EpisodeModel.objects.filter(number=self.data.number + 1).first()

    def get_characters(self, types):
        q = Q()
        if types is not None:
            if 'human' not in types:
                q &= Q(human=None)
            if 'droid' not in types:
                q &= Q(droid=None)
        return self.data.characters.filter(q).order_by('pk')
class IntrospectionQueryRoot(Object):
    """
    Provides basic functionality related to introspection.
    The query root for a graphql view should be a subclass of BaseQueryRoot.
    """
    introspection_fields = OrderedDict((
        ('__schema', RelatedField(introspection.Schema)),
        ('__type',
         RelatedField(
             introspection.Type,
             arguments={'name': String()},
         )),
    ))

    def get___schema(self):
        return self.data

    def get___type(self, name):
        schema = introspection.Schema(None, self.data, None)
        for type_ in schema.get_types():
            if type_.object_name == name:
                return type_
        return None
def test_string_coerce_result():
    assert String.coerce_result(True) == 'True'
    assert String.coerce_result(4.9) == '4.9'
    assert String.coerce_result(None) is None