Module: Elastictastic::TestHelpers
- Defined in:
- lib/elastictastic/test_helpers.rb
Constant Summary collapse
- ALPHANUM =
('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
Class Method Summary collapse
Instance Method Summary collapse
- #stub_elasticsearch_bulk(*responses) ⇒ Object
- #stub_elasticsearch_create(index, type, *args) ⇒ Object
- #stub_elasticsearch_destroy(index, type, id, options = {}) ⇒ Object
- #stub_elasticsearch_destroy_all(index, type) ⇒ Object
- #stub_elasticsearch_get(index, type, id, doc = {}) ⇒ Object
- #stub_elasticsearch_mget(index, type, *ids) ⇒ Object
- #stub_elasticsearch_put_mapping(index, type) ⇒ Object
- #stub_elasticsearch_scan(index, type, batch_size, *hits) ⇒ Object
- #stub_elasticsearch_search(index, type, data) ⇒ Object
- #stub_elasticsearch_update(index, type, id) ⇒ Object
Class Method Details
.uri_for_path(path) ⇒ Object
168 169 170 |
# File 'lib/elastictastic/test_helpers.rb', line 168 def self.uri_for_path(path) "#{Elastictastic.config.hosts.first}#{path}" end |
Instance Method Details
#stub_elasticsearch_bulk(*responses) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/elastictastic/test_helpers.rb', line 110 def stub_elasticsearch_bulk(*responses) FakeWeb.register_uri( :post, TestHelpers.uri_for_path("/_bulk"), :body => { 'took' => 1, 'items' => responses }.to_json ) end |
#stub_elasticsearch_create(index, type, *args) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/elastictastic/test_helpers.rb', line 7 def stub_elasticsearch_create(index, type, *args) = args. id = args.pop if id.nil? id = '' 22.times { id << ALPHANUM[rand(ALPHANUM.length)] } path = "/#{index}/#{type}" method = :post else path = "/#{index}/#{type}/#{id}/_create" method = :put end FakeWeb.register_uri( method, /^#{Regexp.escape(TestHelpers.uri_for_path(path))}(\?.*)?$/, .reverse_merge(:body => { 'ok' => 'true', '_index' => index, '_type' => type, '_id' => id }.to_json) ) id end |
#stub_elasticsearch_destroy(index, type, id, options = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/elastictastic/test_helpers.rb', line 87 def stub_elasticsearch_destroy(index, type, id, = {}) FakeWeb.register_uri( :delete, /^#{TestHelpers.uri_for_path("/#{index}/#{type}/#{id}")}(\?.*)?$/, .reverse_merge(:body => { 'ok' => true, 'found' => true, '_index' => 'test', '_type' => 'test', '_id' => id, '_version' => 1 }.to_json) ) end |
#stub_elasticsearch_destroy_all(index, type) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/elastictastic/test_helpers.rb', line 102 def stub_elasticsearch_destroy_all(index, type) FakeWeb.register_uri( :delete, TestHelpers.uri_for_path("/#{index}/#{type}"), :body => { 'ok' => true }.to_json ) end |
#stub_elasticsearch_get(index, type, id, doc = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/elastictastic/test_helpers.rb', line 46 def stub_elasticsearch_get(index, type, id, doc = {}) FakeWeb.register_uri( :get, /^#{Regexp.escape(TestHelpers.uri_for_path("/#{index}/#{type}/#{id}").to_s)}(\?.*)?$/, :body => { 'ok' => true, '_index' => index, '_type' => type, '_id' => id, '_source' => doc, 'exists' => !doc.nil? }.to_json ) end |
#stub_elasticsearch_mget(index, type, *ids) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/elastictastic/test_helpers.rb', line 61 def stub_elasticsearch_mget(index, type, *ids) given_ids_with_docs = ids. ids_with_docs = {} ids.each { |id| ids_with_docs[id] = {} } ids_with_docs.merge!(given_ids_with_docs) path = index ? "/#{index}/#{type}/_mget" : "/_mget" docs = ids_with_docs.each_pair.map do |id, doc| id, index = *id if Array === id { '_index' => index, '_type' => type, '_id' => id, 'exists' => !!doc, '_source' => doc } end FakeWeb.register_uri( :post, TestHelpers.uri_for_path(path).to_s, :body => { 'docs' => docs }.to_json ) end |
#stub_elasticsearch_put_mapping(index, type) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/elastictastic/test_helpers.rb', line 118 def stub_elasticsearch_put_mapping(index, type) FakeWeb.register_uri( :put, TestHelpers.uri_for_path("/#{index}/#{type}/_mapping"), :body => { 'ok' => true, 'acknowledged' => true }.to_json ) end |
#stub_elasticsearch_scan(index, type, batch_size, *hits) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/elastictastic/test_helpers.rb', line 143 def stub_elasticsearch_scan(index, type, batch_size, *hits) scan_uri = Regexp.escape(TestHelpers.uri_for_path("/#{index}/#{type}/_search").to_s) scroll_ids = Array.new(batch_size + 1) { rand(10**100).to_s(36) } FakeWeb.register_uri( :post, /^#{scan_uri}\?.*search_type=scan/, :body => { '_scroll_id' => scroll_ids.first, 'hits' => { 'total' => hits.length, 'hits' => [] } }.to_json ) batches = hits.each_slice(batch_size).each_with_index.map do |hit_batch, i| { :body => { '_scroll_id' => scroll_ids[i+1], 'hits' => { 'hits' => hit_batch }}.to_json } end batches << { :body => { 'hits' => { 'hits' => [] }}.to_json } scroll_uri = Regexp.escape(TestHelpers.uri_for_path("/_search/scroll").to_s) FakeWeb.register_uri( :post, /^#{scroll_uri}/, batches ) scroll_ids end |
#stub_elasticsearch_search(index, type, data) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/elastictastic/test_helpers.rb', line 126 def stub_elasticsearch_search(index, type, data) if Array === data response = data.map do |datum| { :body => datum.to_json } end else response = { :body => data.to_json } end uri = TestHelpers.uri_for_path("/#{index}/#{type}/_search").to_s FakeWeb.register_uri( :post, /^#{Regexp.escape(uri)}/, response ) end |
#stub_elasticsearch_update(index, type, id) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/elastictastic/test_helpers.rb', line 33 def stub_elasticsearch_update(index, type, id) FakeWeb.register_uri( :put, /^#{TestHelpers.uri_for_path("/#{index}/#{type}/#{id}")}(\?.*)?$/, :body => { 'ok' => 'true', '_index' => index, '_type' => type, '_id' => id }.to_json ) end |