|
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. |
| 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 |
17 | 8 | #
|
18 |
| -# require 'spec_helper' |
19 |
| -# require 'elastic-transport' |
20 |
| -# require_relative '../../../utils/endpoint_spec' |
21 |
| -# require_relative '../../../utils/thor/generator/files_helper' |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
22 | 10 | #
|
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 |
| -# # The create method ends up becoming an 'index' request |
42 |
| -# if expected_perform_request_params[:endpoint] == 'create' |
43 |
| -# expected_perform_request_params[:endpoint] = 'index' |
44 |
| -# end |
45 |
| -# # Check that the expected hash is passed to the perform_request method |
46 |
| -# expect(request_params).to eq(expected_perform_request_params) |
47 |
| -# end.and_return(response_double) |
48 |
| -# end |
49 |
| -# end |
50 |
| -# |
51 |
| -# let(:response_double) do |
52 |
| -# double('response', status: 200, body: {}, headers: {}) |
53 |
| -# end |
54 |
| -# |
55 |
| -# context("'#{spec.endpoint_name}'") do |
56 |
| -# # The expected hash passed to perform_request contains the endpoint name and any defined path parts |
57 |
| -# let(:expected_perform_request_params) do |
58 |
| -# if defined_path_parts.empty? |
59 |
| -# { endpoint: spec.endpoint_name } |
60 |
| -# else |
61 |
| -# { endpoint: spec.endpoint_name, defined_params: defined_path_parts} |
62 |
| -# end |
63 |
| -# end |
64 |
| -# |
65 |
| -# if spec.path_parts.empty? |
66 |
| -# it "passes the endpoint id to the request" do |
67 |
| -# if spec.module_namespace.empty? |
68 |
| -# client_double.send(spec.method_name, required_params) |
69 |
| -# else |
70 |
| -# client_double.send(spec.module_namespace[0]).send(spec.method_name, required_params) |
71 |
| -# end |
72 |
| -# end |
73 |
| -# else |
74 |
| -# it "passes params to the request with the endpoint id: #{spec.path_parts.keys}" do |
75 |
| -# if spec.module_namespace.empty? |
76 |
| -# client_double.send(spec.method_name, required_params.merge(defined_path_parts)) |
77 |
| -# else |
78 |
| -# client_double.send( |
79 |
| -# spec.module_namespace[0]).send(spec.method_name, required_params.merge(defined_path_parts) |
80 |
| -# ) |
81 |
| -# end |
82 |
| -# end |
83 |
| -# end |
84 |
| -# end |
85 |
| -# end |
86 |
| -# end |
| 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 | + # The create method ends up becoming an 'index' request |
| 42 | + if expected_perform_request_params[:endpoint] == 'create' |
| 43 | + expected_perform_request_params[:endpoint] = 'index' |
| 44 | + end |
| 45 | + # Check that the expected hash is passed to the perform_request method |
| 46 | + expect(request_params).to eq(expected_perform_request_params) |
| 47 | + end.and_return(response_double) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + let(:response_double) do |
| 52 | + double('response', status: 200, body: {}, headers: {}) |
| 53 | + end |
| 54 | + |
| 55 | + context("'#{spec.endpoint_name}'") do |
| 56 | + # The expected hash passed to perform_request contains the endpoint name and any defined path parts |
| 57 | + let(:expected_perform_request_params) do |
| 58 | + if defined_path_parts.empty? |
| 59 | + { endpoint: spec.endpoint_name } |
| 60 | + else |
| 61 | + { endpoint: spec.endpoint_name, defined_params: defined_path_parts} |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + if spec.path_parts.empty? |
| 66 | + it "passes the endpoint id to the request" do |
| 67 | + if spec.module_namespace.empty? |
| 68 | + client_double.send(spec.method_name, required_params) |
| 69 | + else |
| 70 | + client_double.send(spec.module_namespace[0]).send(spec.method_name, required_params) |
| 71 | + end |
| 72 | + end |
| 73 | + else |
| 74 | + it "passes params to the request with the endpoint id: #{spec.path_parts.keys}" do |
| 75 | + if spec.module_namespace.empty? |
| 76 | + client_double.send(spec.method_name, required_params.merge(defined_path_parts)) |
| 77 | + else |
| 78 | + client_double.send( |
| 79 | + spec.module_namespace[0]).send(spec.method_name, required_params.merge(defined_path_parts) |
| 80 | + ) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments