def test_schema_added_type(): old_schema = schema(""" schema { query: Query } type Query { field: String! } """) new_schema = schema(""" schema { query: Query } type Query { field: String! } type AddedType { added: Int } """) diff = Schema(old_schema, new_schema).diff() assert diff and len(diff) == 1 # Type Int was also added but its ignored because its a primitive. assert diff[0].message == "Type `AddedType` was added" assert diff[0].criticality == Criticality.safe()
def test_schema_removed_type(): old_schema = schema(""" schema { query: Query } type Query { field: String! } type ToBeRemovedType { added: Int } """) new_schema = schema(""" schema { query: Query } type Query { field: String! } """) diff = Schema(old_schema, new_schema).diff() assert diff and len(diff) == 1 # Type Int was also removed but it is ignored because it's a primitive. assert diff[0].message == "Type `ToBeRemovedType` was removed" assert diff[0].criticality == Criticality.breaking( 'Removing a type is a breaking change. It is preferred to ' 'deprecate and remove all references to this type first.')
def test_schema_query_root_changed(): old_schema = schema(""" schema { query: Query } type Query { field: String! } """) new_schema = schema(""" schema { query: ChangedQuery } type ChangedQuery { field: String! } """) diff = Schema(old_schema, new_schema).diff() print(diff) assert diff and len(diff) == 3 assert {x.message for x in diff} == { 'Type `ChangedQuery` was added', 'Type `Query` was removed', 'Schema query root has changed from `Query` to `ChangedQuery`' }
def test_schema_mutation_root_changed(): old_schema = schema(""" schema { query: Query } type Query { field: String! } """) new_schema = schema(""" schema { query: Query mutation: Mutation } type Query { field: String! } type Mutation { my_mutation: Int } """) diff = Schema(old_schema, new_schema).diff() print(diff) assert diff and len(diff) == 2 assert {x.message for x in diff} == { 'Type `Mutation` was added', 'Schema mutation root has changed from `None` to `Mutation`' }
def test_directive_argument_changes(): name_arg = schema(""" directive @somedir(name: String) on FIELD_DEFINITION type A { a: String } """) id_arg = schema(""" directive @somedir(id: ID) on FIELD_DEFINITION type A { a: String } """) diff = Schema(name_arg, id_arg).diff() assert diff and len(diff) == 2 expected_message = ( 'Removed argument `name: String` from `@somedir` directive' "Added argument `id: ID` to `@somedir` directive" ) for change in diff: assert change.message in expected_message assert change.path == '@somedir' assert change.criticality == Criticality.breaking( 'Removing a directive argument will break existing usages of the argument' ) if 'Removed' in change.message else Criticality.safe()
def test_object_type_added_field(): a = schema(""" type MyType{ a: Int } """) b = schema(""" type MyType{ a: Int b: String! } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[0].message == "Field `b` was added to object type `MyType`" assert diff[0].path == 'MyType.b' assert diff[0].criticality == Criticality.safe() diff = Schema(b, a).diff() assert diff and len(diff) == 1 assert diff[0].message == "Field `b` was removed from object type `MyType`" assert diff[0].path == 'MyType.b' assert diff[0].criticality == Criticality.breaking( 'Removing a field is a breaking change. It is preferred to deprecate the field before removing it.' )
def test_enums_added(): a = schema(""" type Query { a: String } enum Letters { A B } """) b = schema(""" type Query { a: String } enum Letters { A B C D } """) diff = Schema(a, b).diff() assert len(diff) == 2 expected_diff = { "Enum value `C` was added to `Letters` enum", "Enum value `D` was added to `Letters` enum", } expected_paths = {'Letters.C', 'Letters.D'} for change in diff: assert change.message in expected_diff assert change.path in expected_paths assert change.criticality == Criticality.dangerous( "Adding an enum value may break existing clients that " "were not programming defensively against an added case when querying an enum." )
def test_enum_value_removed(): a = schema(""" type Query { a: String } enum Letters { A B } """) b = schema(""" type Query { a: String } enum Letters { A } """) diff = Schema(a, b).diff() assert len(diff) == 1 change = diff[0] assert change.message == "Enum value `B` was removed from `Letters` enum" assert change.path == 'Letters' assert change.criticality == Criticality.breaking( 'Removing an enum value will break existing queries that use this enum value' )
def test_deprecated_reason_changed(): a = schema(""" type Query { a: Int } enum Letters { A B @deprecated(reason: "a reason") } """) b = schema(""" type Query { a: Int } enum Letters { A B @deprecated(reason: "a new reason") } """) diff = Schema(a, b).diff() assert len(diff) == 1 assert diff[0].message == ( "Deprecation reason for enum value `B` changed from `a reason` to `a new reason`" ) assert diff[0].path == 'Letters.B' assert diff[0].criticality == Criticality.safe( "A deprecated field can still be used by clients and will give them time to adapt their queries" )
def test_description_changed(): a = schema(''' type Query { a: Int } enum Letters { """My description""" A } ''') b = schema(''' type Query { a: Int } enum Letters { """My new description""" A } ''') diff = Schema(a, b).diff() assert len(diff) == 1 assert diff[0].message == ( "Description for enum value `A` changed from `My description` to `My new description`" ) assert diff[0].path == 'Letters.A' assert diff[0].criticality == Criticality.safe()
def test_added_removed_directive(): no_directive = schema(""" type A { a: String } """) one_directive = schema(""" directive @somedir on FIELD_DEFINITION type A { a: String } """) diff = Schema(no_directive, one_directive).diff() assert diff and len(diff) == 1 assert diff[0].message == "Directive `@somedir` was added to use on `FIELD_DEFINITION`" assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.safe() diff = Schema(one_directive, no_directive).diff() assert diff and len(diff) == 1 assert diff[0].message == "Directive `@somedir` was removed" assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.breaking('Removing a directive may break clients that depend on them.') two_locations = schema(""" directive @somedir on FIELD_DEFINITION | QUERY type A { a: String } """) diff = Schema(no_directive, two_locations).diff() assert diff and len(diff) == 1 assert diff[0].message == "Directive `@somedir` was added to use on `FIELD_DEFINITION | QUERY`" assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.safe()
def test_directive_location_added_and_removed(): one_location = schema(""" directive @somedir on FIELD_DEFINITION type A { a: String } """) two_locations = schema(""" directive @somedir on FIELD_DEFINITION | FIELD type A { a: String } """) diff = Schema(one_location, two_locations).diff() assert diff and len(diff) == 1 expected_message = ( "Directive locations of `@somedir` changed from `FIELD_DEFINITION` to `FIELD_DEFINITION | FIELD`" ) assert diff[0].message == expected_message assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.safe() diff = Schema(two_locations, one_location).diff() assert diff and len(diff) == 1 expected_message = ( "Directive locations of `@somedir` changed from `FIELD_DEFINITION | FIELD` to `FIELD_DEFINITION`" ) assert diff[0].message == expected_message assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.breaking( 'Removing a directive location will break any instance of its usage. Be sure no one uses it before removing it' )
def test_interface_field_added_and_removed(): a = schema(""" interface Person { name: String age: Int } """) b = schema(""" interface Person { name: String age: Int favorite_number: Float } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "Field `favorite_number` of type `Float` was added to interface `Person`" assert diff[0].path == 'Person.favorite_number' assert diff[0].criticality == Criticality.dangerous( 'Adding an interface to an object type may break existing clients ' 'that were not programming defensively against a new possible type.') diff = Schema(b, a).diff() assert diff[ 0].message == "Field `favorite_number` was removed from interface `Person`" assert diff[0].path == 'Person.favorite_number' assert diff[0].criticality == Criticality.dangerous( 'Removing an interface field can break existing queries that use this in a fragment spread.' )
def test_input_field_added_field(): a = schema(""" input Recipe { ingredients: [String] } """) b = schema(""" input Recipe { ingredients: [String] love: Float } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[0].message == ( "Input Field `love: Float` was added to input type `Recipe`") assert diff[0].path == 'Recipe.love' assert diff[0].criticality == Criticality.safe() diff = Schema(b, a).diff() assert diff and len(diff) == 1 assert diff[0].message == ( "Input Field `love` removed from input type `Recipe`") assert diff[0].path == 'Recipe.love' assert diff[0].criticality == Criticality.breaking( 'Removing an input field will break queries that use this input field.' )
def test_deprecated_enum_value(): a = schema(""" type Query { a: Int } enum Letters { A B } """) b = schema(""" type Query { a: Int } enum Letters { A B @deprecated(reason: "Changed the alphabet") } """) diff = Schema(a, b).diff() assert len(diff) == 1 assert diff[ 0].message == "Enum value `B` was deprecated with reason `Changed the alphabet`" assert diff[0].path == 'Letters.B' assert diff[0].criticality == Criticality.safe( "A deprecated field can still be used by clients and will give them time to adapt their queries" )
def test_directive_default_value_changed(): default_100 = schema(""" directive @limit(number: Int=100) on FIELD_DEFINITION type A { a: String } """) default_0 = schema(""" directive @limit(number: Int=0) on FIELD_DEFINITION type A { a: String } """) diff = Schema(default_100, default_0).diff() assert diff and len(diff) == 1 expected_message = ( 'Default value for argument `number` on `@limit` directive changed from `100` to `0`' ) assert diff[0].message == expected_message assert diff[0].path == '@limit' assert diff[0].criticality == Criticality.dangerous( 'Changing the default value for an argument may change ' 'the runtime behaviour of a field if it was never provided.' )
def test_schema_query_fields_type_has_changes(): old_schema = schema(""" schema { query: Query } type Query { field: String! } """) new_schema = schema(""" schema { query: Query } type Query { field: Int! } """) diff = Schema(old_schema, new_schema).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "`Query.field` type changed from `String!` to `Int!`" assert diff[0].criticality == Criticality.breaking( 'Changing a field type will break queries that assume its type')
def test_directive_argument_description_changed(): no_desc = schema(""" directive @limit( number: Int ) on FIELD_DEFINITION type A { a: String } """) a_desc = schema(""" directive @limit( "number limit" number: Int ) on FIELD_DEFINITION type A { a: String } """) other_desc = schema(""" directive @limit( "field limit" number: Int ) on FIELD_DEFINITION type A { a: String } """) diff = Schema(no_desc, a_desc).diff() assert diff and len(diff) == 1 expected_message = ( "Description for argument `number` on `@limit` directive changed from `None` to `number limit`" ) assert diff[0].message == expected_message assert diff[0].path == '@limit' assert diff[0].criticality == Criticality.safe() diff = Schema(a_desc, other_desc).diff() assert diff and len(diff) == 1 expected_message = ( "Description for argument `number` on `@limit` directive changed from `number limit` to `field limit`" ) assert diff[0].message == expected_message assert diff[0].path == '@limit' assert diff[0].criticality == Criticality.safe() diff = Schema(other_desc, no_desc).diff() assert diff and len(diff) == 1 expected_message = ( "Description for argument `number` on `@limit` directive changed from `field limit` to `None`" ) assert diff[0].message == expected_message assert diff[0].path == '@limit' assert diff[0].criticality == Criticality.safe()
def test_add_type_to_union(): two_types = schema(""" type Query { c: Int } type Result { message: String } type Error { message: String details: String } type Unknown { message: String details: String traceback: String } union Outcome = Result | Error """) three_types = schema(""" type Query { c: Int } type Result { message: String } type Error { message: String details: String } type Unknown { message: String details: String traceback: String } union Outcome = Result | Error | Unknown """) diff = Schema(two_types, three_types).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "Union member `Unknown` was added to `Outcome` Union type" assert diff[0].path == 'Outcome' assert diff[0].criticality == Criticality.dangerous( 'Adding a possible type to Unions may break existing clients ' 'that were not programming defensively against a new possible type.') diff = Schema(three_types, two_types).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "Union member `Unknown` was removed from `Outcome` Union type" assert diff[0].path == 'Outcome' assert diff[0].criticality == Criticality.breaking( 'Removing a union member from a union can break queries that use this union member in a fragment spread' )
def test_no_field_diff(): a = schema(""" type Query { b: Int! } """) b = schema(""" type Query { b: Int! } """) diff = Schema(a, b).diff() assert not diff
def test_enum_value_removing_desc(): a = schema(''' enum Letters { """WithDesc""" A } ''') b = schema(''' enum Letters { A } ''') diff = Schema(a, b).diff() assert RemoveEnumValueDescription(diff[0]).is_valid() is False
def test_interface_no_changes(): a = schema(""" interface Person { name: String age: Int } """) b = schema(""" interface Person { age: Int name: String } """) diff = Schema(a, b).diff() assert not diff
def test_input_field_no_diff(): a = schema(""" input Params { will: String! love: Int! } """) b = schema(""" input Params { will: String! love: Int! } """) diff = Schema(a, b).diff() assert not diff
def test_input_field_now_is_not_nullable(): a = schema(""" input Params { arg: ID } """) b = schema(""" input Params { arg: ID! } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[0].message == "`Params.arg` type changed from `ID` to `ID!`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.breaking(ERROR)
def test_type_added_without_desc(): a = schema(''' type MyType{ a: Int } ''') b = schema(''' type MyType{ a: Int } type NewType{ b: String! } ''') diff = Schema(a, b).diff() assert AddTypeWithoutDescription(diff[0]).is_valid() is False
def test_directive_description_changed(): no_desc = schema(""" directive @my_directive on FIELD type A { a: String } """) with_desc = schema(''' """directive desc""" directive @my_directive on FIELD type A { a: String } ''') new_desc = schema(''' """new description""" directive @my_directive on FIELD type A { a: String } ''') diff = Schema(no_desc, with_desc).diff() assert diff and len(diff) == 1 expected_message = ( 'Description for directive `@my_directive` changed from `None` to `directive desc`' ) assert diff[0].message == expected_message assert diff[0].path == '@my_directive' assert diff[0].criticality == Criticality.safe() diff = Schema(with_desc, new_desc).diff() assert diff and len(diff) == 1 expected_message = ( 'Description for directive `@my_directive` changed from `directive desc` to `new description`' ) assert diff[0].message == expected_message assert diff[0].path == '@my_directive' assert diff[0].criticality == Criticality.safe() diff = Schema(with_desc, no_desc).diff() assert diff and len(diff) == 1 expected_message = ( 'Description for directive `@my_directive` changed from `directive desc` to `None`' ) assert diff[0].message == expected_message assert diff[0].path == '@my_directive' assert diff[0].criticality == Criticality.safe()
def test_input_field_inner_type_changed(): a = schema(""" input Params { arg: [Int] } """) b = schema(""" input Params { arg: [String] } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "`Params.arg` type changed from `[Int]` to `[String]`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.breaking(ERROR)
def test_field_type_changed(): a_schema = schema(""" type Query { a: String! } """) changed_schema = schema(""" type Query { a: Int } """) diff = Schema(a_schema, changed_schema).diff() assert len(diff) == 1 assert diff[0].message == "`Query.a` type changed from `String!` to `Int`" assert diff[0].path == 'Query.a' assert diff[0].criticality == Criticality.breaking( 'Changing a field type will break queries that assume its type')
def test_deprecation_reason_changed(): a = schema(''' type Query { b: Int! @deprecated(reason: "Not used") } ''') b = schema(''' type Query { b: Int! @deprecated(reason: "Some string") } ''') diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "Deprecation reason on field `Query.b` changed from `Not used` to `Some string`" assert diff[0].path == 'Query.b' assert diff[0].criticality == Criticality.safe()
def test_input_field_dropped_non_null_constraint(): a = schema(""" input Params { arg: String! } """) b = schema(""" input Params { arg: String } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "`Params.arg` type changed from `String!` to `String`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.safe()