From d3c4de2c804fd3658c8de8a997ed566d7c4e6236 Mon Sep 17 00:00:00 2001 From: Sebastien Savater Date: Wed, 21 Feb 2024 15:22:22 +0100 Subject: [PATCH] Add few tests about Search#delete_documents --- lib/caoutsearch/config/mappings.rb | 6 ++- .../caoutsearch/search/delete_methods_spec.rb | 44 +++++++++++++++++++ spec/caoutsearch/search/internal_dsl_spec.rb | 31 +++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 spec/caoutsearch/search/delete_methods_spec.rb create mode 100644 spec/caoutsearch/search/internal_dsl_spec.rb diff --git a/lib/caoutsearch/config/mappings.rb b/lib/caoutsearch/config/mappings.rb index ca293f2..9f6bf63 100644 --- a/lib/caoutsearch/config/mappings.rb +++ b/lib/caoutsearch/config/mappings.rb @@ -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) diff --git a/spec/caoutsearch/search/delete_methods_spec.rb b/spec/caoutsearch/search/delete_methods_spec.rb new file mode 100644 index 0000000..d70afd8 --- /dev/null +++ b/spec/caoutsearch/search/delete_methods_spec.rb @@ -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 diff --git a/spec/caoutsearch/search/internal_dsl_spec.rb b/spec/caoutsearch/search/internal_dsl_spec.rb new file mode 100644 index 0000000..8263882 --- /dev/null +++ b/spec/caoutsearch/search/internal_dsl_spec.rb @@ -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