Skip to content

Commit f56b5e8

Browse files
committed
Add unit test for perform request args
1 parent 5476287 commit f56b5e8

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
require 'spec_helper'
19+
require 'elastic-transport'
20+
require_relative '../../../utils/endpoint_spec'
21+
require_relative '../../../utils/thor/generator/files_helper'
22+
23+
describe 'Perform request args' do
24+
Elasticsearch::API::FilesHelper.files.each do |filepath|
25+
spec = Elasticsearch::API::EndpointSpec.new(filepath)
26+
next if spec.module_namespace.flatten.first == '_internal'
27+
28+
# These are the path parts defined by the user in the method argument
29+
defined_path_parts = spec.path_params.inject({}) do |params, part|
30+
params.merge(part => 'testing')
31+
end
32+
33+
# These are the required params, we must pass them to the method even when testing
34+
required_params = spec.required_parts.inject({}) do |params, part|
35+
params.merge(part.to_sym => 'testing')
36+
end
37+
38+
let(:client_double) do
39+
Class.new { include Elasticsearch::API }.new.tap do |client|
40+
expect(client).to receive(:perform_request) do |_, _, _, _, _, request_params|
41+
# Check that the expected hash is passed to the perform_request method
42+
expect(request_params).to eq(expected_perform_request_params)
43+
end.and_return(response_double)
44+
end
45+
end
46+
47+
let(:response_double) do
48+
double('response', status: 200, body: {}, headers: {})
49+
end
50+
51+
context("'#{spec.endpoint_name}'") do
52+
# The expected hash passed to perform_request contains the endpoint name and any defined path parts
53+
let(:expected_perform_request_params) do
54+
if defined_path_parts.empty?
55+
{ endpoint: spec.endpoint_name }
56+
else
57+
{ endpoint: spec.endpoint_name, defined_params: defined_path_parts}
58+
end
59+
end
60+
61+
if spec.path_parts.empty?
62+
it "passes the endpoint id to the request" do
63+
if spec.module_namespace.empty?
64+
client_double.send(spec.method_name, required_params)
65+
else
66+
client_double.send(spec.module_namespace[0]).send(spec.method_name, required_params)
67+
end
68+
end
69+
else
70+
it "passes params to the request with the endpoint id: #{spec.path_parts.keys}" do
71+
if spec.module_namespace.empty?
72+
client_double.send(spec.method_name, required_params.merge(defined_path_parts))
73+
else
74+
client_double.send(
75+
spec.module_namespace[0]).send(spec.method_name, required_params.merge(defined_path_parts)
76+
)
77+
end
78+
end
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)