search_flip 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,243 @@
1
+
2
+ require "minitest"
3
+ require "minitest/autorun"
4
+ require "webmock/minitest"
5
+ require "mocha/mini_test"
6
+ require "search_flip"
7
+ require "active_record"
8
+ require "factory_girl"
9
+ require "timecop"
10
+ require "yaml"
11
+
12
+ WebMock.allow_net_connect!
13
+
14
+ ActiveRecord::Base.establish_connection YAML.load_file(File.expand_path("../database.yml", __FILE__))
15
+
16
+ SearchFlip::Config[:auto_refresh] = true
17
+
18
+ ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS products"
19
+
20
+ ActiveRecord::Base.connection.create_table :products do |t|
21
+ t.string :title
22
+ t.text :description
23
+ t.float :price
24
+ t.string :category
25
+ t.integer :version, default: 1
26
+ t.integer :rank, :default => 0
27
+ t.integer :user_id
28
+ t.timestamps null: false
29
+ end
30
+
31
+ ActiveRecord::Base.connection.add_index :products, :user_id
32
+
33
+ class Product < ActiveRecord::Base
34
+ belongs_to :user
35
+ has_many :comments
36
+ end
37
+
38
+ FactoryGirl.define do
39
+ factory :product
40
+ end
41
+
42
+ ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS users"
43
+
44
+ ActiveRecord::Base.connection.create_table :users do |t|
45
+ t.string :name
46
+ t.timestamps null: false
47
+ end
48
+
49
+ class User < ActiveRecord::Base
50
+ has_many :products
51
+ end
52
+
53
+ FactoryGirl.define do
54
+ factory :user
55
+ end
56
+
57
+ ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS comments"
58
+
59
+ ActiveRecord::Base.connection.create_table :comments do |t|
60
+ t.text :message
61
+ t.integer :product_id
62
+ t.timestamps null: false
63
+ end
64
+
65
+ ActiveRecord::Base.connection.add_index :comments, :product_id
66
+
67
+ class Comment < ActiveRecord::Base
68
+ belongs_to :product
69
+ end
70
+
71
+ FactoryGirl.define do
72
+ factory :comment
73
+ end
74
+
75
+ class CommentIndex
76
+ include SearchFlip::Index
77
+
78
+ def self.type_name
79
+ "comments"
80
+ end
81
+
82
+ def self.model
83
+ Comment
84
+ end
85
+
86
+ def self.serialize(comment)
87
+ {
88
+ id: comment.id,
89
+ message: comment.message
90
+ }
91
+ end
92
+ end
93
+
94
+ CommentIndex.delete_index if CommentIndex.index_exists?
95
+ CommentIndex.create_index
96
+ CommentIndex.update_mapping
97
+
98
+ class ProductIndex
99
+ include SearchFlip::Index
100
+
101
+ def self.mapping
102
+ if SearchFlip.version.to_i >= 5
103
+ {
104
+ products: {
105
+ properties: {
106
+ category: {
107
+ type: "text",
108
+ fielddata: true
109
+ },
110
+ title: {
111
+ type: "text",
112
+ fielddata: true
113
+ },
114
+ description: {
115
+ type: "text",
116
+ fielddata: true
117
+ }
118
+ }
119
+ }
120
+ }
121
+ else
122
+ { products: {} }
123
+ end
124
+ end
125
+
126
+ def self.type_name
127
+ "products"
128
+ end
129
+
130
+ def self.model
131
+ Product
132
+ end
133
+
134
+ def self.serialize(product)
135
+ {
136
+ id: product.id,
137
+ title: product.title,
138
+ description: product.description,
139
+ category: product.category,
140
+ price: product.price,
141
+ rank: product.rank,
142
+ created_at: product.created_at,
143
+ updated_at: product.updated_at
144
+ }
145
+ end
146
+ end
147
+
148
+ ProductIndex.delete_index if ProductIndex.index_exists?
149
+ ProductIndex.create_index
150
+ ProductIndex.update_mapping
151
+
152
+ class TestIndex
153
+ include SearchFlip::Index
154
+
155
+ def self.mapping
156
+ {
157
+ test: {
158
+ properties: {
159
+ test_field: { type: "string" }
160
+ }
161
+ }
162
+ }
163
+ end
164
+
165
+ def self.type_name
166
+ "test"
167
+ end
168
+ end
169
+
170
+ TestIndex.delete_index if TestIndex.index_exists?
171
+
172
+ class SearchFlip::TestCase < MiniTest::Test
173
+ include FactoryGirl::Syntax::Methods
174
+
175
+ def self.should_delegate_method(method, to:, subject:, as: method)
176
+ define_method :"test_delegate_#{method}_to_#{to}" do
177
+ assert subject.respond_to?(method), "subject doesn't respond to #{method}"
178
+
179
+ target = subject.send(to)
180
+
181
+ assert target.respond_to?(as), "#{to} doesn't respond to #{as}"
182
+
183
+ params = subject.method(method).arity.abs.times.map { |i| "param-#{i}" }
184
+
185
+ mock_target = mock
186
+ mock_target.expects(as).with(*params)
187
+
188
+ subject.stubs(to).returns(mock_target)
189
+
190
+ subject.send(method, *params)
191
+ end
192
+ end
193
+
194
+ def self.should_delegate_methods(*methods, to:, subject:)
195
+ methods.each do |method|
196
+ should_delegate_method method, to: to, subject: subject
197
+ end
198
+ end
199
+
200
+ def assert_difference(expressions, difference = 1, &block)
201
+ callables = Array(expressions).map { |e| lambda { eval(e, block.binding) } }
202
+
203
+ before = callables.map(&:call)
204
+
205
+ res = yield
206
+
207
+ Array(expressions).zip(callables).each_with_index do |(code, callable), i|
208
+ assert_equal before[i] + difference, callable.call, "#{code.inspect} didn't change by #{difference}"
209
+ end
210
+
211
+ res
212
+ end
213
+
214
+ def assert_no_difference(expressions, &block)
215
+ assert_difference(expressions, 0, &block)
216
+ end
217
+
218
+ def assert_not_nil(object)
219
+ assert !object.nil?, "shouldn't be nil"
220
+ end
221
+
222
+ def assert_present(object)
223
+ assert object.present?, "should be present"
224
+ end
225
+
226
+ def assert_blank(object)
227
+ assert object.blank?, "should be blank"
228
+ end
229
+
230
+ def refute_present(object)
231
+ refute object.present?, "shouldn't be present"
232
+ end
233
+
234
+ def refute_blank(object)
235
+ refute object.blank?, "shouldn't be blank"
236
+ end
237
+
238
+ def setup
239
+ ProductIndex.match_all.delete
240
+ Product.delete_all
241
+ end
242
+ end
243
+
metadata ADDED
@@ -0,0 +1,258 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_flip
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Vetter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: timecop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: http
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: hashie
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: oj
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Compositional EasticSearch client library
182
+ email:
183
+ - vetter@flakks.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".travis.yml"
190
+ - Gemfile
191
+ - LICENSE.txt
192
+ - README.md
193
+ - Rakefile
194
+ - irb.rb
195
+ - lib/search_flip.rb
196
+ - lib/search_flip/aggregatable.rb
197
+ - lib/search_flip/aggregation.rb
198
+ - lib/search_flip/bulk.rb
199
+ - lib/search_flip/config.rb
200
+ - lib/search_flip/criteria.rb
201
+ - lib/search_flip/filterable.rb
202
+ - lib/search_flip/http_client.rb
203
+ - lib/search_flip/index.rb
204
+ - lib/search_flip/json.rb
205
+ - lib/search_flip/model.rb
206
+ - lib/search_flip/post_filterable.rb
207
+ - lib/search_flip/response.rb
208
+ - lib/search_flip/result.rb
209
+ - lib/search_flip/to_json.rb
210
+ - lib/search_flip/version.rb
211
+ - search_flip.gemspec
212
+ - test/database.yml
213
+ - test/search_flip/aggregation_test.rb
214
+ - test/search_flip/bulk_test.rb
215
+ - test/search_flip/criteria_test.rb
216
+ - test/search_flip/http_client_test.rb
217
+ - test/search_flip/index_test.rb
218
+ - test/search_flip/model_test.rb
219
+ - test/search_flip/response_test.rb
220
+ - test/search_flip/to_json_test.rb
221
+ - test/search_flip_test.rb
222
+ - test/test_helper.rb
223
+ homepage: https://github.com/mrkamel/search_flip
224
+ licenses:
225
+ - MIT
226
+ metadata: {}
227
+ post_install_message:
228
+ rdoc_options: []
229
+ require_paths:
230
+ - lib
231
+ required_ruby_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - ">="
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ required_rubygems_version: !ruby/object:Gem::Requirement
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ version: '0'
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 2.5.2.1
244
+ signing_key:
245
+ specification_version: 4
246
+ summary: Powerful ElasticSearch client library to easily build complex queries
247
+ test_files:
248
+ - test/database.yml
249
+ - test/search_flip/aggregation_test.rb
250
+ - test/search_flip/bulk_test.rb
251
+ - test/search_flip/criteria_test.rb
252
+ - test/search_flip/http_client_test.rb
253
+ - test/search_flip/index_test.rb
254
+ - test/search_flip/model_test.rb
255
+ - test/search_flip/response_test.rb
256
+ - test/search_flip/to_json_test.rb
257
+ - test/search_flip_test.rb
258
+ - test/test_helper.rb