|
18 | 18 | require 'spec_helper' |
19 | 19 | require 'uri' |
20 | 20 |
|
21 | | -ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}" |
22 | | -raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/ |
23 | | - |
24 | | -context 'Elasticsearch client' do |
25 | | - let(:client) do |
26 | | - Elasticsearch::Client.new(host: ELASTICSEARCH_URL, user: 'elastic', password: 'changeme') |
27 | | - end |
| 21 | +context 'Elasticsearch CLIENT' do |
28 | 22 | let(:index) { 'tvs' } |
29 | 23 |
|
30 | 24 | after do |
31 | | - client.indices.delete(index: index) |
| 25 | + CLIENT.indices.delete(index: index) |
32 | 26 | end |
33 | 27 |
|
34 | 28 | context 'escaping spaces in ids' do |
35 | 29 | it 'escapes spaces for id when using index' do |
36 | | - response = client.index(index: index, id: 'a test 1', body: { name: 'A test 1' }, refresh: true) |
| 30 | + response = CLIENT.index(index: index, id: 'a test 1', body: { name: 'A test 1' }, refresh: true) |
37 | 31 | expect(response.body['_id']).to eq 'a test 1' |
38 | 32 |
|
39 | | - response = client.search(index: index) |
| 33 | + response = CLIENT.search(index: index) |
40 | 34 | expect(response.body['hits']['hits'].first['_id']).to eq 'a test 1' |
41 | 35 |
|
42 | 36 | # Raises exception, _id is unrecognized |
43 | 37 | expect do |
44 | | - client.index(index: index, _id: 'a test 2', body: { name: 'A test 2' }) |
| 38 | + CLIENT.index(index: index, _id: 'a test 2', body: { name: 'A test 2' }) |
45 | 39 | end.to raise_exception Elastic::Transport::Transport::Errors::BadRequest |
46 | 40 |
|
47 | 41 | # Raises exception, id is a query parameter |
48 | 42 | expect do |
49 | | - client.index(index: index, body: { name: 'A test 3', _id: 'a test 3' }) |
| 43 | + CLIENT.index(index: index, body: { name: 'A test 3', _id: 'a test 3' }) |
50 | 44 | end.to raise_exception Elastic::Transport::Transport::Errors::BadRequest |
51 | 45 | end |
52 | 46 |
|
53 | 47 | it 'escapes spaces for id when using create' do |
54 | 48 | # Works with create |
55 | | - response = client.create(index: index, id: 'a test 4', body: { name: 'A test 4' }) |
| 49 | + response = CLIENT.create(index: index, id: 'a test 4', body: { name: 'A test 4' }) |
56 | 50 | expect(response.body['_id']).to eq 'a test 4' |
57 | 51 | end |
58 | 52 |
|
59 | 53 | it 'escapes spaces for id when using bulk' do |
60 | 54 | body = [ |
61 | 55 | { create: { _index: index, _id: 'a test 5', data: { name: 'A test 5' } } } |
62 | 56 | ] |
63 | | - expect(client.bulk(body: body, refresh: true).status).to eq 200 |
| 57 | + expect(CLIENT.bulk(body: body, refresh: true).status).to eq 200 |
64 | 58 |
|
65 | | - response = client.search(index: index) |
| 59 | + response = CLIENT.search(index: index) |
66 | 60 | expect( |
67 | 61 | response.body['hits']['hits'].select { |a| a['_id'] == 'a test 5' }.size |
68 | 62 | ).to eq 1 |
|
71 | 65 |
|
72 | 66 | context 'it doesnae escape plus signs in id' do |
73 | 67 | it 'escapes spaces for id when using index' do |
74 | | - response = client.index(index: index, id: 'a+test+1', body: { name: 'A test 1' }) |
| 68 | + response = CLIENT.index(index: index, id: 'a+test+1', body: { name: 'A test 1' }) |
75 | 69 | expect(response.body['_id']).to eq 'a+test+1' |
76 | 70 | end |
77 | 71 |
|
78 | 72 | it 'escapes spaces for id when using create' do |
79 | 73 | # Works with create |
80 | | - response = client.create(index: index, id: 'a+test+2', body: { name: 'A test 2' }) |
| 74 | + response = CLIENT.create(index: index, id: 'a+test+2', body: { name: 'A test 2' }) |
81 | 75 | expect(response.body['_id']).to eq 'a+test+2' |
82 | 76 | end |
83 | 77 |
|
84 | 78 | it 'escapes spaces for id when using bulk' do |
85 | 79 | body = [ |
86 | 80 | { create: { _index: index, _id: 'a+test+3', data: { name: 'A test 3' } } } |
87 | 81 | ] |
88 | | - expect(client.bulk(body: body, refresh: true).status).to eq 200 |
| 82 | + expect(CLIENT.bulk(body: body, refresh: true).status).to eq 200 |
89 | 83 |
|
90 | | - response = client.search(index: index) |
| 84 | + response = CLIENT.search(index: index) |
91 | 85 | expect( |
92 | 86 | response.body['hits']['hits'].select { |a| a['_id'] == 'a+test+3' }.size |
93 | 87 | ).to eq 1 |
|
0 commit comments