Commit 3816fd744f3e198b980c0a747ed641f67850af77

Authored by Andrew Kane
1 parent 476ea1ba

Moved tests

test/index_test.rb
... ... @@ -55,26 +55,6 @@ class IndexTest < Minitest::Test
55 55 assert_equal "text", mapping["properties"]["name"]["type"]
56 56 end
57 57  
58   - def test_body
59   - store_names ["Dollar Tree"], Store
60   - assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name)
61   - end
62   -
63   - def test_body_incompatible_options
64   - assert_raises(ArgumentError) do
65   - Store.search(body: {query: {match: {name: "dollar"}}}, where: {id: 1})
66   - end
67   - end
68   -
69   - def test_block
70   - store_names ["Dollar Tree"]
71   - products =
72   - Product.search "boom" do |body|
73   - body[:query] = {match_all: {}}
74   - end
75   - assert_equal ["Dollar Tree"], products.map(&:name)
76   - end
77   -
78 58 def test_tokens
79 59 assert_equal ["dollar", "dollartre", "tree"], Product.searchkick_index.tokens("Dollar Tree", analyzer: "searchkick_index")
80 60 end
... ... @@ -83,25 +63,6 @@ class IndexTest < Minitest::Test
83 63 assert_equal ["dollar", "tree"], Product.searchkick_index.tokens("Dollar Tree", analyzer: "searchkick_search2")
84 64 end
85 65  
86   - def test_record_not_found
87   - store_names ["Product A", "Product B"]
88   - Product.where(name: "Product A").delete_all
89   - assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
90   - assert_search "product", ["Product B"]
91   - end
92   - ensure
93   - Product.reindex
94   - end
95   -
96   - def test_bad_mapping
97   - Product.searchkick_index.delete
98   - store_names ["Product A"]
99   - error = assert_raises(Searchkick::InvalidQueryError) { Product.search "test" }
100   - assert_equal "Bad mapping - run Product.reindex", error.message
101   - ensure
102   - Product.reindex
103   - end
104   -
105 66 def test_remove_blank_id
106 67 store_names ["Product A"]
107 68 Product.searchkick_index.remove(Product.new)
... ... @@ -110,31 +71,6 @@ class IndexTest < Minitest::Test
110 71 Product.reindex
111 72 end
112 73  
113   - def test_missing_index
114   - assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found") }
115   - end
116   -
117   - def test_unsupported_version
118   - raises_exception = ->(_) { raise Elasticsearch::Transport::Transport::Error, "[500] No query registered for [multi_match]" }
119   - Searchkick.client.stub :search, raises_exception do
120   - assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test") }
121   - end
122   - end
123   -
124   - def test_invalid_body
125   - assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}) }
126   - end
127   -
128   - def test_transaction
129   - skip unless activerecord?
130   -
131   - Product.transaction do
132   - store_names ["Product A"]
133   - raise ActiveRecord::Rollback
134   - end
135   - assert_search "*", []
136   - end
137   -
138 74 def test_filterable
139 75 # skip for 5.0 since it throws
140 76 # Cannot search on field [alt_description] since it is not indexed.
... ...
test/model_test.rb
... ... @@ -1,19 +0,0 @@
1   -require_relative "test_helper"
2   -
3   -class ModelTest < Minitest::Test
4   - def test_search_relation
5   - _, stderr = capture_io { Product.search("*") }
6   - assert_equal "", stderr
7   - _, stderr = capture_io { Product.all.search("*") }
8   - assert_match "WARNING", stderr
9   - end
10   -
11   - def test_search_relation_default_scope
12   - Band.reindex
13   -
14   - _, stderr = capture_io { Band.search("*") }
15   - assert_equal "", stderr
16   - _, stderr = capture_io { Band.all.search("*") }
17   - assert_match "WARNING", stderr
18   - end
19   -end
test/reindex_test.rb
... ... @@ -194,4 +194,14 @@ class ReindexTest &lt; Minitest::Test
194 194 assert_search "product", ["Product A", "Product C"], load: false
195 195 assert_equal 0, reindex_queue.length
196 196 end
  197 +
  198 + def test_transaction
  199 + skip unless activerecord?
  200 +
  201 + Product.transaction do
  202 + store_names ["Product A"]
  203 + raise ActiveRecord::Rollback
  204 + end
  205 + assert_search "*", []
  206 + end
197 207 end
... ...
test/search_test.rb 0 โ†’ 100644
... ... @@ -0,0 +1,73 @@
  1 +require_relative "test_helper"
  2 +
  3 +class SearchTest < Minitest::Test
  4 + def test_search_relation
  5 + _, stderr = capture_io { Product.search("*") }
  6 + assert_equal "", stderr
  7 + _, stderr = capture_io { Product.all.search("*") }
  8 + assert_match "WARNING", stderr
  9 + end
  10 +
  11 + def test_search_relation_default_scope
  12 + Band.reindex
  13 +
  14 + _, stderr = capture_io { Band.search("*") }
  15 + assert_equal "", stderr
  16 + _, stderr = capture_io { Band.all.search("*") }
  17 + assert_match "WARNING", stderr
  18 + end
  19 +
  20 + def test_body
  21 + store_names ["Dollar Tree"], Store
  22 + assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name)
  23 + end
  24 +
  25 + def test_body_incompatible_options
  26 + assert_raises(ArgumentError) do
  27 + Store.search(body: {query: {match: {name: "dollar"}}}, where: {id: 1})
  28 + end
  29 + end
  30 +
  31 + def test_block
  32 + store_names ["Dollar Tree"]
  33 + products =
  34 + Product.search "boom" do |body|
  35 + body[:query] = {match_all: {}}
  36 + end
  37 + assert_equal ["Dollar Tree"], products.map(&:name)
  38 + end
  39 +
  40 + def test_record_not_found
  41 + store_names ["Product A", "Product B"]
  42 + Product.where(name: "Product A").delete_all
  43 + assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
  44 + assert_search "product", ["Product B"]
  45 + end
  46 + ensure
  47 + Product.reindex
  48 + end
  49 +
  50 + def test_bad_mapping
  51 + Product.searchkick_index.delete
  52 + store_names ["Product A"]
  53 + error = assert_raises(Searchkick::InvalidQueryError) { Product.search "test" }
  54 + assert_equal "Bad mapping - run Product.reindex", error.message
  55 + ensure
  56 + Product.reindex
  57 + end
  58 +
  59 + def test_missing_index
  60 + assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found") }
  61 + end
  62 +
  63 + def test_unsupported_version
  64 + raises_exception = ->(_) { raise Elasticsearch::Transport::Transport::Error, "[500] No query registered for [multi_match]" }
  65 + Searchkick.client.stub :search, raises_exception do
  66 + assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test") }
  67 + end
  68 + end
  69 +
  70 + def test_invalid_body
  71 + assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}) }
  72 + end
  73 +end
... ...