예제 #1
0
def get_ast_value(value):
    if isinstance(value, ast.Node):
        return value
    if isinstance(value, six.string_types):
        return ast.StringValue(value=value)
    elif isinstance(value, bool):
        return ast.BooleanValue(value=value)
    elif isinstance(value, (float, decimal.Decimal)):
        return ast.FloatValue(value=value)
    elif isinstance(value, int):
        return ast.IntValue(value=value)
    return None
def test_converts_input_objects():
    value = OrderedDict([('foo', 3), ('bar', 'HELLO')])

    assert ast_from_value(value) == ast.ObjectValue(fields=[
        ast.ObjectField(name=ast.Name('foo'), value=ast.IntValue('3')),
        ast.ObjectField(name=ast.Name('bar'), value=ast.StringValue('HELLO'))
    ])

    input_obj = GraphQLInputObjectType(
        'MyInputObj', {
            'foo': GraphQLInputObjectField(GraphQLFloat),
            'bar': GraphQLInputObjectField(my_enum)
        })

    assert ast_from_value(value, input_obj) == ast.ObjectValue(fields=[
        ast.ObjectField(name=ast.Name('foo'), value=ast.FloatValue('3.0')),
        ast.ObjectField(name=ast.Name('bar'), value=ast.EnumValue('HELLO'))
    ])
예제 #3
0
def test_converts_input_objects():
    value = OrderedDict([("foo", 3), ("bar", "HELLO")])

    assert ast_from_value(value) == ast.ObjectValue(fields=[
        ast.ObjectField(name=ast.Name("foo"), value=ast.IntValue("3")),
        ast.ObjectField(name=ast.Name("bar"), value=ast.StringValue("HELLO")),
    ])

    input_obj = GraphQLInputObjectType(
        "MyInputObj",
        {
            "foo": GraphQLInputObjectField(GraphQLFloat),
            "bar": GraphQLInputObjectField(my_enum),
        },
    )

    assert ast_from_value(value, input_obj) == ast.ObjectValue(fields=[
        ast.ObjectField(name=ast.Name("foo"), value=ast.FloatValue("3.0")),
        ast.ObjectField(name=ast.Name("bar"), value=ast.EnumValue("HELLO")),
    ])
예제 #4
0
def value_node_from_pyvalue(val: object):
    if isinstance(val, str):
        val = val.replace('\\', '\\\\')
        value = eql_quote.quote_literal(val)
        return gql_ast.StringValue(value=value[1:-1])
    elif isinstance(val, bool):
        return gql_ast.BooleanValue(value=bool(val))
    elif isinstance(val, int):
        return gql_ast.IntValue(value=str(val))
    elif isinstance(val, float):
        return gql_ast.FloatValue(value=str(val))
    elif isinstance(val, list):
        return gql_ast.ListValue(
            values=[value_node_from_pyvalue(v) for v in val])
    elif isinstance(val, dict):
        return gql_ast.ObjectValue(fields=[
            gql_ast.ObjectField(name=n, value=value_node_from_pyvalue(v))
            for n, v in val.items()
        ])
    else:
        raise ValueError(f'unexpected constant type: {type(val)!r}')
예제 #5
0
    def test_geometry(self):
        geometry = geos.Point(1, 0)
        geometry_type = graphql_geojson.Geometry()
        serialized = geometry_type.serialize(geometry)

        self.assertEqual(geometry.geom_type, serialized['type'])
        self.assertSequenceEqual(geometry.coords, serialized['coordinates'])

        node = ast.FloatValue(.0)
        self.assertIsNone(geometry_type.parse_literal(node))

        # WKT
        node = ast.StringValue(str(geometry))
        wkt_parsed = geometry_type.parse_literal(node)
        self.assertEqual(wkt_parsed, geometry)

        # GeoJSON
        geojson_parsed = geometry_type.parse_value(serialized)
        self.assertEqual(geojson_parsed.geojson, geometry.geojson)

        # Hex
        hex_parsed = geometry_type.parse_value(geometry.hexewkb)
        self.assertEqual(hex_parsed.geojson, geometry.geojson)
예제 #6
0
def test_it_converts_numeric_values_to_float_asts():
    assert ast_from_value(123, GraphQLFloat) == ast.FloatValue("123.0")
    assert ast_from_value(123.0, GraphQLFloat) == ast.FloatValue("123.0")
    assert ast_from_value(123.5, GraphQLFloat) == ast.FloatValue("123.5")
    assert ast_from_value(1e4, GraphQLFloat) == ast.FloatValue("10000.0")
    assert ast_from_value(1e40, GraphQLFloat) == ast.FloatValue("1e+40")
예제 #7
0
def test_converts_numeric_values_to_asts():
    assert ast_from_value(123) == ast.IntValue("123")
    assert ast_from_value(123.0) == ast.IntValue("123")
    assert ast_from_value(123.5) == ast.FloatValue("123.5")
    assert ast_from_value(1e4) == ast.IntValue("10000")
    assert ast_from_value(1e40) == ast.FloatValue("1e+40")
def test_converts_numeric_values_to_asts():
    assert ast_from_value(123) == ast.IntValue('123')
    assert ast_from_value(123.0) == ast.IntValue('123')
    assert ast_from_value(123.5) == ast.FloatValue('123.5')
    assert ast_from_value(1e4) == ast.IntValue('10000')
    assert ast_from_value(1e40) == ast.FloatValue('1e+40')