File: backend_assertions.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 (96 lines) | stat: -rw-r--r-- 3,197 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
# frozen_string_literal: true

module GraphQLTracingDetailedTraceBackendAssertions
  def self.included(child_class)
    child_class.instance_eval do
      describe "BackendAssertions" do
        before do
          @backend = new_backend
          @backend.delete_all_traces
        end

        it "can save, retreive, list, and delete traces" do
          data = SecureRandom.bytes(1000)
          trace_id = @backend.save_trace(
            "GetStuff",
            100.56,
            (Time.utc(2024, 01, 01, 04, 44, 33, 695000).to_f * 1000).round,
            data
          )

          trace = @backend.find_trace(trace_id)
          assert_kind_of GraphQL::Tracing::DetailedTrace::StoredTrace, trace
          assert_equal trace_id, trace.id
          assert_equal "GetStuff", trace.operation_name
          assert_equal 100.56, trace.duration_ms
          assert_equal "2024-01-01 04:44:33.694", Time.at(trace.begin_ms / 1000.0).utc.strftime("%Y-%m-%d %H:%M:%S.%L")
          assert_equal data, trace.trace_data


          @backend.save_trace(
            "GetOtherStuff",
            200.16,
            (Time.utc(2024, 01, 03, 04, 44, 33, 695000).to_f * 1000).round,
            data
          )

          @backend.save_trace(
            "GetMoreOtherStuff",
            200.16,
            (Time.utc(2024, 01, 03, 04, 44, 33, 795000).to_f * 1000).round,
            data
          )

          assert_equal ["GetMoreOtherStuff", "GetOtherStuff", "GetStuff" ], @backend.traces(last: 20, before: nil).map(&:operation_name)

          assert_equal ["GetMoreOtherStuff"], @backend.traces(last: 1, before: nil).map(&:operation_name)
          assert_equal ["GetOtherStuff", "GetStuff"], @backend.traces(last: 2, before: Time.utc(2024, 01, 03, 04, 44, 33, 795000).to_f * 1000).map(&:operation_name)


          @backend.delete_trace(trace_id)

          assert_equal ["GetMoreOtherStuff", "GetOtherStuff"], @backend.traces(last: 20, before: nil).map(&:operation_name)

          @backend.delete_all_traces
          assert_equal [], @backend.traces(last: 20, before: nil)
        end

        it "returns nil for nonexistent IDs" do
          assert_nil @backend.find_trace(999_999_999)
        end

        it "implements a limit" do
          limit_backend = new_backend(limit: 10)

          10.times do |n|
            limit_backend.save_trace(
              "Trace#{n}",
              1.5,
              5000 + n,
              "some-data"
            )
          end

          all_traces = limit_backend.traces(last: nil, before: nil)
          assert_equal 10, all_traces.size
          assert_equal "Trace9", all_traces.first.operation_name
          assert_equal "Trace0", all_traces.last.operation_name

          3.times do |n|
            limit_backend.save_trace(
              "Trace 2-#{n}",
              1.5,
              5020 + n,
              "some-data"
            )
          end

          all_traces = limit_backend.traces(last: nil, before: nil)
          assert_equal 10, all_traces.size
          assert_equal "Trace 2-2", all_traces.first.operation_name
          assert_equal "Trace3", all_traces.last.operation_name
        end
      end
    end
  end
end