File: spec_helper.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (229 lines) | stat: -rw-r--r-- 5,697 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true
require 'rubygems'
require 'bundler'
require 'simplecov'
require 'simplecov-lcov'
SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true
SimpleCov.formatters = [
  SimpleCov::Formatter::HTMLFormatter,
  SimpleCov::Formatter::LcovFormatter
]
SimpleCov.start do
  enable_coverage :branch
  add_filter "spec/"
  add_group "Generators", "lib/generators"
  add_group "Execution", [/analysis/, /language/, /execution/, /static_validation/, /pagination/, /subscriptions/, /query/, /tracing/, /introspection/, /backtrace/]
  add_group "Helpers", [/rake_task/, /testing/, /rubocop/]
  add_group "Definition", [/types/, /relay/, /schema/]
  add_group "Dataloader", [/dataloader/]
end

Bundler.require

# Print full backtrace for failures:
ENV["BACKTRACE"] = "1"

require "graphql"
if ENV["GRAPHQL_CPARSER"]
  USING_C_PARSER = true
  puts "Opting in to GraphQL::CParser"
  require "graphql-c_parser"
else
  USING_C_PARSER = false
end

if ENV["GRAPHQL_REJECT_NUMBERS_FOLLOWED_BY_NAMES"]
  puts "Opting into GraphQL.reject_numbers_followed_by_names"
  GraphQL.reject_numbers_followed_by_names = true
  puts "Opting into GraphQL::Schema::Visibility::Profile"
  GraphQL::Schema.use(GraphQL::Schema::Visibility, migration_errors: true)
  ADD_WARDEN = false
else
  ADD_WARDEN = true
end

# C methods aren't fair game in non-main Ractors
RUN_RACTOR_TESTS = defined?(::Ractor) && !USING_C_PARSER

require "rake"
require "graphql/rake_task"
require "pry"
require "minitest/autorun"
require "minitest/focus"
require "minitest/reporters"
require "graphql/batch"

running_in_rubymine = ENV["RM_INFO"]
unless running_in_rubymine
  Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new(color: true)
end

# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new(color: true)

Minitest::Spec.make_my_diffs_pretty!

module CheckWardenShape
  DEFAULT_SHAPE = GraphQL::Schema::Warden.new(context: {}, schema: GraphQL::Schema).instance_variables

  class CheckShape
    def initialize(warden)
      @warden = warden
    end

    def call(_obj_id)
      ivars = @warden.instance_variables
      if ivars != DEFAULT_SHAPE
        raise <<-ERR
Object Shape Failed (#{@warden.class}):
  - Expected: #{DEFAULT_SHAPE.inspect}
  - Actual: #{ivars.inspect}
ERR
      # else # To make sure it's running properly:
      #   puts "OK Warden #{@warden.object_id}"
      end
    end
  end

  def prepare_ast
    super
    setup_finalizer
  end

  private

  def setup_finalizer
    if !@finalizer_defined
      @finalizer_defined = true
      if warden.is_a?(GraphQL::Schema::Warden)
        ObjectSpace.define_finalizer(self, CheckShape.new(warden))
      end
    end
  end
end

GraphQL::Query.prepend(CheckWardenShape)
# Filter out Minitest backtrace while allowing backtrace from other libraries
# to be shown.
Minitest.backtrace_filter = Minitest::BacktraceFilter.new

# Can be used as a GraphQL::Schema::Warden for some purposes, but allows nothing
module NothingWarden
  def self.enum_values(enum_type)
    []
  end
end

# Use this when a schema requires a `resolve_type` hook
# but you know it won't be called
NO_OP_RESOLVE_TYPE = ->(type, obj, ctx) {
  raise "this should never be called"
}

def testing_rails?
  defined?(::Rails)
end

def testing_mongoid?
  defined?(::Mongoid)
end

def testing_redis?
  defined?(::Redis)
end

if testing_rails?
  require "integration/rails/spec_helper"
end

if testing_mongoid?
  require "integration/mongoid/star_trek/data"
  require "integration/mongoid/star_trek/schema"
end

# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each do |f|
  require f
end

def star_trek_query(string, variables={}, context: {})
  StarTrek::Schema.execute(string, variables: variables, context: context)
end

def star_wars_query(string, variables={}, context: {})
  StarWars::Schema.execute(string, variables: variables, context: context)
end

def with_bidirectional_pagination
  prev_value = GraphQL::Relay::ConnectionType.bidirectional_pagination
  GraphQL::Relay::ConnectionType.bidirectional_pagination = true
  yield
ensure
  GraphQL::Relay::ConnectionType.bidirectional_pagination = prev_value
end

module TestTracing
  class << self
    def clear
      traces.clear
    end

    def with_trace
      clear
      yield
      traces
    end

    def traces
      @traces ||= []
    end

    def trace(key, data)
      data[:key] = key
      result = yield
      data[:result] = result
      traces << data
      result
    end
  end
end


if !USING_C_PARSER && defined?(GraphQL::CParser::Parser)
  raise "Load error: didn't opt in to C parser but GraphQL::CParser::Parser was defined"
end

def assert_warns(warning, printing = "")
  return_val = nil
  stdout, stderr = capture_io { return_val = yield }
  assert_equal warning, stderr, "It produced the expected stderr"
  assert_equal stdout, printing, "It produced the expected stdout"
  return_val
end

module Minitest
  class Test
    def self.it_dataloads(message, &block)
      it(message) do
        GraphQL::Dataloader.with_dataloading do |d|
          self.instance_exec(d, &block)
        end
      end
    end
  end

  module Assertions
    def assert_graphql_equal(data1, data2, message = "GraphQL Result was equal")
      case data1
      when Hash
        assert_equal(data1, data2, message)
        assert_equal(data1.keys, data2.keys, "Order of keys matched (#{message})")
      when Array
        data1.each_with_index do |item1, idx|
          assert_graphql_equal(item1, data2[idx], message + "[Item #{idx + 1}] ")
        end
      else
        raise ArgumentError, "assert_graphql_equal doesn't support #{data1.class} yet"
      end
    end
  end
end