Module: Caoutsearch::Testing::MockRequests

Defined in:
lib/caoutsearch/testing/mock_requests.rb

Instance Method Summary collapse

Instance Method Details

#stub_elasticsearch_batching_requests(index_name, hits = [], keep_alive: "1m", batch_size: 1000) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/caoutsearch/testing/mock_requests.rb', line 66

def stub_elasticsearch_batching_requests(index_name, hits = [], keep_alive: "1m", batch_size: 1000)
  pid_id = SecureRandom.base64

  stub_elasticsearch_request(:post, "#{index_name}/_pit?keep_alive=#{keep_alive}")
    .to_return_json(body: {id: pid_id})

  stub_elasticsearch_request(:delete, "_pit")
    .with(body: {id: pid_id})
    .to_return_json(body: {succeed: true})

  search_request = stub_elasticsearch_request(:post, "_search")
    .with { |request| request.body.include?(pid_id) }

  hits.each_slice(batch_size).each_with_index do |slice, index|
    total = index.zero? ? {value: hits.size} : {value: slice.size, relation: "gte"}

    search_request.to_return_json(body: {
      hits: {total: total, hits: slice},
      pit_id: pid_id
    })
  end

  search_request
end

#stub_elasticsearch_reindex_request(index_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/caoutsearch/testing/mock_requests.rb', line 50

def stub_elasticsearch_reindex_request(index_name)
  stub_elasticsearch_request(:post, "#{index_name}/_bulk").to_return_json(body: {
    "took" => 100,
    "items" => [],
    "errors" => false
  })

  stub_elasticsearch_request(:post, "#{index_name}/_refresh").to_return_json(body: {
    "_shards" => {
      "total" => 1,
      "failed" => 0,
      "successful" => 1
    }
  })
end

#stub_elasticsearch_request(verb, pattern) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/caoutsearch/testing/mock_requests.rb', line 9

def stub_elasticsearch_request(verb, pattern)
  stub_elasticsearch_validation_request

  case pattern
  when String
    pattern = URI.join(elasticsearch_client_host, pattern).to_s
  when Regexp
    pattern = URI.join(elasticsearch_client_host, pattern.source).to_s
    pattern = Regexp.new(pattern)
  else
    raise TypeError, "wrong type received for URL pattern"
  end

  stub_request(verb, pattern)
end

#stub_elasticsearch_search_request(index_name, hits, sources: true, total: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/caoutsearch/testing/mock_requests.rb', line 25

def stub_elasticsearch_search_request(index_name, hits, sources: true, total: nil)
  hits = hits.map.each_with_index do |item, index|
    hit = {}
    hit = item if item.is_a?(Hash)
    hit = yield(item) if block_given?
    hit["_index"] ||= index_name
    hit["_id"] ||= item.respond_to?(:id) ? item.id.to_s : (index + 1).to_s
    hit["_score"] ||= 1
    hit["_source"] ||= (sources && item.respond_to?(:as_indexed_json)) ? item.as_indexed_json : {}
    hit
  end

  total ||= hits.size
  total = {"value" => total} if total.is_a?(Numeric)

  stub_elasticsearch_request(:post, "#{index_name}/_search").to_return_json(body: {
    "took" => 10,
    "hits" => {
      "total" => total,
      "max_score" => hits.max { |hit| hit["_score"] },
      "hits" => hits
    }
  })
end