|
| 1 | +"""Tests for schema sorting functionality.""" |
| 2 | +import enum |
| 3 | + |
| 4 | +from precisely import assert_that, contains_exactly, equal_to |
| 5 | + |
| 6 | +import graphlayer as g |
| 7 | +from graphlayer.graphql.schema import create_graphql_schema |
| 8 | + |
| 9 | + |
| 10 | +def test_fields_are_sorted_alphabetically_when_sort_schema_is_true(): |
| 11 | + """Test that object type fields are sorted alphabetically.""" |
| 12 | + Root = g.ObjectType("Root", fields=( |
| 13 | + g.field("zebra", g.String), |
| 14 | + g.field("apple", g.String), |
| 15 | + g.field("mango", g.String), |
| 16 | + g.field("banana", g.String), |
| 17 | + )) |
| 18 | + |
| 19 | + schema = create_graphql_schema(query_type=Root, sort_schema=True) |
| 20 | + field_names = list(schema.graphql_schema.query_type.fields.keys()) |
| 21 | + |
| 22 | + assert_that(field_names, equal_to(["apple", "banana", "mango", "zebra"])) |
| 23 | + |
| 24 | + |
| 25 | +def test_fields_preserve_definition_order_when_sort_schema_is_false(): |
| 26 | + """Test that object type fields preserve definition order when not sorted.""" |
| 27 | + Root = g.ObjectType("Root", fields=( |
| 28 | + g.field("zebra", g.String), |
| 29 | + g.field("apple", g.String), |
| 30 | + g.field("mango", g.String), |
| 31 | + g.field("banana", g.String), |
| 32 | + )) |
| 33 | + |
| 34 | + schema = create_graphql_schema(query_type=Root, sort_schema=False) |
| 35 | + field_names = list(schema.graphql_schema.query_type.fields.keys()) |
| 36 | + |
| 37 | + assert_that(field_names, equal_to(["zebra", "apple", "mango", "banana"])) |
| 38 | + |
| 39 | + |
| 40 | +def test_field_arguments_are_sorted_alphabetically_when_sort_schema_is_true(): |
| 41 | + """Test that field arguments are sorted alphabetically.""" |
| 42 | + Root = g.ObjectType("Root", fields=( |
| 43 | + g.field("value", g.String, params=( |
| 44 | + g.param("zebra", g.Int), |
| 45 | + g.param("apple", g.Int), |
| 46 | + g.param("mango", g.Int), |
| 47 | + g.param("banana", g.Int), |
| 48 | + )), |
| 49 | + )) |
| 50 | + |
| 51 | + schema = create_graphql_schema(query_type=Root, sort_schema=True) |
| 52 | + arg_names = list(schema.graphql_schema.query_type.fields["value"].args.keys()) |
| 53 | + |
| 54 | + assert_that(arg_names, equal_to(["apple", "banana", "mango", "zebra"])) |
| 55 | + |
| 56 | + |
| 57 | +def test_enum_values_are_sorted_alphabetically_when_sort_schema_is_true(): |
| 58 | + """Test that enum values are sorted alphabetically.""" |
| 59 | + class Color(enum.Enum): |
| 60 | + red = "RED" |
| 61 | + green = "GREEN" |
| 62 | + blue = "BLUE" |
| 63 | + yellow = "YELLOW" |
| 64 | + |
| 65 | + ColorType = g.EnumType(Color) |
| 66 | + |
| 67 | + Root = g.ObjectType("Root", fields=( |
| 68 | + g.field("color", ColorType), |
| 69 | + )) |
| 70 | + |
| 71 | + schema = create_graphql_schema(query_type=Root, sort_schema=True) |
| 72 | + |
| 73 | + # Find the Color enum type in the schema |
| 74 | + color_enum_type = None |
| 75 | + for type_obj in schema.graphql_schema.type_map.values(): |
| 76 | + if hasattr(type_obj, 'name') and type_obj.name == "Color": |
| 77 | + color_enum_type = type_obj |
| 78 | + break |
| 79 | + |
| 80 | + assert color_enum_type is not None, "Color enum type not found in schema" |
| 81 | + |
| 82 | + enum_value_names = list(color_enum_type.values.keys()) |
| 83 | + assert_that(enum_value_names, equal_to(["BLUE", "GREEN", "RED", "YELLOW"])) |
| 84 | + |
| 85 | + |
| 86 | +def test_nested_type_fields_are_sorted_when_sort_schema_is_true(): |
| 87 | + """Test that nested object type fields are also sorted.""" |
| 88 | + NestedType = g.ObjectType("Nested", fields=( |
| 89 | + g.field("zebra", g.String), |
| 90 | + g.field("apple", g.String), |
| 91 | + )) |
| 92 | + |
| 93 | + Root = g.ObjectType("Root", fields=( |
| 94 | + g.field("nested", NestedType), |
| 95 | + )) |
| 96 | + |
| 97 | + schema = create_graphql_schema(query_type=Root, sort_schema=True) |
| 98 | + |
| 99 | + # Get the Nested type from the schema |
| 100 | + nested_type = schema.graphql_schema.type_map["Nested"] |
| 101 | + field_names = list(nested_type.fields.keys()) |
| 102 | + |
| 103 | + assert_that(field_names, equal_to(["apple", "zebra"])) |
| 104 | + |
| 105 | + |
| 106 | +def test_mutation_fields_are_sorted_when_sort_schema_is_true(): |
| 107 | + """Test that mutation type fields are sorted alphabetically.""" |
| 108 | + Root = g.ObjectType("Root", fields=( |
| 109 | + g.field("value", g.String), |
| 110 | + )) |
| 111 | + |
| 112 | + Mutation = g.ObjectType("Mutation", fields=( |
| 113 | + g.field("updateZebra", g.String), |
| 114 | + g.field("createApple", g.String), |
| 115 | + g.field("deleteMango", g.String), |
| 116 | + )) |
| 117 | + |
| 118 | + schema = create_graphql_schema( |
| 119 | + query_type=Root, |
| 120 | + mutation_type=Mutation, |
| 121 | + sort_schema=True, |
| 122 | + ) |
| 123 | + |
| 124 | + mutation_field_names = list(schema.graphql_schema.mutation_type.fields.keys()) |
| 125 | + assert_that(mutation_field_names, equal_to(["createApple", "deleteMango", "updateZebra"])) |
| 126 | + |
| 127 | + |
| 128 | +def test_input_object_fields_are_sorted_when_sort_schema_is_true(): |
| 129 | + """Test that input object type fields are sorted alphabetically.""" |
| 130 | + InputType = g.InputObjectType("Input", fields=( |
| 131 | + g.input_field("zebra", g.String), |
| 132 | + g.input_field("apple", g.String), |
| 133 | + g.input_field("mango", g.String), |
| 134 | + )) |
| 135 | + |
| 136 | + Root = g.ObjectType("Root", fields=( |
| 137 | + g.field("process", g.String, params=( |
| 138 | + g.param("input", InputType), |
| 139 | + )), |
| 140 | + )) |
| 141 | + |
| 142 | + schema = create_graphql_schema(query_type=Root, sort_schema=True) |
| 143 | + |
| 144 | + input_type = schema.graphql_schema.type_map["Input"] |
| 145 | + field_names = list(input_type.fields.keys()) |
| 146 | + |
| 147 | + assert_that(field_names, equal_to(["apple", "mango", "zebra"])) |
| 148 | + |
| 149 | + |
| 150 | +def test_sort_schema_defaults_to_false(): |
| 151 | + """Test that sort_schema defaults to False (preserves definition order).""" |
| 152 | + Root = g.ObjectType("Root", fields=( |
| 153 | + g.field("zebra", g.String), |
| 154 | + g.field("apple", g.String), |
| 155 | + )) |
| 156 | + |
| 157 | + # Call without sort_schema parameter |
| 158 | + schema = create_graphql_schema(query_type=Root) |
| 159 | + field_names = list(schema.graphql_schema.query_type.fields.keys()) |
| 160 | + |
| 161 | + # Should preserve definition order (not sorted) |
| 162 | + assert_that(field_names, equal_to(["zebra", "apple"])) |
| 163 | + |
| 164 | + |
| 165 | +def test_types_are_sorted_in_type_map_when_sort_schema_is_true(): |
| 166 | + """Test that type names in the type map are sorted alphabetically.""" |
| 167 | + TypeZ = g.ObjectType("TypeZ", fields=(g.field("value", g.String),)) |
| 168 | + TypeA = g.ObjectType("TypeA", fields=(g.field("value", g.String),)) |
| 169 | + TypeM = g.ObjectType("TypeM", fields=(g.field("value", g.String),)) |
| 170 | + |
| 171 | + Root = g.ObjectType("Root", fields=( |
| 172 | + g.field("z", TypeZ), |
| 173 | + g.field("a", TypeA), |
| 174 | + g.field("m", TypeM), |
| 175 | + )) |
| 176 | + |
| 177 | + schema = create_graphql_schema(query_type=Root, sort_schema=True, types=(TypeZ, TypeA, TypeM)) |
| 178 | + |
| 179 | + # Filter out built-in types (those starting with __) |
| 180 | + custom_type_names = [ |
| 181 | + name for name in schema.graphql_schema.type_map.keys() |
| 182 | + if not name.startswith("__") |
| 183 | + ] |
| 184 | + |
| 185 | + # Check that non-introspection types (built-ins and custom object types) |
| 186 | + # appear in sorted order: Boolean, Root, String, TypeA, TypeM, TypeZ |
| 187 | + assert_that(custom_type_names, contains_exactly( |
| 188 | + "Boolean", "Root", "String", "TypeA", "TypeM", "TypeZ" |
| 189 | + )) |
0 commit comments