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(filepath:) ⇒ Plan

Returns a new instance of Plan.



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

def initialize(filepath:)
  @routes = []
  @index = {}
  @filepath = filepath
end

Instance Attribute Details

#filepathObject (readonly)

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

#routesObject (readonly)

Returns the value of attribute routes.



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

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(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



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

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



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

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

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

#done?Boolean

Returns:

  • (Boolean)


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

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

#tasksObject



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

def tasks
  index.values
end

#track_request(validated_request) ⇒ Object



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

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

#track_response(validated_response) ⇒ Object



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

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