diff --git a/spec/graphql/execution/interpreter_spec.rb b/spec/graphql/execution/interpreter_spec.rb index 432abc901a..f280473efd 100644 --- a/spec/graphql/execution/interpreter_spec.rb +++ b/spec/graphql/execution/interpreter_spec.rb @@ -535,8 +535,8 @@ def execute_multiplex(multiplex:) assert_equal 3, res["data"]["findMany"].size assert_equal "RAV", res["data"]["findMany"][0]["sym"] - assert_equal nil, res["data"]["findMany"][1] - assert_equal nil, res["data"]["findMany"][2] + assert_nil res["data"]["findMany"][1] + assert_nil res["data"]["findMany"][2] assert_equal false, res.key?("errors") assert_equal Hash, res["data"].class diff --git a/spec/graphql/schema/input_object_spec.rb b/spec/graphql/schema/input_object_spec.rb index 8bf436a69e..7bf3078dcc 100644 --- a/spec/graphql/schema/input_object_spec.rb +++ b/spec/graphql/schema/input_object_spec.rb @@ -17,7 +17,7 @@ describe "type info" do it "has it" do assert_equal "EnsembleInput", input_object.graphql_name - assert_equal nil, input_object.description + assert_nil input_object.description assert_equal 1, input_object.arguments.size end diff --git a/spec/graphql/schema/loader_spec.rb b/spec/graphql/schema/loader_spec.rb index 368932a58e..552026ad98 100644 --- a/spec/graphql/schema/loader_spec.rb +++ b/spec/graphql/schema/loader_spec.rb @@ -154,74 +154,81 @@ def self.coerce_result(value, _ctx) } describe "load" do + def assert_equal_or_nil(expected_value, actual_value) + if expected_value.nil? + assert_nil actual_value + else + assert_equal expected_value, actual_value + end + end def assert_deep_equal(expected_type, actual_type) if actual_type.is_a?(Array) actual_type.each_with_index do |obj, index| assert_deep_equal expected_type[index], obj end elsif actual_type.is_a?(GraphQL::Schema::Field) - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.deprecation_reason, actual_type.deprecation_reason - assert_equal expected_type.arguments.keys.sort, actual_type.arguments.keys.sort + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.deprecation_reason, actual_type.deprecation_reason + assert_equal_or_nil expected_type.arguments.keys.sort, actual_type.arguments.keys.sort assert_deep_equal expected_type.arguments.values.sort_by(&:graphql_name), actual_type.arguments.values.sort_by(&:graphql_name) elsif actual_type.is_a?(GraphQL::Schema::EnumValue) - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.deprecation_reason, actual_type.deprecation_reason + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.deprecation_reason, actual_type.deprecation_reason elsif actual_type.is_a?(GraphQL::Schema::Argument) - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.deprecation_reason, actual_type.deprecation_reason + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.deprecation_reason, actual_type.deprecation_reason assert_deep_equal expected_type.type, actual_type.type elsif actual_type.is_a?(GraphQL::Schema::NonNull) || actual_type.is_a?(GraphQL::Schema::List) - assert_equal expected_type.class, actual_type.class + assert_equal_or_nil expected_type.class, actual_type.class assert_deep_equal expected_type.of_type, actual_type.of_type elsif actual_type < GraphQL::Schema - assert_equal expected_type.query.graphql_name, actual_type.query.graphql_name - assert_equal expected_type.mutation.graphql_name, actual_type.mutation.graphql_name - assert_equal expected_type.directives.keys.sort, actual_type.directives.keys.sort + assert_equal_or_nil expected_type.query.graphql_name, actual_type.query.graphql_name + assert_equal_or_nil expected_type.mutation.graphql_name, actual_type.mutation.graphql_name + assert_equal_or_nil expected_type.directives.keys.sort, actual_type.directives.keys.sort assert_deep_equal expected_type.directives.values.sort_by(&:graphql_name), actual_type.directives.values.sort_by(&:graphql_name) - assert_equal expected_type.types.keys.sort, actual_type.types.keys.sort + assert_equal_or_nil expected_type.types.keys.sort, actual_type.types.keys.sort assert_deep_equal expected_type.types.values.sort_by(&:graphql_name), actual_type.types.values.sort_by(&:graphql_name) - assert_equal expected_type.description, actual_type.description + assert_equal_or_nil expected_type.description, actual_type.description elsif actual_type < GraphQL::Schema::Object - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.interfaces.map(&:graphql_name).sort, actual_type.interfaces.map(&:graphql_name).sort + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.interfaces.map(&:graphql_name).sort, actual_type.interfaces.map(&:graphql_name).sort assert_deep_equal expected_type.interfaces.sort_by(&:graphql_name), actual_type.interfaces.sort_by(&:graphql_name) - assert_equal expected_type.fields.keys.sort, actual_type.fields.keys.sort + assert_equal_or_nil expected_type.fields.keys.sort, actual_type.fields.keys.sort assert_deep_equal expected_type.fields.values.sort_by(&:graphql_name), actual_type.fields.values.sort_by(&:graphql_name) elsif actual_type < GraphQL::Schema::Interface - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.fields.keys.sort, actual_type.fields.keys.sort + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.fields.keys.sort, actual_type.fields.keys.sort assert_deep_equal expected_type.fields.values.sort_by(&:graphql_name), actual_type.fields.values.sort_by(&:graphql_name) elsif actual_type < GraphQL::Schema::Union - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.possible_types.map(&:graphql_name).sort, actual_type.possible_types.map(&:graphql_name).sort + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.possible_types.map(&:graphql_name).sort, actual_type.possible_types.map(&:graphql_name).sort assert_deep_equal expected_type.possible_types.sort_by(&:graphql_name), actual_type.possible_types.sort_by(&:graphql_name) elsif actual_type < GraphQL::Schema::Scalar - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.specified_by_url, actual_type.specified_by_url + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.specified_by_url, actual_type.specified_by_url elsif actual_type < GraphQL::Schema::Enum - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description assert_deep_equal expected_type.values.values.sort_by(&:graphql_name), actual_type.values.values.sort_by(&:graphql_name) elsif actual_type < GraphQL::Schema::InputObject - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.arguments.keys.sort, actual_type.arguments.keys.sort + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.arguments.keys.sort, actual_type.arguments.keys.sort assert_deep_equal expected_type.arguments.values.sort_by(&:graphql_name), actual_type.arguments.values.sort_by(&:graphql_name) elsif actual_type < GraphQL::Schema::Directive - assert_equal expected_type.graphql_name, actual_type.graphql_name - assert_equal expected_type.description, actual_type.description - assert_equal expected_type.repeatable?, actual_type.repeatable? - assert_equal expected_type.locations.sort, actual_type.locations.sort - assert_equal expected_type.arguments.keys.sort, actual_type.arguments.keys.sort + assert_equal_or_nil expected_type.graphql_name, actual_type.graphql_name + assert_equal_or_nil expected_type.description, actual_type.description + assert_equal_or_nil expected_type.repeatable?, actual_type.repeatable? + assert_equal_or_nil expected_type.locations.sort, actual_type.locations.sort + assert_equal_or_nil expected_type.arguments.keys.sort, actual_type.arguments.keys.sort assert_deep_equal expected_type.arguments.values.sort_by(&:graphql_name), actual_type.arguments.values.sort_by(&:graphql_name) else - assert_equal expected_type, actual_type + assert_equa_or_nil expected_type, actual_type end end diff --git a/spec/graphql/schema/member/has_arguments_spec.rb b/spec/graphql/schema/member/has_arguments_spec.rb index a4a9de58e2..ae4253022c 100644 --- a/spec/graphql/schema/member/has_arguments_spec.rb +++ b/spec/graphql/schema/member/has_arguments_spec.rb @@ -23,7 +23,7 @@ def add(left:, right:) it "doesn't require authorization when arguments with default values aren't present in the query" do assert_equal 5, DefaultArgumentAuthSchema.execute("{ add(left: 3, right: 2) }", context: { is_authorized: true })["data"].fetch("add") - assert_equal nil, DefaultArgumentAuthSchema.execute("{ add(left: 3, right: 2) }", context: { is_authorized: false })["data"].fetch("add") + assert_nil DefaultArgumentAuthSchema.execute("{ add(left: 3, right: 2) }", context: { is_authorized: false })["data"].fetch("add") assert_equal 4, DefaultArgumentAuthSchema.execute("{ add(left: 3) }", context: { is_authorized: false })["data"].fetch("add") end end diff --git a/spec/graphql/schema/resolver_spec.rb b/spec/graphql/schema/resolver_spec.rb index c9532bed35..d9c8273fb8 100644 --- a/spec/graphql/schema/resolver_spec.rb +++ b/spec/graphql/schema/resolver_spec.rb @@ -935,21 +935,21 @@ def load_input(input); end it "preserves `nil` when nullable argument is provided `null`" do res = exec_query("mutation { mutationWithNullableLoadsArgument(labelId: null) { inputs } }") - assert_equal nil, res["errors"] + assert_nil res["errors"] assert_equal '{"label":null}', res["data"]["mutationWithNullableLoadsArgument"]["inputs"] end it "preserves `nil` when nullable list argument is provided `null`" do res = exec_query("mutation { mutationWithNullableLoadsArgument(labelIds: null) { inputs } }") - assert_equal nil, res["errors"] + assert_nil res["errors"] assert_equal '{"labels":null}', res["data"]["mutationWithNullableLoadsArgument"]["inputs"] end it "omits omitted nullable argument" do res = exec_query("mutation { mutationWithNullableLoadsArgument { inputs } }") - assert_equal nil, res["errors"] + assert_nil res["errors"] assert_equal "{}", res["data"]["mutationWithNullableLoadsArgument"]["inputs"] end diff --git a/spec/graphql/schema/validator/allow_blank_validator_spec.rb b/spec/graphql/schema/validator/allow_blank_validator_spec.rb index f605c8d6b2..408b825264 100644 --- a/spec/graphql/schema/validator/allow_blank_validator_spec.rb +++ b/spec/graphql/schema/validator/allow_blank_validator_spec.rb @@ -15,14 +15,14 @@ it "rejects blank by default" do schema = build_schema(String, {length: { minimum: 5 }}) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: ValidatorHelpers::BlankString.new }) - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] assert_equal ["value is too short (minimum is 5)"], result["errors"].map { |e| e["message"] } end it "can be used standalone" do schema = build_schema(String, { allow_blank: false }) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: ValidatorHelpers::BlankString.new }) - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] assert_equal ["value can't be blank"], result["errors"].map { |e| e["message"] } end end diff --git a/spec/graphql/schema/validator/allow_null_validator_spec.rb b/spec/graphql/schema/validator/allow_null_validator_spec.rb index 8c978f7114..ecf1e94419 100644 --- a/spec/graphql/schema/validator/allow_null_validator_spec.rb +++ b/spec/graphql/schema/validator/allow_null_validator_spec.rb @@ -8,32 +8,32 @@ it "allows nil when permitted" do schema = build_schema(String, {length: { minimum: 5 }, allow_null: true}) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil }) - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] refute result.key?("errors") end it "rejects null by default" do schema = build_schema(String, {length: { minimum: 5 }}) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil }) - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] assert_equal ["value is too short (minimum is 5)"], result["errors"].map { |e| e["message"] } end it "can be used standalone" do schema = build_schema(String, { allow_null: false }) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil }) - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] assert_equal ["value can't be null"], result["errors"].map { |e| e["message"] } end it "allows nil when no validations are configured" do schema = build_schema(String, {}) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil }) - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] refute result.key?("errors") result = schema.execute("query { validated }") - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] refute result.key?("errors") end end diff --git a/spec/graphql/schema/validator/length_validator_spec.rb b/spec/graphql/schema/validator/length_validator_spec.rb index f5877e2208..0c4445f7ce 100644 --- a/spec/graphql/schema/validator/length_validator_spec.rb +++ b/spec/graphql/schema/validator/length_validator_spec.rb @@ -20,7 +20,7 @@ schema = build_schema(String, {length: { minimum: 5 }, allow_null: true, allow_blank: false}) result = schema.execute("{ validated(value: null) }") - assert_equal nil, result["data"]["validated"] + assert_nil result["data"]["validated"] refute result.key?("errors") result = schema.execute("query($str: String!) { validated(value: $str) }", variables: { str: blank_string }) diff --git a/spec/graphql/schema_spec.rb b/spec/graphql/schema_spec.rb index fdb2aa8ac3..c8457641ab 100644 --- a/spec/graphql/schema_spec.rb +++ b/spec/graphql/schema_spec.rb @@ -47,6 +47,7 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions extra_types ExtraType query_analyzer Object.new multiplex_analyzer Object.new + validate_timeout 100 rescue_from(StandardError) { } use GraphQL::Backtrace use GraphQL::Subscriptions::ActionCableSubscriptions, action_cable: nil, action_cable_coder: JSON diff --git a/spec/graphql/subscriptions_spec.rb b/spec/graphql/subscriptions_spec.rb index 245c6196a4..4c8815db89 100644 --- a/spec/graphql/subscriptions_spec.rb +++ b/spec/graphql/subscriptions_spec.rb @@ -736,7 +736,7 @@ def str failedEvent(id: $id) { str, int } } GRAPHQL - assert_equal nil, res["data"] + assert_nil res["data"] assert_equal "unauthorized", res["errors"][0]["message"] assert_equal 0, subscriptions_by_topic.size diff --git a/spec/graphql/types/big_int_spec.rb b/spec/graphql/types/big_int_spec.rb index 64558a97ff..8dc93d86d6 100644 --- a/spec/graphql/types/big_int_spec.rb +++ b/spec/graphql/types/big_int_spec.rb @@ -18,12 +18,12 @@ end it "returns `nil` for invalid inputs" do - assert_equal nil, GraphQL::Types::BigInt.coerce_input("xyz", nil) - assert_equal nil, GraphQL::Types::BigInt.coerce_input("2.2", nil) + assert_nil GraphQL::Types::BigInt.coerce_input("xyz", nil) + assert_nil GraphQL::Types::BigInt.coerce_input("2.2", nil) end it 'returns `nil` for nil' do - assert_equal nil, GraphQL::Types::BigInt.coerce_input(nil, nil) + assert_nil GraphQL::Types::BigInt.coerce_input(nil, nil) end it 'parses integers with base 10' do diff --git a/spec/integration/rails/graphql/query/variables_spec.rb b/spec/integration/rails/graphql/query/variables_spec.rb index 992b337367..c169a42fc1 100644 --- a/spec/integration/rails/graphql/query/variables_spec.rb +++ b/spec/integration/rails/graphql/query/variables_spec.rb @@ -230,7 +230,11 @@ class << self def assert_has_key_with_value(hash, key, has_key, value) assert_equal(has_key, hash.key?(key)) - assert_equal(value, hash[key]) + if value.nil? + assert_nil hash[key] + else + assert_equal(value, hash[key]) + end end it "preserves explicit null" do diff --git a/spec/integration/rails/graphql/types/iso_8601_duration_spec.rb b/spec/integration/rails/graphql/types/iso_8601_duration_spec.rb index f1c9eaa38f..8c670c7f04 100644 --- a/spec/integration/rails/graphql/types/iso_8601_duration_spec.rb +++ b/spec/integration/rails/graphql/types/iso_8601_duration_spec.rb @@ -12,7 +12,7 @@ def self.type_error(err, ctx) let(:context) { GraphQL::Query.new(DurationTest::Schema, "{ __typename }").context } - # 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5.12345 seconds + # 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5.12345 seconds let (:duration_str) { "P3Y6M4DT12H30M5.12345S" } let (:duration) { ActiveSupport::Duration.parse(duration_str) } @@ -67,7 +67,7 @@ def self.type_error(err, ctx) describe "coercing nil" do it "returns nil" do - assert_equal nil, GraphQL::Types::ISO8601Duration.coerce_input(nil, context) + assert_nil GraphQL::Types::ISO8601Duration.coerce_input(nil, context) end end @@ -98,7 +98,7 @@ def self.type_error(err, ctx) it "coerce_result and coerce_input raise GraphQL::Error" do old_active_support = defined?(ActiveSupport) ? ActiveSupport : nil Object.send(:remove_const, :ActiveSupport) if defined?(ActiveSupport) - + assert_raises GraphQL::Error do GraphQL::Types::ISO8601Duration.coerce_result("", context) end