Module: GraphQL::Coverage

Defined in:
lib/graphql/coverage.rb,
lib/graphql/coverage/cli.rb,
lib/graphql/coverage/call.rb,
lib/graphql/coverage/store.rb,
lib/graphql/coverage/trace.rb,
lib/graphql/coverage/errors.rb,
lib/graphql/coverage/result.rb,
lib/graphql/coverage/version.rb

Defined Under Namespace

Modules: Errors, Trace Classes: CLI, Call, Result, Store

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ignored_fieldsObject

Returns the value of attribute ignored_fields.



16
17
18
# File 'lib/graphql/coverage.rb', line 16

def ignored_fields
  @ignored_fields
end

Class Method Details

.dump(file_path) ⇒ Object



24
25
26
27
28
# File 'lib/graphql/coverage.rb', line 24

def self.dump(file_path)
  calls = Store.current.calls.map(&:to_h)
  content = JSON.generate({ calls: calls, schema: @schema.name, ignored_fields: ignored_fields })
  File.write(_ = file_path, content)
end

.enable(schema) ⇒ Object



19
20
21
22
# File 'lib/graphql/coverage.rb', line 19

def self.enable(schema)
  self.schema = schema
  schema.trace_with(Trace)
end

.load(*file_paths) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/coverage.rb', line 30

def self.load(*file_paths)
  file_paths.each do |file_path|
    content = JSON.parse(File.read(_ = file_path))
    self.schema = Object.const_get(content['schema'])
    self.ignored_fields = content['ignored_fields']

    content['calls'].each do |call_hash|
      call = Call.new(type: call_hash['type'], field: call_hash['field'], result_type: call_hash['result_type'])
      Store.current.append(call)
    end
  end
end

.report(output: $stdout) ⇒ Object



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
# File 'lib/graphql/coverage.rb', line 43

def self.report(output: $stdout)
  res = result

  puts_rate = proc do
    available_size = res.available_fields.size
    covered_size = res.covered_fields.size
    ignored_size = res.ignored_fields.size

    cover_rate = sprintf("%.2f", covered_size.to_f / available_size * 100)
    ignore_rate = sprintf("%.2f", ignored_size.to_f / available_size * 100)

    output.puts "#{covered_size} / #{available_size} fields covered (#{cover_rate}%)"
    output.puts "#{ignored_size} / #{available_size} fields ignored (#{ignore_rate}%)" if 0 < ignored_size
  end

  if res.uncovered_fields.empty?
    output.puts "All fields are covered"
    puts_rate.call
    true
  else
    output.puts "There are uncovered fields"
    puts_rate.call
    output.puts "Missing fields:"
    res.uncovered_fields.each do |call|
      output.puts "  #{call.type}.#{call.field}"
    end
    false
  end
end

.report!(output: $stdout) ⇒ Object



73
74
75
# File 'lib/graphql/coverage.rb', line 73

def self.report!(output: $stdout)
  report(output: output) or raise Errors::UncoveredFields
end

.reset!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



82
83
84
85
86
# File 'lib/graphql/coverage.rb', line 82

def self.reset!
  __skip__ = @schema = nil
  self.ignored_fields = []
  Store.reset!
end

.resultObject



77
78
79
# File 'lib/graphql/coverage.rb', line 77

def self.result
  Result.new(calls: Store.current.calls, schema: @schema, ignored_fields: ignored_fields)
end