Skip to content

Commit a445ba4

Browse files
committed
Updates elasticsearch helper integration tests
1 parent b4b0bb0 commit a445ba4

File tree

5 files changed

+57
-79
lines changed

5 files changed

+57
-79
lines changed

elasticsearch/spec/integration/client_integration_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
context 'Reports the right meta header' do
4949
it 'Reports es service name and gem version' do
5050
headers = CLIENT.transport.connections.first.connection.headers
51-
version = Class.new.extend(Elastic::Transport::MetaHeader).send(:CLIENT_meta_version, Elasticsearch::VERSION)
51+
version = Class.new.extend(Elastic::Transport::MetaHeader).send(:client_meta_version, Elasticsearch::VERSION)
5252
expect(headers['x-elastic-client-meta']).to match /^es=#{version}/
5353
end
5454
end

elasticsearch/spec/integration/helpers/bulk_helper_spec.rb

+20-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
require_relative 'helpers_spec_helper'
17+
require_relative File.expand_path('../../spec_helper', __dir__)
1818
require 'elasticsearch/helpers/bulk_helper'
1919
require 'tempfile'
2020

@@ -23,25 +23,25 @@
2323
let(:index) { 'bulk_animals' }
2424
let(:index_slice) { 'bulk_animals_slice' }
2525
let(:params) { { refresh: 'wait_for' } }
26-
let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(client, index, params) }
26+
let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(CLIENT, index, params) }
2727
let(:docs) do
2828
[
29-
{ scientific_name: 'Lama guanicoe', name:'Guanaco' },
30-
{ scientific_name: 'Tayassu pecari', name:'White-lipped peccary' },
31-
{ scientific_name: 'Snycerus caffer', name:'Buffalo, african' },
32-
{ scientific_name: 'Coluber constrictor', name:'Snake, racer' },
33-
{ scientific_name: 'Thalasseus maximus', name:'Royal tern' },
34-
{ scientific_name: 'Centrocercus urophasianus', name:'Hen, sage' },
35-
{ scientific_name: 'Sitta canadensis', name:'Nuthatch, red-breasted' },
36-
{ scientific_name: 'Aegypius tracheliotus', name:'Vulture, lappet-faced' },
37-
{ scientific_name: 'Bucephala clangula', name:'Common goldeneye' },
38-
{ scientific_name: 'Felis pardalis', name:'Ocelot' }
29+
{ scientific_name: 'Lama guanicoe', name: 'Guanaco' },
30+
{ scientific_name: 'Tayassu pecari', name: 'White-lipped peccary' },
31+
{ scientific_name: 'Snycerus caffer', name: 'Buffalo, african' },
32+
{ scientific_name: 'Coluber constrictor', name: 'Snake, racer' },
33+
{ scientific_name: 'Thalasseus maximus', name: 'Royal tern' },
34+
{ scientific_name: 'Centrocercus urophasianus', name: 'Hen, sage' },
35+
{ scientific_name: 'Sitta canadensis', name: 'Nuthatch, red-breasted' },
36+
{ scientific_name: 'Aegypius tracheliotus', name: 'Vulture, lappet-faced' },
37+
{ scientific_name: 'Bucephala clangula', name: 'Common goldeneye' },
38+
{ scientific_name: 'Felis pardalis', name: 'Ocelot' }
3939
]
4040
end
4141

4242
after do
43-
client.indices.delete(index: index, ignore: 404)
44-
client.indices.delete(index: index_slice, ignore: 404)
43+
CLIENT.indices.delete(index: index, ignore: 404)
44+
CLIENT.indices.delete(index: index_slice, ignore: 404)
4545
end
4646

4747
it 'Ingests documents' do
@@ -58,9 +58,9 @@
5858
]
5959
bulk_helper.ingest(docs)
6060
# Get the ingested documents, add id and modify them to update them:
61-
animals = client.search(index: index)['hits']['hits']
61+
animals = CLIENT.search(index: index)['hits']['hits']
6262
# Add id to each doc
63-
docs = animals.map { |animal| animal['_source'].merge({'id' => animal['_id'] }) }
63+
docs = animals.map { |animal| animal['_source'].merge({ 'id' => animal['_id'] }) }
6464
docs.map { |doc| doc['scientific_name'].upcase! }
6565
response = bulk_helper.update(docs)
6666
expect(response.status).to eq(200)
@@ -73,17 +73,17 @@
7373
response = bulk_helper.delete(ids)
7474
expect(response.status).to eq 200
7575
expect(response['items'].map { |item| item['delete']['result'] }.uniq.first).to eq('deleted')
76-
expect(client.count(index: index)['count']).to eq(0)
76+
expect(CLIENT.count(index: index)['count']).to eq(0)
7777
end
7878

7979
it 'Ingests documents and yields response and docs' do
8080
slice = 2
81-
bulk_helper = Elasticsearch::Helpers::BulkHelper.new(client, index_slice, params)
82-
response = bulk_helper.ingest(docs, {slice: slice}) do |response, docs|
81+
bulk_helper = Elasticsearch::Helpers::BulkHelper.new(CLIENT, index_slice, params)
82+
bulk_helper.ingest(docs, { slice: slice }) do |response, docs|
8383
expect(response).to be_an_instance_of Elasticsearch::API::Response
8484
expect(docs.count).to eq slice
8585
end
86-
response = client.search(index: index_slice, size: 200)
86+
response = CLIENT.search(index: index_slice, size: 200)
8787
expect(response['hits']['hits'].map { |a| a['_source'].transform_keys(&:to_sym) }).to eq docs
8888
end
8989

elasticsearch/spec/integration/helpers/esql_helper_spec.rb

+31-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
require_relative 'helpers_spec_helper'
17+
require_relative File.expand_path('../../spec_helper', __dir__)
1818
require 'elasticsearch/helpers/esql_helper'
1919
require 'ipaddr'
2020

@@ -24,48 +24,55 @@
2424
let(:esql_helper) { Elasticsearch::Helpers::ESQLHelper }
2525
let(:query) do
2626
<<~ESQL
27-
FROM #{index}
28-
| EVAL duration_ms = ROUND(event.duration / 1000000.0, 1)
27+
FROM #{index}
28+
| EVAL duration_ms = ROUND(event.duration / 1000000.0, 1)
2929
ESQL
3030
end
3131

3232
before do
33-
client.indices.create(
33+
CLIENT.indices.create(
3434
index: index,
3535
body: {
3636
mappings: {
3737
properties: { 'client.ip' => { type: 'ip' }, message: { type: 'keyword' } }
3838
}
3939
}
4040
)
41-
client.bulk(
41+
CLIENT.bulk(
4242
index: index,
4343
body: [
44-
{'index': {}},
45-
{'@timestamp' => '2023-10-23T12:15:03.360Z', 'client.ip' => '172.21.2.162', message: 'Connected to 10.1.0.3', 'event.duration' => 3450233},
46-
{'index': {}},
47-
{'@timestamp' => '2023-10-23T12:27:28.948Z', 'client.ip' => '172.21.2.113', message: 'Connected to 10.1.0.2', 'event.duration' => 2764889},
48-
{'index': {}},
49-
{'@timestamp' => '2023-10-23T13:33:34.937Z', 'client.ip' => '172.21.0.5', message: 'Disconnected', 'event.duration' => 1232382},
50-
{'index': {}},
51-
{'@timestamp' => '2023-10-23T13:51:54.732Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 725448},
52-
{'index': {}},
53-
{'@timestamp' => '2023-10-23T13:52:55.015Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 8268153},
54-
{'index': {}},
55-
{'@timestamp' => '2023-10-23T13:53:55.832Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 5033755},
56-
{'index': {}},
57-
{'@timestamp' => '2023-10-23T13:55:01.543Z', 'client.ip' => '172.21.3.15', message: 'Connected to 10.1.0.1', 'event.duration' => 1756467}
44+
{ 'index': {} },
45+
{ '@timestamp' => '2023-10-23T12:15:03.360Z', 'client.ip' => '172.21.2.162', message: 'Connected to 10.1.0.3',
46+
'event.duration' => 3_450_233 },
47+
{ 'index': {} },
48+
{ '@timestamp' => '2023-10-23T12:27:28.948Z', 'client.ip' => '172.21.2.113', message: 'Connected to 10.1.0.2',
49+
'event.duration' => 2_764_889 },
50+
{ 'index': {} },
51+
{ '@timestamp' => '2023-10-23T13:33:34.937Z', 'client.ip' => '172.21.0.5', message: 'Disconnected',
52+
'event.duration' => 1_232_382 },
53+
{ 'index': {} },
54+
{ '@timestamp' => '2023-10-23T13:51:54.732Z', 'client.ip' => '172.21.3.15', message: 'Connection error',
55+
'event.duration' => 725_448 },
56+
{ 'index': {} },
57+
{ '@timestamp' => '2023-10-23T13:52:55.015Z', 'client.ip' => '172.21.3.15', message: 'Connection error',
58+
'event.duration' => 8_268_153 },
59+
{ 'index': {} },
60+
{ '@timestamp' => '2023-10-23T13:53:55.832Z', 'client.ip' => '172.21.3.15', message: 'Connection error',
61+
'event.duration' => 5_033_755 },
62+
{ 'index': {} },
63+
{ '@timestamp' => '2023-10-23T13:55:01.543Z', 'client.ip' => '172.21.3.15', message: 'Connected to 10.1.0.1',
64+
'event.duration' => 1_756_467 }
5865
],
5966
refresh: true
6067
)
6168
end
6269

6370
after do
64-
client.indices.delete(index: index)
71+
CLIENT.indices.delete(index: index)
6572
end
6673

6774
it 'returns an ESQL response as a relational key/value object' do
68-
response = esql_helper.query(client, query)
75+
response = esql_helper.query(CLIENT, query)
6976
expect(response.count).to eq 7
7077
expect(response.first.keys).to eq ['duration_ms', 'message', 'event.duration', 'client.ip', '@timestamp']
7178
response.each do |r|
@@ -82,7 +89,7 @@
8289
'client.ip' => Proc.new { |i| IPAddr.new(i) },
8390
'event.duration' => Proc.new { |d| d.to_s }
8491
}
85-
response = esql_helper.query(client, query, parser: parser)
92+
response = esql_helper.query(CLIENT, query, parser: parser)
8693
response.each do |r|
8794
expect(r['@timestamp']).to be_a DateTime
8895
expect(r['client.ip']).to be_a IPAddr
@@ -92,7 +99,7 @@
9299
end
93100

94101
it 'parser does not error when value is nil, leaves nil' do
95-
client.index(
102+
CLIENT.index(
96103
index: index,
97104
body: {
98105
'@timestamp' => nil,
@@ -107,7 +114,7 @@
107114
'client.ip' => Proc.new { |i| IPAddr.new(i) },
108115
'event.duration' => Proc.new { |d| d.to_s }
109116
}
110-
response = esql_helper.query(client, query, parser: parser)
117+
response = esql_helper.query(CLIENT, query, parser: parser)
111118
response.each do |r|
112119
expect [DateTime, NilClass].include?(r['@timestamp'].class)
113120
expect [IPAddr, NilClass].include?(r['client.ip'].class)

elasticsearch/spec/integration/helpers/helpers_spec_helper.rb

-29
This file was deleted.

elasticsearch/spec/integration/helpers/scroll_helper_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
require_relative 'helpers_spec_helper'
17+
require_relative File.expand_path('../../spec_helper', __dir__)
1818
require 'elasticsearch/helpers/scroll_helper'
1919

2020
context 'Elasticsearch client helpers' do
2121
context 'ScrollHelper' do
2222
let(:index) { 'books' }
2323
let(:body) { { size: 12, query: { match_all: {} } } }
24-
let(:scroll_helper) { Elasticsearch::Helpers::ScrollHelper.new(client, index, body) }
24+
let(:scroll_helper) { Elasticsearch::Helpers::ScrollHelper.new(CLIENT, index, body) }
2525

2626
before do
2727
documents = [
@@ -50,11 +50,11 @@
5050
{ index: { _index: index, data: {name: "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304} } },
5151
{ index: { _index: index, data: {name: "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288 } } }
5252
]
53-
client.bulk(body: documents, refresh: 'wait_for')
53+
CLIENT.bulk(body: documents, refresh: 'wait_for')
5454
end
5555

5656
after do
57-
client.indices.delete(index: index)
57+
CLIENT.indices.delete(index: index)
5858
end
5959

6060
it 'instantiates a scroll helper' do
@@ -63,7 +63,7 @@
6363

6464
it 'searches an index' do
6565
my_documents = []
66-
while !(documents = scroll_helper.results).empty?
66+
until (documents = scroll_helper.results).empty?
6767
my_documents << documents
6868
end
6969

0 commit comments

Comments
 (0)