File: detailed_trace_spec.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 (81 lines) | stat: -rw-r--r-- 2,711 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Tracing::DetailedTrace do
  class SamplerSchema < GraphQL::Schema
    class Query < GraphQL::Schema::Object
      field :truthy, Boolean, fallback_value: true
    end

    query(Query)
    use GraphQL::Tracing::DetailedTrace, memory: true
    def self.detailed_trace?(query)
      if query.is_a?(GraphQL::Execution::Multiplex)
        query.queries.all? { |q| q.context[:profile] != false }
      else
        query.context[:profile] != false
      end
    end
  end

  before do
    SamplerSchema.detailed_trace.delete_all_traces
  end

  it "runs when the configured trace mode is set" do
    assert_equal 0, SamplerSchema.detailed_trace.traces.size
    res = SamplerSchema.execute("{ truthy }", context: { profile: false })
    assert_equal true, res["data"]["truthy"]
    assert_equal 0, SamplerSchema.detailed_trace.traces.size

    SamplerSchema.execute("{ truthy }")
    assert_equal 1, SamplerSchema.detailed_trace.traces.size
  end

  it "calls through to storage for access methods" do
    SamplerSchema.execute("{ truthy }")
    id = SamplerSchema.detailed_trace.traces.first.id
    assert_kind_of GraphQL::Tracing::DetailedTrace::StoredTrace, SamplerSchema.detailed_trace.find_trace(id)
    SamplerSchema.detailed_trace.delete_trace(id)
    assert_equal 0, SamplerSchema.detailed_trace.traces.size

    SamplerSchema.execute("{ truthy }")
    assert_equal 1, SamplerSchema.detailed_trace.traces.size
    SamplerSchema.detailed_trace.delete_all_traces
  end

  if testing_rails?
    it "defaults to ActiveRecord" do
      schema = Class.new(GraphQL::Schema) do
        use GraphQL::Tracing::DetailedTrace
      end

      assert_instance_of GraphQL::Tracing::DetailedTrace::ActiveRecordBackend, schema.detailed_trace.instance_variable_get(:@storage)
    end
  else
    it "raises when no storage is configured" do
      err = assert_raises ArgumentError do
        Class.new(GraphQL::Schema) do
          use GraphQL::Tracing::DetailedTrace
        end
      end
      assert_equal "To store traces, install ActiveRecord or provide `redis: ...`", err.message
    end
  end

  it "calls detailed_profile? on a Multiplex" do
    assert_equal 0, SamplerSchema.detailed_trace.traces.size

    SamplerSchema.multiplex([
      { query: "{ truthy }", context: { profile: false } },
      { query: "{ truthy }", context: { profile: true } },
    ])
    assert_equal 0, SamplerSchema.detailed_trace.traces.size

    SamplerSchema.multiplex([
      { query: "{ truthy }", context: { profile: true } },
      { query: "{ truthy }", context: { profile: true } },
    ])
    assert_equal 1, SamplerSchema.detailed_trace.traces.size
  end
end