Skip to content

Commit 297fe86

Browse files
committed
Refactors perform_request_spec, removes dependency on outdated generator code.
1 parent 30282b5 commit 297fe86

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

elasticsearch-api/spec/unit/perform_request_spec.rb

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,55 @@
1818
require 'elastic-transport'
1919
require 'spec_helper'
2020

21-
require_relative File.expand_path('../../utils/thor/endpoint_spec', __dir__)
22-
require_relative File.expand_path('../../utils/thor/generator/files_helper', __dir__)
21+
# Helper Class to replace the EndpointSpec class from the old code generator. The created object
22+
# will store some relevant data to the endpoint specification so that it can be used to test
23+
# OpenTelemetry.
24+
class EndpointSpec
25+
attr_reader :module_namespace,
26+
:method_name,
27+
:endpoint_name,
28+
:visibility
29+
30+
def initialize(filepath)
31+
@path = Pathname(filepath)
32+
json = MultiJson.load(File.read(@path))
33+
@endpoint_name = json.keys.first
34+
35+
full_namespace = parse_full_namespace
36+
@namespace_depth = full_namespace.size.positive? ? full_namespace.size - 1 : 0
37+
@module_namespace = full_namespace[0, @namespace_depth]
38+
@visivility = json.values.first['visibility']
39+
end
40+
41+
def parse_full_namespace
42+
names = @endpoint_name.split('.')
43+
# Return an array to expand 'ccr', 'ilm', 'ml' and 'slm'
44+
names.map do |name|
45+
name
46+
.gsub(/^ml$/, 'machine_learning')
47+
.gsub(/^ilm$/, 'index_lifecycle_management')
48+
.gsub(/^ccr/, 'cross_cluster_replication')
49+
.gsub(/^slm/, 'snapshot_lifecycle_management')
50+
end
51+
end
52+
end
53+
54+
# JSON spec files to test
55+
# This is a helper which used to be in the FilesHelper module in the old code generator. It goes
56+
# through the files in the Elasticsearch JSON specification to see which methods need to be tested
57+
# and how.
58+
def files
59+
src_path = File.expand_path('../../../tmp/rest-api-spec/api/', __dir__)
60+
61+
Dir.entries(src_path).reject do |file|
62+
File.extname(file) != '.json' ||
63+
File.basename(file) == '_common.json'
64+
end.map { |file| "#{src_path}/#{file}" }
65+
end
2366

2467
describe 'Perform request args' do
25-
Elasticsearch::API::FilesHelper.files.each do |filepath|
26-
spec = Elasticsearch::API::EndpointSpec.new(filepath)
68+
files.each do |filepath|
69+
spec = EndpointSpec.new(filepath)
2770
next if spec.module_namespace.flatten.first == '_internal' ||
2871
spec.visibility != 'public' ||
2972
# TODO: Once the test suite is migrated to elasticsearch-specification, these should be removed

0 commit comments

Comments
 (0)