File: rspec_matchers.rb

package info (click to toggle)
ruby-elasticsearch 7.17.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,820 kB
  • sloc: ruby: 44,308; sh: 16; makefile: 2
file content (297 lines) | stat: -rw-r--r-- 10,319 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Match the `length` of a field.
RSpec::Matchers.define :match_response_field_length do |expected_pairs, test|
  match do |response|
    expected_pairs.all? do |expected_key, expected_value|
      # ssl test returns results at '$body' key. See ssl/10_basic.yml
      expected_pairs = expected_pairs['$body'] if expected_pairs['$body']

      split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
        test.get_cached_value(k)
      end

      actual_value = split_key.inject(response) do |_response, key|
        # If the key is an index, indicating element of a list
        if _response.empty? && key == '$body'
          _response
        else
          _response[key] || _response[key.to_s]
        end
      end
      actual_value.size == expected_value
    end
  end
end

# Validate that a field is `true`.
RSpec::Matchers.define :match_true_field do |field, test|
  match do |response|
    # Handle is_true: ''
    return !!response if field == ''

    split_key = TestFile::Test.split_and_parse_key(field).collect do |k|
      test.get_cached_value(k)
    end
    !!TestFile::Test.find_value_in_document(split_key, response)
  end
end

# Validate that a field is `false`.
RSpec::Matchers.define :match_false_field do |field, test|
  match do |response|
    # Handle is_false: ''
    return !response if field == ''
    split_key = TestFile::Test.split_and_parse_key(field).collect do |k|
      test.get_cached_value(k)
    end
    value_in_doc = TestFile::Test.find_value_in_document(split_key, response)
    value_in_doc == 0 || !value_in_doc
  end
end

# Validate that a field is `gte` than a given value.
RSpec::Matchers.define :match_gte_field do |expected_pairs, test|
  match do |response|
    expected_pairs.all? do |expected_key, expected_value|

      split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
        test.get_cached_value(k)
      end
      actual_value = split_key.inject(response) do |_response, key|

        # If the key is an index, indicating element of a list
        if _response.empty? && key == '$body'
          _response
        else
          _response[key] || _response[key]
        end
      end
      actual_value >= test.get_cached_value(expected_value)
    end
  end
end

# Validate that a field is `gt` than a given value.
RSpec::Matchers.define :match_gt_field do |expected_pairs, test|
  match do |response|
    expected_pairs.all? do |expected_key, expected_value|

      split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
        test.get_cached_value(k)
      end

      actual_value = split_key.inject(response) do |_response, key|
        # If the key is an index, indicating element of a list
        if _response.empty? && key == '$body'
          _response
        else
          _response[key] || _response[key.to_s]
        end
      end
      actual_value > test.get_cached_value(expected_value)
    end
  end
end

# Validate that a field is `lte` than a given value.
RSpec::Matchers.define :match_lte_field do |expected_pairs, test|
  match do |response|
    expected_pairs.all? do |expected_key, expected_value|

      split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
        test.get_cached_value(k)
      end

      actual_value = split_key.inject(response) do |_response, key|
        # If the key is an index, indicating element of a list
        if _response.empty? && key == '$body'
          _response
        else
          _response[key] || _response[key.to_s]
        end
      end
      actual_value <= test.get_cached_value(expected_value)
    end
  end
end

# Validate that a field is `lt` than a given value.
RSpec::Matchers.define :match_lt_field do |expected_pairs, test|
  match do |response|
    expected_pairs.all? do |expected_key, expected_value|

      split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
        test.get_cached_value(k)
      end

      actual_value = split_key.inject(response) do |_response, key|
        # If the key is an index, indicating element of a list
        if _response.empty? && key == '$body'
          _response
        else
          _response[key] || _response[key.to_s]
        end
      end
      actual_value < test.get_cached_value(expected_value)
    end
  end
end

# Match an arbitrary field of a response to a given value.
RSpec::Matchers.define :match_response do |pairs, test|
  match do |response|
    pairs = sanitize_pairs(pairs)
    compare_pairs(pairs, response, test).empty?
  end

  failure_message do |response|
    "the expected response pair/value(s) #{@mismatched_pairs}" +
        " does not match the pair/value(s) in the response #{response}"
  end

  def sanitize_pairs(expected_pairs)
    # sql test returns results at '$body' key. See sql/translate.yml
    @pairs ||= expected_pairs['$body'] ? expected_pairs['$body'] : expected_pairs
  end

  def compare_pairs(expected_pairs, response, test)
    @mismatched_pairs = {}
    if expected_pairs.is_a?(String)
      @mismatched_pairs = expected_pairs unless compare_string_response(expected_pairs, response)
    else
      compare_hash(expected_pairs, response, test)
    end
    @mismatched_pairs
  end

  def compare_hash(expected_pairs, actual_hash, test)
    expected_pairs.each do |expected_key, expected_value|
      # Find the value to compare in the response
      split_key = TestFile::Test.split_and_parse_key(expected_key).collect do |k|
        # Sometimes the expected *key* is a cached value from a previous request.
        test.get_cached_value(k)
      end
      # We now accept 'nested.keys' so let's try the previous implementation and if that doesn't
      # work, try with the nested key, otherwise, raise exception.
      begin
        actual_value = TestFile::Test.find_value_in_document(split_key, actual_hash)
      rescue TypeError => e
        actual_value = TestFile::Test.find_value_in_document(expected_key, actual_hash)
      rescue StandardError => e
        raise e
      end
      # When the expected_key is ''
      actual_value = actual_hash if split_key.empty?
      # Sometimes the key includes dots. See watcher/put_watch/60_put_watch_with_action_condition.yml
      actual_value = TestFile::Test.find_value_in_document(expected_key, actual_hash) if actual_value.nil?

      # Sometimes the expected *value* is a cached value from a previous request.
      # See test api_key/10_basic.yml
      expected_value = test.get_cached_value(expected_value)

      case expected_value
      when Hash
        compare_hash(expected_value, actual_value, test)
      when Array
        unless compare_array(expected_value, actual_value, test, actual_hash)
          @mismatched_pairs.merge!(expected_key => expected_value)
        end
      when String
        unless compare_string(expected_value, actual_value, test, actual_hash)
          @mismatched_pairs.merge!(expected_key => expected_value)
        end
      else
        unless expected_value == actual_value
          @mismatched_pairs.merge!(expected_key => expected_value)
        end
      end
    end
  end

  def compare_string(expected, actual_value, test, response)
    # When you must match a regex. For example:
    #   match: {task: '/.+:\d+/'}
    if expected[0] == '/' && expected[-1] == '/'
      parsed = expected
      expected.scan(/\$\{([a-z_0-9]+)\}/) do |match|
        parsed = parsed.gsub(/\$\{?#{match.first}\}?/, test.cached_values[match.first])
      end
      /#{parsed.tr("/", "")}/ =~ actual_value
    elsif !!(expected.match?(/^-?[0-9]{1}\.[0-9]+E[0-9]+/))
      # When the value in the yaml test is a big number, the format is
      # different from what Ruby uses, so we transform  X.XXXXEXX to X.XXXXXe+XX
      # to be able to compare the values
      actual_value.to_s == expected.gsub('E', 'e+')
    elsif expected == '' && actual_value != ''
      actual_value == response
    else
      expected == actual_value
    end
  end

  def compare_array(expected, actual, test, response)
    expected.each_with_index do |value, i|
      case value
      when Hash
        return false unless compare_hash(value, actual[i], test)
      when Array
        return false unless compare_array(value, actual[i], test, response)
      when String
        return false unless compare_string(value, actual[i], test, response)
      end
    end
  end

  def compare_string_response(expected_string, response)
    regexp = Regexp.new(expected_string.strip[1..-2], Regexp::EXTENDED|Regexp::MULTILINE)
    regexp =~ response
  end
end

# Match that a request returned a given error.
RSpec::Matchers.define :match_error do |expected_error|
  match do |actual_error|
    # Remove surrounding '/' in string representing Regex
    expected_error = expected_error.chomp("/")
    expected_error = expected_error[1..-1] if expected_error =~ /^\//
    message = actual_error.message.tr("\\","")

    case expected_error
    when 'request_timeout'
      message =~ /\[408\]/
    when 'missing'
      message =~ /\[404\]/
    when 'conflict'
      message =~ /\[409\]/
    when 'request'
      message =~ /\[500\]/
    when 'bad_request'
      message =~ /\[400\]/
    when 'param'
      message =~ /\[400\]/ ||
          actual_error.is_a?(ArgumentError)
    when 'unauthorized'
      actual_error.is_a?(Elasticsearch::Transport::Transport::Errors::Unauthorized)
    when 'forbidden'
      actual_error.is_a?(Elasticsearch::Transport::Transport::Errors::Forbidden)
    else
      message =~ /#{expected_error}/
    end
  end
end