Beispiel #1
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}')
Beispiel #2
0
def test_parses_multi_byte_characters():
    # type: () -> None
    result = parse(
        u"""
        # This comment has a \u0A0A multi-byte character.
        { field(arg: "Has a \u0A0A multi-byte character.") }
    """,
        no_location=True,
        no_source=True,
    )
    assert result == ast.Document(
        definitions=[
            ast.OperationDefinition(
                operation="query",
                name=None,
                variable_definitions=None,
                directives=[],
                selection_set=ast.SelectionSet(
                    selections=[
                        ast.Field(
                            alias=None,
                            name=ast.Name(value=u"field"),
                            arguments=[
                                ast.Argument(
                                    name=ast.Name(value=u"arg"),
                                    value=ast.StringValue(
                                        value=u"Has a \u0a0a multi-byte character."
                                    ),
                                )
                            ],
                            directives=[],
                            selection_set=None,
                        )
                    ]
                ),
            )
        ]
    )
Beispiel #3
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)
Beispiel #4
0
 def test_invalid_email_parse_literal_not_email(self):
     test_value = ast.StringValue(value='This Will Fail')
     with self.assertRaisesRegex(
             GraphQLError,
             scalar_error_type("email address", test_value.value)):
         EmailAddress.parse_literal(test_value)
Beispiel #5
0
 def test_valid_email_parse_literal(self):
     assert EmailAddress.parse_literal(
         ast.StringValue(value="*****@*****.**"))
Beispiel #6
0
 def test_valid_url_parse_literal(self):
     test_value = ast.StringValue(value='www.canada.ca')
     assert URL.parse_literal(test_value)
Beispiel #7
0
    def test_id_error(self):
        global_id_type = types.GlobalID()
        node = ast.StringValue('invalid')

        with self.assertRaises(exceptions.ValidationError):
            global_id_type.parse_literal(node)
def test_converts_array_values_to_list_asts():
    assert ast_from_value(["FOO", "BAR"]) == ast.ListValue(
        values=[ast.StringValue("FOO"),
                ast.StringValue("BAR")])
def test_converts_string_values_to_enum_asts_if_possible():
    assert ast_from_value("hello", my_enum) == ast.EnumValue("hello")
    assert ast_from_value("HELLO", my_enum) == ast.EnumValue("HELLO")
    assert ast_from_value("VAL\nUE", my_enum) == ast.StringValue("VAL\\nUE")
    assert ast_from_value("123", my_enum) == ast.StringValue("123")
def test_it_converts_string_values_to_asts():
    assert ast_from_value("hello") == ast.StringValue("hello")
    assert ast_from_value("VALUE") == ast.StringValue("VALUE")
    assert ast_from_value(u"VAL\nUE") == ast.StringValue("VAL\\nUE")
    assert ast_from_value("VAL\nUE") == ast.StringValue("VAL\\nUE")
    assert ast_from_value("123") == ast.StringValue("123")
def test_converts_array_values_to_list_asts():
    assert ast_from_value(['FOO', 'BAR']) == ast.ListValue(
        values=[ast.StringValue('FOO'),
                ast.StringValue('BAR')])
def test_converts_string_values_to_enum_asts_if_possible():
    assert ast_from_value('hello', my_enum) == ast.EnumValue('hello')
    assert ast_from_value('HELLO', my_enum) == ast.EnumValue('HELLO')
    assert ast_from_value('VAL\nUE', my_enum) == ast.StringValue('VAL\\nUE')
    assert ast_from_value('123', my_enum) == ast.StringValue('123')
def test_it_converts_string_values_to_asts():
    assert ast_from_value('hello') == ast.StringValue('hello')
    assert ast_from_value('VALUE') == ast.StringValue('VALUE')
    assert ast_from_value(u'VAL\nUE') == ast.StringValue('VAL\\nUE')
    assert ast_from_value('VAL\nUE') == ast.StringValue('VAL\\nUE')
    assert ast_from_value('123') == ast.StringValue('123')