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.



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

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.



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

def api_identifier
  @api_identifier
end

#filepathObject (readonly)

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Class Method Details

.for(oad, skip_response: nil, skip_route: nil) ⇒ Object



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

def self.for(oad, skip_response: nil, skip_route: nil)
  plan = new(definition_key: oad.key, filepath: oad.filepath)
  routes = oad.routes
  routes = routes.reject { |route| skip_route[route.path, route.request_method] } if skip_route
  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



65
66
67
68
69
70
71
72
73
# File 'lib/openapi_first/test/coverage/plan.rb', line 65

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



51
52
53
54
55
56
57
58
59
# File 'lib/openapi_first/test/coverage/plan.rb', line 51

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

  all = tasks.count
  return 100 if done == all

  (done / (all.to_f / 100))
end

#done?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/openapi_first/test/coverage/plan.rb', line 47

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

#tasksObject



61
62
63
# File 'lib/openapi_first/test/coverage/plan.rb', line 61

def tasks
  index.values
end

#track_request(validated_request) ⇒ Object



39
40
41
# File 'lib/openapi_first/test/coverage/plan.rb', line 39

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

#track_response(validated_response) ⇒ Object



43
44
45
# File 'lib/openapi_first/test/coverage/plan.rb', line 43

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