From 3816fd744f3e198b980c0a747ed641f67850af77 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 20 Jun 2020 05:50:49 -0700 Subject: [PATCH] Moved tests --- test/index_test.rb | 64 ---------------------------------------------------------------- test/model_test.rb | 19 ------------------- test/reindex_test.rb | 10 ++++++++++ test/search_test.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 83 deletions(-) delete mode 100644 test/model_test.rb create mode 100644 test/search_test.rb diff --git a/test/index_test.rb b/test/index_test.rb index ff72f95..55bd7d0 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -55,26 +55,6 @@ class IndexTest < Minitest::Test assert_equal "text", mapping["properties"]["name"]["type"] end - def test_body - store_names ["Dollar Tree"], Store - assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name) - end - - def test_body_incompatible_options - assert_raises(ArgumentError) do - Store.search(body: {query: {match: {name: "dollar"}}}, where: {id: 1}) - end - end - - def test_block - store_names ["Dollar Tree"] - products = - Product.search "boom" do |body| - body[:query] = {match_all: {}} - end - assert_equal ["Dollar Tree"], products.map(&:name) - end - def test_tokens assert_equal ["dollar", "dollartre", "tree"], Product.searchkick_index.tokens("Dollar Tree", analyzer: "searchkick_index") end @@ -83,25 +63,6 @@ class IndexTest < Minitest::Test assert_equal ["dollar", "tree"], Product.searchkick_index.tokens("Dollar Tree", analyzer: "searchkick_search2") end - def test_record_not_found - store_names ["Product A", "Product B"] - Product.where(name: "Product A").delete_all - assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do - assert_search "product", ["Product B"] - end - ensure - Product.reindex - end - - def test_bad_mapping - Product.searchkick_index.delete - store_names ["Product A"] - error = assert_raises(Searchkick::InvalidQueryError) { Product.search "test" } - assert_equal "Bad mapping - run Product.reindex", error.message - ensure - Product.reindex - end - def test_remove_blank_id store_names ["Product A"] Product.searchkick_index.remove(Product.new) @@ -110,31 +71,6 @@ class IndexTest < Minitest::Test Product.reindex end - def test_missing_index - assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found") } - end - - def test_unsupported_version - raises_exception = ->(_) { raise Elasticsearch::Transport::Transport::Error, "[500] No query registered for [multi_match]" } - Searchkick.client.stub :search, raises_exception do - assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test") } - end - end - - def test_invalid_body - assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}) } - end - - def test_transaction - skip unless activerecord? - - Product.transaction do - store_names ["Product A"] - raise ActiveRecord::Rollback - end - assert_search "*", [] - end - def test_filterable # skip for 5.0 since it throws # Cannot search on field [alt_description] since it is not indexed. diff --git a/test/model_test.rb b/test/model_test.rb deleted file mode 100644 index 33fe690..0000000 --- a/test/model_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require_relative "test_helper" - -class ModelTest < Minitest::Test - def test_search_relation - _, stderr = capture_io { Product.search("*") } - assert_equal "", stderr - _, stderr = capture_io { Product.all.search("*") } - assert_match "WARNING", stderr - end - - def test_search_relation_default_scope - Band.reindex - - _, stderr = capture_io { Band.search("*") } - assert_equal "", stderr - _, stderr = capture_io { Band.all.search("*") } - assert_match "WARNING", stderr - end -end diff --git a/test/reindex_test.rb b/test/reindex_test.rb index d6bba9b..b5ef5e1 100644 --- a/test/reindex_test.rb +++ b/test/reindex_test.rb @@ -194,4 +194,14 @@ class ReindexTest < Minitest::Test assert_search "product", ["Product A", "Product C"], load: false assert_equal 0, reindex_queue.length end + + def test_transaction + skip unless activerecord? + + Product.transaction do + store_names ["Product A"] + raise ActiveRecord::Rollback + end + assert_search "*", [] + end end diff --git a/test/search_test.rb b/test/search_test.rb new file mode 100644 index 0000000..1305a96 --- /dev/null +++ b/test/search_test.rb @@ -0,0 +1,73 @@ +require_relative "test_helper" + +class SearchTest < Minitest::Test + def test_search_relation + _, stderr = capture_io { Product.search("*") } + assert_equal "", stderr + _, stderr = capture_io { Product.all.search("*") } + assert_match "WARNING", stderr + end + + def test_search_relation_default_scope + Band.reindex + + _, stderr = capture_io { Band.search("*") } + assert_equal "", stderr + _, stderr = capture_io { Band.all.search("*") } + assert_match "WARNING", stderr + end + + def test_body + store_names ["Dollar Tree"], Store + assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name) + end + + def test_body_incompatible_options + assert_raises(ArgumentError) do + Store.search(body: {query: {match: {name: "dollar"}}}, where: {id: 1}) + end + end + + def test_block + store_names ["Dollar Tree"] + products = + Product.search "boom" do |body| + body[:query] = {match_all: {}} + end + assert_equal ["Dollar Tree"], products.map(&:name) + end + + def test_record_not_found + store_names ["Product A", "Product B"] + Product.where(name: "Product A").delete_all + assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do + assert_search "product", ["Product B"] + end + ensure + Product.reindex + end + + def test_bad_mapping + Product.searchkick_index.delete + store_names ["Product A"] + error = assert_raises(Searchkick::InvalidQueryError) { Product.search "test" } + assert_equal "Bad mapping - run Product.reindex", error.message + ensure + Product.reindex + end + + def test_missing_index + assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found") } + end + + def test_unsupported_version + raises_exception = ->(_) { raise Elasticsearch::Transport::Transport::Error, "[500] No query registered for [multi_match]" } + Searchkick.client.stub :search, raises_exception do + assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test") } + end + end + + def test_invalid_body + assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}) } + end +end -- libgit2 0.21.0