Skip to content

Commit 9567ba4

Browse files
committed
Migrate another test
1 parent a068c4a commit 9567ba4

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

spec/integration/rails/graphql/schema_spec.rb

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -208,38 +208,27 @@
208208
end
209209

210210
describe "#instrument" do
211-
class VariableCountInstrumenter
212-
attr_reader :counts
213-
def initialize
214-
@counts = []
215-
end
216-
217-
def before_query(query)
218-
@counts << query.variables.length
219-
end
220-
221-
def after_query(query)
222-
@counts << :end
211+
module VariableCountTrace
212+
def execute_query(query:)
213+
query.context[:counter] << query.variables.length
214+
super
215+
ensure
216+
query.context[:counter] << :end
223217
end
224218
end
225219

226220
# Use this to assert instrumenters are called as a stack
227-
class StackCheckInstrumenter
228-
def initialize(counter)
229-
@counter = counter
230-
end
231-
232-
def before_query(query)
233-
@counter.counts << :in
234-
end
235-
236-
def after_query(query)
237-
@counter.counts << :out
221+
module StackCheckTrace
222+
def execute_query(query:)
223+
query.context[:counter] << :in
224+
super
225+
ensure
226+
query.context[:counter] << :out
238227
end
239228
end
240229

241-
let(:variable_counter) {
242-
VariableCountInstrumenter.new
230+
let(:variable_counts) {
231+
[]
243232
}
244233

245234
let(:schema) {
@@ -257,23 +246,23 @@ def int(value:)
257246
end
258247

259248
query(query_type)
260-
instrument(:query, StackCheckInstrumenter.new(spec.variable_counter))
261-
instrument(:query, spec.variable_counter)
249+
trace_with VariableCountTrace
250+
trace_with StackCheckTrace
262251
end
263252
}
264253

265254
it "can wrap query execution" do
266-
schema.execute("query getInt($val: Int = 5){ int(value: $val) } ")
267-
schema.execute("query getInt($val: Int = 5, $val2: Int = 3){ int(value: $val) int2: int(value: $val2) } ")
268-
assert_equal [:in, 1, :end, :out, :in, 2, :end, :out], variable_counter.counts
255+
schema.execute("query getInt($val: Int = 5){ int(value: $val) } ", context: { counter: variable_counts })
256+
schema.execute("query getInt($val: Int = 5, $val2: Int = 3){ int(value: $val) int2: int(value: $val2) } ", context: { counter: variable_counts })
257+
assert_equal [:in, 1, :end, :out, :in, 2, :end, :out], variable_counts
269258
end
270259

271260
it "runs even when a runtime error occurs" do
272-
schema.execute("query getInt($val: Int = 5){ int(value: $val) } ")
261+
schema.execute("query getInt($val: Int = 5){ int(value: $val) } ", context: { counter: variable_counts })
273262
assert_raises(RuntimeError) {
274-
schema.execute("query getInt($val: Int = 13){ int(value: $val) } ")
263+
schema.execute("query getInt($val: Int = 13){ int(value: $val) } ", context: { counter: variable_counts })
275264
}
276-
assert_equal [:in, 1, :end, :out, :in, 1, :end, :out], variable_counter.counts
265+
assert_equal [:in, 1, :end, :out, :in, 1, :end, :out], variable_counts
277266
end
278267
end
279268

0 commit comments

Comments
 (0)