<s> from graphql . core . error import format_error <EOL> from graphql . core . language . parser import parse <EOL> from graphql . core . type import ( GraphQLArgument , GraphQLBoolean , <EOL> GraphQLEnumType , GraphQLEnumValue , GraphQLField , <EOL> GraphQLFloat , GraphQLID , <EOL> GraphQLInputObjectField , GraphQLInputObjectType , <EOL> GraphQLInt , GraphQLInterfaceType , GraphQLList , <EOL> GraphQLNonNull , GraphQLObjectType , <EOL> GraphQLSchema , GraphQLString , GraphQLUnionType ) <EOL> from graphql . core . type . directives import ( GraphQLDirective , <EOL> GraphQLIncludeDirective , <EOL> GraphQLSkipDirective ) <EOL> from graphql . core . validation import validate <EOL> Being = GraphQLInterfaceType ( '<STR_LIT>' , { <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) <EOL> } ) <EOL> Pet = GraphQLInterfaceType ( '<STR_LIT>' , { <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) , <EOL> } ) <EOL> Canine = GraphQLInterfaceType ( '<STR_LIT>' , { <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) , <EOL> } ) <EOL> DogCommand = GraphQLEnumType ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:2> ) , <EOL> } ) <EOL> Dog = GraphQLObjectType ( '<STR_LIT>' , { <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLInt ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLBoolean ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLBoolean , { <EOL> '<STR_LIT>' : GraphQLArgument ( DogCommand ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( <EOL> GraphQLBoolean , <EOL> args = { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean , default_value = True ) <EOL> } <EOL> ) , <EOL> '<STR_LIT>' : GraphQLField ( <EOL> GraphQLBoolean , <EOL> args = { <EOL> '<STR_LIT:x>' : GraphQLArgument ( GraphQLInt ) , <EOL> '<STR_LIT:y>' : GraphQLArgument ( GraphQLInt ) <EOL> } <EOL> ) <EOL> } , interfaces = [ Being , Pet , Canine ] , is_type_of = lambda : None ) <EOL> Cat = GraphQLObjectType ( '<STR_LIT>' , lambda : { <EOL> '<STR_LIT>' : GraphQLField ( FurColor ) , <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString ) , <EOL> } , interfaces = [ Being , Pet ] , is_type_of = lambda : None ) <EOL> CatOrDog = GraphQLUnionType ( '<STR_LIT>' , [ Dog , Cat ] ) <EOL> Intelligent = GraphQLInterfaceType ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : GraphQLField ( GraphQLInt ) , <EOL> } ) <EOL> Human = GraphQLObjectType ( <EOL> name = '<STR_LIT>' , <EOL> interfaces = [ Being , Intelligent ] , <EOL> is_type_of = lambda : None , <EOL> fields = { <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLList ( Pet ) ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLInt ) , <EOL> } , <EOL> ) <EOL> Alien = GraphQLObjectType ( <EOL> name = '<STR_LIT>' , <EOL> is_type_of = lambda * args : True , <EOL> interfaces = [ Being , Intelligent ] , <EOL> fields = { <EOL> '<STR_LIT>' : GraphQLField ( GraphQLInt ) , <EOL> '<STR_LIT:name>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) , <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLInt ) , <EOL> } , <EOL> ) <EOL> DogOrHuman = GraphQLUnionType ( '<STR_LIT>' , [ Dog , Human ] ) <EOL> HumanOrAlien = GraphQLUnionType ( '<STR_LIT>' , [ Human , Alien ] ) <EOL> FurColor = GraphQLEnumType ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : GraphQLEnumValue ( <NUM_LIT:3> ) , <EOL> } ) <EOL> ComplexInput = GraphQLInputObjectType ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : GraphQLInputObjectField ( GraphQLNonNull ( GraphQLBoolean ) ) , <EOL> '<STR_LIT>' : GraphQLInputObjectField ( GraphQLInt ) , <EOL> '<STR_LIT>' : GraphQLInputObjectField ( GraphQLString ) , <EOL> '<STR_LIT>' : GraphQLInputObjectField ( GraphQLBoolean ) , <EOL> '<STR_LIT>' : GraphQLInputObjectField ( GraphQLList ( GraphQLString ) ) , <EOL> } ) <EOL> ComplicatedArgs = GraphQLObjectType ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLInt ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLNonNull ( GraphQLInt ) ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLString ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLBoolean ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( FurColor ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLFloat ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLID ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLList ( GraphQLString ) ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( ComplexInput ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLNonNull ( GraphQLInt ) ) , <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLNonNull ( GraphQLInt ) ) , <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLInt , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLInt , <NUM_LIT:0> ) <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( GraphQLString , { <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLNonNull ( GraphQLInt ) ) , <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLNonNull ( GraphQLInt ) ) , <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLInt , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : GraphQLArgument ( GraphQLInt , <NUM_LIT:0> ) <EOL> } ) <EOL> } ) <EOL> QueryRoot = GraphQLObjectType ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : GraphQLField ( Human , { <EOL> '<STR_LIT:id>' : GraphQLArgument ( GraphQLID ) , <EOL> } ) , <EOL> '<STR_LIT>' : GraphQLField ( Dog ) , <EOL> '<STR_LIT>' : GraphQLField ( Pet ) , <EOL> '<STR_LIT>' : GraphQLField ( Alien ) , <EOL> '<STR_LIT>' : GraphQLField ( CatOrDog ) , <EOL> '<STR_LIT>' : GraphQLField ( HumanOrAlien ) , <EOL> '<STR_LIT>' : GraphQLField ( ComplicatedArgs ) , <EOL> } ) <EOL> test_schema = GraphQLSchema ( query = QueryRoot , directives = [ <EOL> GraphQLDirective ( name = '<STR_LIT>' , on_operation = True ) , <EOL> GraphQLIncludeDirective , <EOL> GraphQLSkipDirective <EOL> ] ) <EOL> def expect_valid ( schema , rules , query ) : <EOL> errors = validate ( schema , parse ( query ) , rules ) <EOL> assert errors == [ ] , '<STR_LIT>' <EOL> def sort_lists ( value ) : <EOL> if isinstance ( value , dict ) : <EOL> new_mapping = [ ] <EOL> for k , v in value . items ( ) : <EOL> new_mapping . append ( ( k , sort_lists ( v ) ) ) <EOL> return sorted ( new_mapping ) <EOL> elif isinstance ( value , list ) : <EOL> return sorted ( map ( sort_lists , value ) ) <EOL> return value <EOL> def expect_invalid ( schema , rules , query , expected_errors , sort_list = True ) : <EOL> errors = validate ( schema , parse ( query ) , rules ) <EOL> assert errors , '<STR_LIT>' <EOL> for error in expected_errors : <EOL> error [ '<STR_LIT>' ] = [ <EOL> { '<STR_LIT>' : loc . line , '<STR_LIT>' : loc . column } <EOL> for loc in error [ '<STR_LIT>' ] <EOL> ] <EOL> if sort_list : <EOL> assert sort_lists ( list ( map ( format_error , errors ) ) ) == sort_lists ( expected_errors ) <EOL> else : <EOL> assert list ( map ( format_error , errors ) ) == expected_errors <EOL> def expect_passes_rule ( rule , query ) : <EOL> return expect_valid ( test_schema , [ rule ] , query ) <EOL> def expect_fails_rule ( rule , query , errors , sort_list = True ) : <EOL> return expect_invalid ( test_schema , [ rule ] , query , errors , sort_list ) <EOL> def expect_fails_rule_with_schema ( schema , rule , query , errors , sort_list = True ) : <EOL> return expect_invalid ( schema , [ rule ] , query , errors , sort_list ) <EOL> def expect_passes_rule_with_schema ( schema , rule , query ) : <EOL> return expect_valid ( schema , [ rule ] , query ) </s>
