Skip to content

Commit 83768d6

Browse files
committed
[API] Test Runner: Refactors running rspec matchers into file
1 parent 0a33fdc commit 83768d6

File tree

2 files changed

+117
-95
lines changed

2 files changed

+117
-95
lines changed

elasticsearch-api/spec/elasticsearch/api/rest_api_yaml_spec.rb

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
require 'spec_helper'
1919
require 'rest_api_tests_helper'
20+
require_relative './run_rspec_matchers'
21+
22+
LOGGER = Logger.new($stdout)
2023

2124
describe 'Rest API YAML tests' do
22-
LOGGER = Logger.new($stdout)
2325
if REST_API_YAML_FILES.empty?
2426
LOGGER.error 'No test files found!'
2527
LOGGER.info 'Use rake rake elasticsearch:download_artifacts in the root directory of the project to download the test artifacts.'
@@ -37,8 +39,7 @@
3739
next
3840
end
3941

40-
ctx = file.gsub("#{YAML_FILES_DIRECTORY}/", '')
41-
context ctx do
42+
context file.gsub("#{YAML_FILES_DIRECTORY}/", '') do
4243
let(:client) { DEFAULT_CLIENT }
4344

4445
test_file.tests.each do |test|
@@ -59,98 +60,8 @@
5960
task_group.run(client)
6061
end
6162

62-
# 'catch' is in the task group definition
63-
if task_group.catch_exception?
64-
it 'sends the request and throws the expected error' do
65-
expect(task_group.exception).to match_error(task_group.expected_exception_message)
66-
end
67-
68-
# 'match' on error description is in the task group definition
69-
if task_group.has_match_clauses?
70-
task_group.match_clauses.each do |match|
71-
it 'contains the expected error in the request response' do
72-
regexp = if (val = match['match'].values.first.to_s).include?('\\s')
73-
Regexp.new(val.gsub('\\\\', '\\').gsub('/', ''))
74-
else
75-
Regexp.new(Regexp.escape(val))
76-
end
77-
expect(task_group.exception.message).to match(regexp)
78-
end
79-
end
80-
end
81-
else
82-
# 'match' is in the task group definition
83-
if task_group.has_match_clauses?
84-
task_group.match_clauses.each do |match|
85-
it "has the expected value (#{match['match'].values.join(',')}) in the response field (#{match['match'].keys.join(',')})" do
86-
expect(task_group.response).to match_response(match['match'], test)
87-
end
88-
end
89-
end
90-
91-
# 'length' is in the task group definition
92-
if task_group.has_length_match_clauses?
93-
task_group.length_match_clauses.each do |match|
94-
it "the '#{match['length'].keys.join(',')}' field have the expected length" do
95-
expect(task_group.response).to match_response_field_length(match['length'], test)
96-
end
97-
end
98-
end
99-
100-
# 'is_true' is in the task group definition
101-
if task_group.has_true_clauses?
102-
task_group.true_clauses.each do |match|
103-
it "sends the request and the '#{match['is_true']}' field is set to true" do
104-
expect(task_group.response).to match_true_field(match['is_true'], test)
105-
end
106-
end
107-
end
108-
109-
# 'is_false' is in the task group definition
110-
if task_group.has_false_clauses?
111-
task_group.false_clauses.each do |match|
112-
it "sends the request and the '#{match['is_false']}' field is set to true" do
113-
expect(task_group.response).to match_false_field(match['is_false'], test)
114-
end
115-
end
116-
end
117-
118-
# 'gte' is in the task group definition
119-
if task_group.has_gte_clauses?
120-
task_group.gte_clauses.each do |match|
121-
it "sends the request and the '#{match['gte']}' field is greater than or equal to the expected value" do
122-
expect(task_group.response).to match_gte_field(match['gte'], test)
123-
end
124-
end
125-
end
126-
127-
# 'gt' is in the task group definition
128-
if task_group.has_gt_clauses?
129-
task_group.gt_clauses.each do |match|
130-
it "sends the request and the '#{match['gt']}' field is greater than the expected value" do
131-
expect(task_group.response).to match_gt_field(match['gt'], test)
132-
end
133-
end
134-
end
135-
136-
# 'lte' is in the task group definition
137-
if task_group.has_lte_clauses?
138-
task_group.lte_clauses.each do |match|
139-
it "sends the request and the '#{match['lte']}' field is less than or equal to the expected value" do
140-
expect(task_group.response).to match_lte_field(match['lte'], test)
141-
end
142-
end
143-
end
144-
145-
# 'lt' is in the task group definition
146-
if task_group.has_lt_clauses?
147-
task_group.lt_clauses.each do |match|
148-
it "sends the request and the '#{match['lt']}' field is less than the expected value" do
149-
expect(task_group.response).to match_lt_field(match['lt'], test)
150-
end
151-
end
152-
end
153-
end
63+
# ./run_task_groups.rb
64+
run_rspec_matchers_on_task_group(task_group, test)
15465
end
15566
end
15667
end
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# 'catch' is in the task group definition
19+
def run_rspec_matchers_on_task_group(task_group, test)
20+
if task_group.catch_exception?
21+
it 'sends the request and throws the expected error' do
22+
expect(task_group.exception).to match_error(task_group.expected_exception_message)
23+
end
24+
25+
# 'match' on error description is in the task group definition
26+
if task_group.has_match_clauses?
27+
task_group.match_clauses.each do |match|
28+
it 'contains the expected error in the request response' do
29+
regexp = if (val = match['match'].values.first.to_s).include?('\\s')
30+
Regexp.new(val.gsub('\\\\', '\\').gsub('/', ''))
31+
else
32+
Regexp.new(Regexp.escape(val))
33+
end
34+
expect(task_group.exception.message).to match(regexp)
35+
end
36+
end
37+
end
38+
else
39+
# 'match' is in the task group definition
40+
if task_group.has_match_clauses?
41+
task_group.match_clauses.each do |match|
42+
it "has the expected value (#{match['match'].values.join(',')}) in the response field (#{match['match'].keys.join(',')})" do
43+
expect(task_group.response).to match_response(match['match'], test)
44+
end
45+
end
46+
end
47+
48+
# 'length' is in the task group definition
49+
if task_group.has_length_match_clauses?
50+
task_group.length_match_clauses.each do |match|
51+
it "the '#{match['length'].keys.join(',')}' field have the expected length" do
52+
expect(task_group.response).to match_response_field_length(match['length'], test)
53+
end
54+
end
55+
end
56+
57+
# 'is_true' is in the task group definition
58+
if task_group.has_true_clauses?
59+
task_group.true_clauses.each do |match|
60+
it "sends the request and the '#{match['is_true']}' field is set to true" do
61+
expect(task_group.response).to match_true_field(match['is_true'], test)
62+
end
63+
end
64+
end
65+
66+
# 'is_false' is in the task group definition
67+
if task_group.has_false_clauses?
68+
task_group.false_clauses.each do |match|
69+
it "sends the request and the '#{match['is_false']}' field is set to true" do
70+
expect(task_group.response).to match_false_field(match['is_false'], test)
71+
end
72+
end
73+
end
74+
75+
# 'gte' is in the task group definition
76+
if task_group.has_gte_clauses?
77+
task_group.gte_clauses.each do |match|
78+
it "sends the request and the '#{match['gte']}' field is greater than or equal to the expected value" do
79+
expect(task_group.response).to match_gte_field(match['gte'], test)
80+
end
81+
end
82+
end
83+
84+
# 'gt' is in the task group definition
85+
if task_group.has_gt_clauses?
86+
task_group.gt_clauses.each do |match|
87+
it "sends the request and the '#{match['gt']}' field is greater than the expected value" do
88+
expect(task_group.response).to match_gt_field(match['gt'], test)
89+
end
90+
end
91+
end
92+
93+
# 'lte' is in the task group definition
94+
if task_group.has_lte_clauses?
95+
task_group.lte_clauses.each do |match|
96+
it "sends the request and the '#{match['lte']}' field is less than or equal to the expected value" do
97+
expect(task_group.response).to match_lte_field(match['lte'], test)
98+
end
99+
end
100+
end
101+
102+
# 'lt' is in the task group definition
103+
if task_group.has_lt_clauses?
104+
task_group.lt_clauses.each do |match|
105+
it "sends the request and the '#{match['lt']}' field is less than the expected value" do
106+
expect(task_group.response).to match_lt_field(match['lt'], test)
107+
end
108+
end
109+
end
110+
end
111+
end

0 commit comments

Comments
 (0)