test_helper.rb
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "bundler/setup"
Bundler.require(:default)
require "minitest/autorun"
require "minitest/pride"
ENV["RACK_ENV"] = "test"
File.delete("elasticsearch.log") if File.exists?("elasticsearch.log")
Tire.configure do
logger "elasticsearch.log", :level => "debug"
pretty true
end
if ENV["MONGOID"]
Mongoid.configure do |config|
config.connect_to "searchkick_test"
end
class Product
include Mongoid::Document
# include Mongoid::Attributes::Dynamic
end
class Store
include Mongoid::Document
end
else
require "active_record"
# for debugging
# ActiveRecord::Base.logger = Logger.new(STDOUT)
# rails does this in activerecord/lib/active_record/railtie.rb
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.time_zone_aware_attributes = true
# migrations
ActiveRecord::Base.establish_connection :adapter => "postgresql", :database => "searchkick_test"
ActiveRecord::Migration.create_table :products, :force => true do |t|
t.string :name
t.integer :store_id
t.boolean :in_stock
t.boolean :backordered
t.integer :orders_count
t.string :color
t.timestamps
end
ActiveRecord::Migration.create_table :store, :force => true do |t|
end
class Product < ActiveRecord::Base
end
class Store < ActiveRecord::Base
end
end
class Product
belongs_to :store
searchkick \
settings: {
number_of_shards: 1,
number_of_replicas: 0
},
synonyms: [
["clorox", "bleach"],
["scallion", "greenonion"],
["saranwrap", "plasticwrap"],
["qtip", "cottonswab"],
["burger", "hamburger"],
["bandaid", "bandag"]
],
autocomplete: [:name],
suggest: [:name, :color],
conversions: "conversions",
personalize: "user_ids"
attr_accessor :conversions, :user_ids
def search_data
as_json(root: false).merge conversions: conversions, user_ids: user_ids
end
end
Product.tire.index.delete if Product.tire.index.exists?
Product.reindex
Product.reindex # run twice for both index paths
class MiniTest::Unit::TestCase
def setup
Product.destroy_all
end
protected
def store(documents)
documents.shuffle.each do |document|
Product.create!(document)
end
Product.tire.index.refresh
end
def store_names(names)
store names.map{|name| {name: name} }
end
# no order
def assert_search(term, expected, options = {})
assert_equal expected.sort, Product.search(term, options).map(&:name).sort
end
def assert_order(term, expected, options = {})
assert_equal expected, Product.search(term, options).map(&:name)
end
def assert_first(term, expected, options = {})
assert_equal expected, Product.search(term, options).map(&:name).first
end
end