Class: OpenapiFirst::Test::Coverage::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/test/coverage/plan.rb

Overview

This stores the coverage data for one API description A plan can be #done? and has several #tasks which can be #finished?

Defined Under Namespace

Classes: UnknownRequestError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_key:, filepath: nil) ⇒ Plan

Returns a new instance of Plan.



27
28
29
30
31
32
# File 'lib/openapi_first/test/coverage/plan.rb', line 27

def initialize(definition_key:, filepath: nil)
  @routes = []
  @index = {}
  @api_identifier = filepath || definition_key
  @filepath = filepath
end

Instance Attribute Details

#api_identifierObject (readonly)

Returns the value of attribute api_identifier.



34
35
36
# File 'lib/openapi_first/test/coverage/plan.rb', line 34

def api_identifier
  @api_identifier
end

#filepathObject (readonly)

Returns the value of attribute filepath.



34
35
36
# File 'lib/openapi_first/test/coverage/plan.rb', line 34

def filepath
  @filepath
end

#routesObject (readonly)

Returns the value of attribute routes.



34
35
36
# File 'lib/openapi_first/test/coverage/plan.rb', line 34

def routes
  @routes
end

Class Method Details

.for(oad, skip_response: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/openapi_first/test/coverage/plan.rb', line 15

def self.for(oad, skip_response: nil)
  plan = new(definition_key: oad.key, filepath: oad.filepath)
  oad.routes.each do |route|
    responses = skip_response ? route.responses.reject(&skip_response) : route.responses
    plan.add_route request_method: route.request_method,
                   path: route.path,
                   requests: route.requests,
                   responses:
  end
  plan
end

Instance Method Details

#add_route(request_method:, path:, requests:, responses:) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/openapi_first/test/coverage/plan.rb', line 61

def add_route(request_method:, path:, requests:, responses:)
  request_tasks = requests.to_a.map do |request|
    index[request.key] = RequestTask.new(request)
  end
  response_tasks = responses.to_a.map do |response|
    index[response.key] = ResponseTask.new(response)
  end
  @routes << RouteTask.new(path:, request_method:, requests: request_tasks, responses: response_tasks)
end

#coverageObject



49
50
51
52
53
54
55
# File 'lib/openapi_first/test/coverage/plan.rb', line 49

def coverage
  done = tasks.count(&:finished?)
  return 0 if done.zero?

  all = tasks.count
  (done / (all.to_f / 100))
end

#done?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/openapi_first/test/coverage/plan.rb', line 45

def done?
  tasks.all?(&:finished?)
end

#tasksObject



57
58
59
# File 'lib/openapi_first/test/coverage/plan.rb', line 57

def tasks
  index.values
end

#track_request(validated_request) ⇒ Object



37
38
39
# File 'lib/openapi_first/test/coverage/plan.rb', line 37

def track_request(validated_request)
  index[validated_request.request_definition.key].track(validated_request) if validated_request.known?
end

#track_response(validated_response) ⇒ Object



41
42
43
# File 'lib/openapi_first/test/coverage/plan.rb', line 41

def track_response(validated_response)
  index[validated_response.response_definition.key]&.track(validated_response) if validated_response.known?
end