Skip to content

Commit

Permalink
Merge pull request #12 from solutions-territoire/feature-delete_by_query
Browse files Browse the repository at this point in the history
Add few tests about Search#delete_documents
  • Loading branch information
inkstak committed Feb 21, 2024
2 parents 86c479a + d3c4de2 commit a0824bc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/caoutsearch/config/mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def remote_mappings
protected

def default_mappings
path = ::Rails.root.join("config/elasticsearch/#{index_name}.json")
if defined?(Rails)
path = ::Rails.root.join("config/elasticsearch/#{index_name}.json")
else
raise NotImplementedError, "Mappings files cannot be found out of a Rails application"
end
raise ArgumentError, "No mappings file found for #{index_name} at #{path}" unless path.exist?

MultiJson.load(path.read)
Expand Down
44 changes: 44 additions & 0 deletions spec/caoutsearch/search/delete_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Caoutsearch::Search::DeleteMethods do
let!(:search_class) do
stub_search_class("SampleSearch") do
self.mappings = {
properties: {
tag: {type: "keyword"}
}
}

filter :tag
end
end

let!(:stubbed_request) do
stub_elasticsearch_request(:post, "samples/_delete_by_query")
.with(body: {query: {bool: {filter: [{term: {tag: "discarded"}}]}}})
.to_return_json(body: {
"took" => 147,
"timed_out" => false,
"total" => 119,
"deleted" => 119,
"batches" => 1,
"noops" => 0,
"retries" => {},
"failures" => []
})
end

it "delete all indexed document by query" do
search = search_class.new.search(tag: "discarded")
response = search.delete_documents

aggregate_failures do
expect(stubbed_request).to have_been_requested.once
expect(response).to be_a(Elasticsearch::API::Response)
expect(response.to_h).to include("took" => 147, "total" => 119, "deleted" => 119)
expect(search.loaded?).to be(false)
end
end
end
31 changes: 31 additions & 0 deletions spec/caoutsearch/search/internal_dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Caoutsearch::Search::InternalDSL do
let!(:search_class) do
stub_search_class("SampleSearch") do
self.mappings = {
properties: {
tag: {type: "keyword"}
}
}

filter :tag
end
end

it "builds a query base on criteria and mapping" do
search = search_class.new.search(tag: "discarded")

expect(search.build.to_h).to eq({
query: {
bool: {
filter: [
{term: {tag: "discarded"}}
]
}
}
})
end
end

0 comments on commit a0824bc

Please sign in to comment.