Class: Maze::Assertions::RequestSetAssertions

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/assertions/request_set_assertions.rb

Overview

Provides helper routines for checking sets of requests against values in a table.

Class Method Summary collapse

Class Method Details

.assert_requests_match(requests, table) ⇒ Object

Checks that a set of requests satisfy the properties expressed by the table given.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/maze/assertions/request_set_assertions.rb', line 18

def assert_requests_match(requests, table)
  Maze.check.equal(table.hashes.length,
                   requests.length,
                   'Number of requests do not match number of entries in table.')
  matches = matching_rows requests, table
  return if matches.length == table.hashes.length

  # Not all matched - log diagnostic before failing assertion
  $logger.error "Only #{matches.length} of #{requests.length} matched:"
  $logger.info matches.keys.sort
  matches.sort.to_h.each do |row, request|
    $logger.info "#{table.rows[row]} matched by request element #{matches[request]}"
  end
  Maze.check.equal(requests.length, matches.length, 'Not all requests matched a row in the table.')
end

.matching_rows(requests, table) ⇒ Hash

Given arrays of requests and table-based criteria, determines which rows of the table are satisfied by one of the requests. Where multiple rows in the table specify the same criteria, there must be multiple requests provided to satisfy each.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/maze/assertions/request_set_assertions.rb', line 44

def matching_rows(requests, table)

  # iterate through each row in the table. exactly 1 request should match each row.
  row_to_request_matches = {}
  table.hashes.each_with_index do |row, row_index|
    requests.each_with_index do |request, request_index|
      # Skip if row already matched
      next if row_to_request_matches.values.include? request_index
      # Skip if no body in this request
      next unless request.key?(:body)
      next unless request_matches_row(request[:body], row)

      # Record the match
      row_to_request_matches[row_index] = request_index
    end
  end
  row_to_request_matches
end

.request_matches_row(body, row) ⇒ Object

Determines if a request body satisfies the criteria expressed by a row. The special string “null” is interpreted as nil in comparisons and regular expressions are assumed is the start and end of the string is a ‘/’.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/maze/assertions/request_set_assertions.rb', line 70

def request_matches_row(body, row)
  row.each do |key, expected_value|
    obs_val = Maze::Helper.read_key_path(body, key)
    next if ('null'.eql? expected_value) && obs_val.nil? # Both are null/nil
    next if ('@not_null'.eql? expected_value) && !obs_val.nil? # The value isn't null

    unless obs_val.nil?
      if expected_value[0] == '/' && expected_value[-1] == '/'
        # Treat as regexp
        regex = Regexp.new expected_value[1, expected_value.length - 2]
        next if regex.match? obs_val.to_s # Value matches regex
      elsif expected_value.eql? obs_val.to_s
        # Values match
        next
      end
    end

    # Match not found - return false
    return false
  end
  # All matched - return true
  true
end