def mixed_lists_and_tuples_with_various_objects(): class TestClass: pass assert inspect([TestClass, (TestClass,), ValueError()]) == ( "[<class TestClass>, (<class TestClass>,), <exception ValueError>]" )
def custom_inspect(): class TestClass: @staticmethod def __inspect__(): return "<custom magic method inspect>" assert inspect(TestClass()) == "<custom magic method inspect>"
def custom_inspect_that_returns_a_list(): class TestClass: @staticmethod def __inspect__(): return [1, 2, 3] assert inspect(TestClass()) == "[1, 2, 3]"
def does_not_include_arguments_that_were_not_set(): schema = GraphQLSchema( GraphQLObjectType( "Type", { "field": GraphQLField( GraphQLString, args={ "a": GraphQLArgument(GraphQLBoolean), "b": GraphQLArgument(GraphQLBoolean), "c": GraphQLArgument(GraphQLBoolean), "d": GraphQLArgument(GraphQLInt), "e": GraphQLArgument(GraphQLInt), }, resolve=lambda _source, _info, **args: inspect(args), ) }, )) document = parse("{ field(a: true, c: false, e: 0) }") assert execute(schema, document) == ( { "field": "{'a': True, 'c': False, 'e': 0}" }, None, )
def inspect_numbers(): assert inspect(0) == "0" assert inspect(0.0) == "0.0" assert inspect(314) == "314" assert inspect(3.14) == "3.14" assert inspect(complex(1, 2)) == "(1+2j)" assert inspect(nan) == "nan" assert inspect(inf) == "inf" assert inspect(-inf) == "-inf"
def inspect_unknown_object(): class MetaClass(type): __name__ = None class TestClass(metaclass=MetaClass): pass assert inspect(TestClass()) == "<object>"
def resolve_to_euros(_root, _info, money): amount = money.amount currency = money.currency if not amount or currency == "EUR": return amount if currency == "DM": return amount * 0.5 raise ValueError("Cannot convert to euros: " + inspect(money))
def mixed_dicts_and_sets_with_various_objects(): class TestClass: pass assert inspect({TestClass: {ValueError()}, ValueError: {TestClass()}}) == ( "{<class TestClass>: {<exception ValueError>}," " <exception class ValueError>: {<TestClass instance>}}" )
def custom_inspect_that_uses_self(): class TestClass: str = "Hello World!" def __inspect__(self): return self.str assert inspect(TestClass()) == "Hello World!"
def parse_money_literal(value_node: ValueNode, variables=None) -> Money: money = value_from_ast_untyped(value_node, variables) if variables is not None and ( # variables are not set when checked with ValuesOfCorrectTypeRule not money or not is_finite(money.get("amount")) or not isinstance(money.get("currency"), str)): raise GraphQLError("Cannot parse literal money value: " + inspect(money)) return Money(**money)
def parse_datetime_literal( value_node: ValueNode, variables: Optional[Dict[str, Any]] = None) -> datetime: ast_value = value_from_ast_untyped(value_node, variables) if not isinstance(ast_value, str): raise GraphQLError("Cannot parse literal datetime value: " + inspect(ast_value)) return parse_datetime_value(ast_value)
def custom_inspect_that_returns_an_overly_large_string(): s = "foo" * 100 class TestClass: @staticmethod def __inspect__(): return s assert inspect(TestClass()) == s[:118] + "..." + s[-119:]
def number(): assert inspect(0) == "0" assert inspect(0.0) == "0.0" assert inspect(314) == "314" assert inspect(3.14) == "3.14" assert inspect(nan) == "nan" assert inspect(inf) == "inf" assert inspect(-inf) == "-inf"
def parse_money_value(input_value: Any) -> Money: """Using Money custom scalar from graphql-core tests except here the input value is supposed to be a dict instead of a Money object.""" """ if isinstance(input_value, Money): return input_value """ if isinstance(input_value, dict): amount = input_value.get("amount", None) currency = input_value.get("currency", None) if not is_finite(amount) or not isinstance(currency, str): raise GraphQLError("Cannot parse money value dict: " + inspect(input_value)) return Money(float(amount), currency) else: raise GraphQLError("Cannot parse money value: " + inspect(input_value))
def inspect_graphql_types(): assert inspect(GraphQLInt) == "Int" assert inspect(GraphQLString) == "String" assert inspect(GraphQLNonNull(GraphQLString)) == "String!" assert inspect(GraphQLList(GraphQLString)) == "[String]" test_object_type = GraphQLObjectType( "TestObjectType", {"test": GraphQLField(GraphQLString)}) assert inspect(test_object_type) == "TestObjectType" test_directive = GraphQLDirective("TestDirective", []) assert inspect(test_directive) == "@TestDirective"
def serialize_money(output_value: Any) -> Dict[str, Any]: if not isinstance(output_value, Money): raise GraphQLError("Cannot serialize money value: " + inspect(output_value)) return output_value._asdict()
def overly_large_int(): n = int("123" * 100) r = repr(n) assert inspect(n) == r[:118] + "..." + r[-119:] with increased_str_size(): assert inspect(n) == r
def inspect_bytes(): for b in b"", b"abc", b"foo\tbar \x7f\xff\0", b"'", b"'": assert inspect(b) == repr(b) a = bytearray(b) assert inspect(a) == repr(a)
def inspect_overly_large_dict(): s = dict(zip((chr(97 + i) for i in range(20)), range(20))) assert (inspect(s) == "{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4," " ..., 'q': 16, 'r': 17, 's': 18, 't': 19}") with increased_list_size(): assert inspect(s) == repr(s)
def inspect_string(): for s in "", "abc", "foo\tbar \u265e\0", "'", "'": assert inspect(s) == repr(s)
def inspect_sets(): assert inspect(set()) == "set()" assert inspect({"a"}) == "{'a'}" assert inspect({"a", 1}) in ("{'a', 1}", "{1, 'a'}") # sets are unordered
def inspect_none(): assert inspect(None) == "None"
def inspect_boolean(): assert inspect(True) == "True" assert inspect(False) == "False"
def inspect_invalid(): assert inspect(INVALID) == "<INVALID>"
def custom_inspect_that_is_recursive(): class TestClass: def __inspect__(self): return self assert inspect(TestClass()) == "<TestClass instance>"
def inspect_recursive_dict(): s = {} s[1] = s assert inspect(s) == "{1: {...}}"
def has_representation_with_start_and_end(): loc = Location(token1, token2, source) assert repr(loc) == "<Location 1:3>" assert inspect(loc) == repr(loc)
def overly_large_string(): s = "foo" * 100 r = repr(s) assert inspect(s) == r[:118] + "..." + r[-119:] with increased_str_size(): assert inspect(s) == r
def has_representation_with_line_and_column(): token = Token(TokenKind.NAME, 1, 2, 1, 2, value="test") assert repr(token) == "<Token Name 'test' 1:2>" assert inspect(token) == repr(token)
def mixed_dicts_and_sets(): assert inspect({"a": {"b"}}) == "{'a': {'b'}}" assert inspect({1: [], 2: (), 3: set()}) == "{1: [], 2: (), 3: set()}" assert inspect([(set(), ), { None: {()} }]) == "[(set(),), {None: set(...)}]"