mongoid-giza 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +1,84 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Mongoid::Giza do
4
- before do
5
- allow(Mongoid::Giza::GizaID).to receive(:create).with(id: :Person)
6
- allow(Mongoid::Giza::Configuration.instance).to receive(:add_index)
4
+ let(:index) { double("index") }
5
+
6
+ let(:search) { double("search") }
7
7
 
8
+ let(:config) { Mongoid::Giza::Configuration.instance }
9
+
10
+ before do
11
+ # :nodoc:
8
12
  class Person
9
13
  include Mongoid::Document
14
+ include Mongoid::Attributes::Dynamic
10
15
  include Mongoid::Giza
11
16
 
12
17
  field :name, type: String
13
18
  field :age, type: Integer
14
19
  end
20
+
21
+ allow(Mongoid::Giza::Configuration.instance).to receive(:add_index)
22
+ allow(Mongoid::Giza::Search).to receive(:new)
23
+ .with("localhost", 9132, []) { search }
24
+ allow(search).to receive(:run) { double("results").as_null_object }
25
+ allow(search).to receive(:indexes=)
15
26
  end
16
27
 
17
28
  after do
18
29
  Object.send(:remove_const, :Person)
19
30
  end
20
31
 
21
- let(:index) do
22
- index = double("index")
23
- allow(index).to receive(:name) { :Person }
24
- index
25
- end
26
-
27
- let(:new_index) { allow(Mongoid::Giza::Index).to receive(:new).with(Person, {}) { index } }
28
-
29
- let(:search) do
30
- search = double("search")
31
- allow(Mongoid::Giza::Search).to receive(:new).with("localhost", 9132, []) { search }
32
- search
33
- end
34
-
35
- let(:search_run) { allow(search).to receive(:run) { double("results").as_null_object } }
36
-
37
- let(:search_indexes) { allow(search).to receive(:indexes=) }
38
-
39
- let(:config) { Mongoid::Giza::Configuration.instance }
40
-
41
32
  describe "sphinx_index" do
42
33
  context "static index" do
43
34
  it "should add the index" do
44
- expect(Person).to receive(:add_static_sphinx_index).with({}, kind_of(Proc))
45
- Person.sphinx_index { }
35
+ expect(Person).to receive(:add_static_sphinx_index)
36
+ .with({}, kind_of(Proc))
37
+ Person.sphinx_index {}
46
38
  end
47
39
  end
48
40
 
49
41
  context "dynamic index" do
50
42
  it "should add the index to the dynamic index list" do
51
- expect(Person).to receive(:add_dynamic_sphinx_index).with({}, kind_of(Proc))
52
- Person.sphinx_index { |person| }
43
+ expect(Person).to receive(:add_dynamic_sphinx_index)
44
+ .with({}, kind_of(Proc))
45
+ Person.sphinx_index { |person| person }
53
46
  end
54
47
  end
55
48
  end
56
49
 
57
50
  describe "add_static_sphinx_index" do
51
+ before do
52
+ allow(Mongoid::Giza::Index).to receive(:new).with(Person, {}) { index }
53
+ allow(index).to receive(:name) { :Person }
54
+ end
55
+
58
56
  it "should create an index" do
59
57
  expect(Mongoid::Giza::Index).to receive(:new).with(Person, {}) { index }
60
- Person.add_static_sphinx_index({}, Proc.new { })
58
+ Person.add_static_sphinx_index({}, -> {})
61
59
  end
62
60
 
63
61
  it "should call index methods" do
64
62
  expect(index).to receive(:field).with(:name)
65
- new_index
66
- Person.add_static_sphinx_index({}, Proc.new { field :name })
63
+ Person.add_static_sphinx_index({}, -> { field :name })
67
64
  end
68
65
 
69
66
  it "should register the index on the class" do
70
67
  sphinx_indexes = double("sphinx_indexes")
71
68
  expect(sphinx_indexes).to receive(:[]=).with(index.name, index)
72
69
  allow(Person).to receive(:static_sphinx_indexes) { sphinx_indexes }
73
- new_index
74
- Person.add_static_sphinx_index({}, Proc.new { })
70
+ Person.add_static_sphinx_index({}, -> {})
75
71
  end
76
72
 
77
73
  it "should accept settings" do
78
- expect(Mongoid::Giza::Index).to receive(:new).with(Person, enable_star: 1) { index }
79
- Person.add_static_sphinx_index({enable_star: 1}, Proc.new { })
74
+ expect(Mongoid::Giza::Index).to receive(:new)
75
+ .with(Person, enable_star: 1) { index }
76
+ Person.add_static_sphinx_index({enable_star: 1}, -> {})
80
77
  end
81
78
 
82
79
  it "should add the index to the configuration" do
83
80
  expect(config).to receive(:add_index).with(index)
84
- new_index
85
- Person.add_static_sphinx_index({}, Proc.new { })
81
+ Person.add_static_sphinx_index({}, -> {})
86
82
  end
87
83
  end
88
84
 
@@ -94,15 +90,19 @@ describe Mongoid::Giza do
94
90
  end
95
91
 
96
92
  it "should create a dynamic index" do
97
- allow(Person).to receive(:generated_sphinx_indexes) { double.as_null_object }
98
- expect(Mongoid::Giza::DynamicIndex).to receive(:new).with(Person, {}, kind_of(Proc)) { double.as_null_object }
99
- Person.add_dynamic_sphinx_index({}, Proc.new { })
93
+ allow(Person).to receive(:generated_sphinx_indexes) do
94
+ double.as_null_object
95
+ end
96
+ expect(Mongoid::Giza::DynamicIndex).to receive(:new)
97
+ .with(Person, {}, kind_of(Proc)) { double.as_null_object }
98
+ Person.add_dynamic_sphinx_index({}, -> {})
100
99
  end
101
100
 
102
101
  it "should generate the indexes" do
103
102
  allow(Mongoid::Giza::DynamicIndex).to receive(:new) { dynamic_index }
104
- expect(Person).to receive(:process_dynamic_sphinx_index).with(dynamic_index)
105
- Person.add_dynamic_sphinx_index({}, Proc.new { })
103
+ expect(Person).to receive(:process_dynamic_sphinx_index)
104
+ .with(dynamic_index)
105
+ Person.add_dynamic_sphinx_index({}, -> {})
106
106
  end
107
107
  end
108
108
 
@@ -115,7 +115,9 @@ describe Mongoid::Giza do
115
115
 
116
116
  before do
117
117
  allow(generated).to receive(:each)
118
- allow(Person).to receive(:generated_sphinx_indexes) { generated_sphinx_indexes }
118
+ allow(Person).to receive(:generated_sphinx_indexes) do
119
+ generated_sphinx_indexes
120
+ end
119
121
  allow(generated_sphinx_indexes).to receive(:merge!)
120
122
  end
121
123
 
@@ -139,74 +141,44 @@ describe Mongoid::Giza do
139
141
  end
140
142
 
141
143
  describe "search" do
144
+ let(:mapped_results) { double("mapped results") }
145
+
142
146
  before do
143
- allow(Mongoid::Giza::Configuration.instance.searchd).to receive(:address) { "localhost" }
144
- allow(Mongoid::Giza::Configuration.instance.searchd).to receive(:port) { 9132 }
147
+ allow(Mongoid::Giza::Configuration.instance.searchd)
148
+ .to receive(:address) { "localhost" }
149
+ allow(Mongoid::Giza::Configuration.instance.searchd)
150
+ .to receive(:port) { 9132 }
145
151
  end
146
152
 
147
153
  it "should create a search" do
148
- expect(Mongoid::Giza::Search).to receive(:new).with("localhost", 9132, [:Person, :Person_2]) { double("search").as_null_object }
149
- Person.sphinx_index { }
154
+ expect(Mongoid::Giza::Search).to receive(:new)
155
+ .with("localhost", 9132, [:Person, :Person_2]) do
156
+ double("search").as_null_object
157
+ end
158
+ Person.sphinx_index {}
150
159
  Person.sphinx_index { name :Person_2 }
151
- Person.search { }
160
+ Person.search {}
152
161
  end
153
162
 
154
163
  it "should call search methods" do
155
- search_run
156
- search_indexes
157
164
  expect(search).to receive(:fulltext).with("query")
158
165
  Person.search { fulltext "query" }
159
166
  end
160
167
 
161
168
  it "should run the query" do
162
- search_run
163
- search_indexes
164
- Person.search { }
165
- end
166
-
167
- it "should return an array of results" do
168
- search_indexes
169
- allow(search).to receive(:run) { [{matches: []}, {matches: []}] }
170
- allow(Person).to receive(:in) { Mongoid::Criteria.new(Person) }
171
- expect(Person.search { }).to be_a_kind_of(Array)
172
- end
173
-
174
- it "should return a Mongoid::Criteria with on each search results" do
175
- search_indexes
176
- allow(search).to receive(:run) { [{matches: []}, {matches: []}] }
177
- expect(Person).to receive(:in).twice { Mongoid::Criteria.new(Person) }
178
- Person.search { }
179
- end
180
- end
181
-
182
- describe "giza_id" do
183
- let(:person) { Person.new }
184
-
185
- it "should use a previously created giza id" do
186
- person[:giza_id] = 1
187
- expect(person.giza_id).to eql(1)
169
+ expect(search).to receive(:run)
170
+ Person.search {}
188
171
  end
189
172
 
190
- it "should create a new giza id when needed" do
191
- allow(Mongoid::Giza::GizaID).to receive(:next_id).with(:Person) { 1 }
192
- expect(person.giza_id).to eql(1)
193
- end
194
-
195
- it "should save the object when the id is created" do
196
- allow(Mongoid::Giza::GizaID).to receive(:next_id).with(:Person) { 1 }
197
- expect(person).to receive(:set).with(:giza_id, 1)
198
- person.giza_id
199
- end
200
-
201
- it "should not save the object when the id is reused" do
202
- person[:giza_id] = 1
203
- expect(person).not_to receive(:set)
204
- person.giza_id
173
+ it "should retur map_to_mongoid" do
174
+ expect(Person).to receive(:map_to_mongoid) { mapped_results }
175
+ Person.search {}
205
176
  end
206
177
  end
207
178
 
208
179
  describe "sphinx_indexes" do
209
- it "should return an collection containg static indexes and generated indexes" do
180
+ it "should return an collection containg static indexes and generated " \
181
+ "indexes" do
210
182
  static = double("static")
211
183
  generated = double("generated")
212
184
  merged = double("merged")
@@ -222,14 +194,14 @@ describe Mongoid::Giza do
222
194
 
223
195
  it "should execute the index with all indexes from this class" do
224
196
  expect(indexer).to receive(:index!).with(:Person, :Person_2)
225
- Person.sphinx_index { }
197
+ Person.sphinx_index {}
226
198
  Person.sphinx_index { name :Person_2 }
227
199
  Person.sphinx_indexer!
228
200
  end
229
201
 
230
202
  it "should accept a list of indexes names" do
231
203
  expect(indexer).to receive(:index!).with(:Person, :Person_3)
232
- Person.sphinx_index { }
204
+ Person.sphinx_index {}
233
205
  Person.sphinx_index { name :Person_2 }
234
206
  Person.sphinx_index { name :Person_3 }
235
207
  Person.sphinx_indexer!(:Person, :Person_3)
@@ -240,9 +212,10 @@ describe Mongoid::Giza do
240
212
  Person.sphinx_indexer!
241
213
  end
242
214
 
243
- it "should not execute if the supplied names do not match any index name of the current class" do
215
+ it "should not execute if the supplied names do not match any index name " \
216
+ "of the current class" do
244
217
  expect(indexer).not_to receive(:index!)
245
- Person.sphinx_index { }
218
+ Person.sphinx_index {}
246
219
  Person.sphinx_indexer!(:Person_2)
247
220
  end
248
221
  end
@@ -288,7 +261,8 @@ describe Mongoid::Giza do
288
261
  it "should process all dynamic indexes" do
289
262
  allow(Person).to receive(:dynamic_sphinx_indexes) { dynamic }
290
263
  allow(dynamic).to receive(:each).and_yield(:dynamic_index)
291
- expect(Person).to receive(:process_dynamic_sphinx_index).with(:dynamic_index)
264
+ expect(Person).to receive(:process_dynamic_sphinx_index)
265
+ .with(:dynamic_index)
292
266
  Person.regenerate_sphinx_indexes
293
267
  end
294
268
  end
@@ -305,18 +279,21 @@ describe Mongoid::Giza do
305
279
  let(:index2) { double("index 2") }
306
280
 
307
281
  before do
308
- allow(Person).to receive(:dynamic_sphinx_indexes) { [dynamic_index, dynamic_index] }
282
+ allow(Person)
283
+ .to receive(:dynamic_sphinx_indexes) { [dynamic_index, dynamic_index] }
309
284
  allow(dynamic_index).to receive(:generate_index) { index }
310
285
  allow(index).to receive(:name) { :name }
311
286
  end
312
287
 
313
288
  it "should generate all the dynamic indexes of the class for the object" do
314
- expect(dynamic_index).to receive(:generate_index).with(person).twice { index }
289
+ expect(dynamic_index).to receive(:generate_index).with(person)
290
+ .twice { index }
315
291
  person.generate_sphinx_indexes
316
292
  end
317
293
 
318
294
  it "should merge the resulting indexes to the class' generated indexes" do
319
- expect(Person.generated_sphinx_indexes).to receive(:merge!).with({name: index}).twice
295
+ expect(Person.generated_sphinx_indexes).to receive(:merge!)
296
+ .with(name: index).twice
320
297
  person.generate_sphinx_indexes
321
298
  end
322
299
 
@@ -330,13 +307,28 @@ describe Mongoid::Giza do
330
307
  let(:index_name) { double("index name") }
331
308
 
332
309
  it "should remove the indexes from the generated indexes collection" do
333
- expect(Person.generated_sphinx_indexes).to receive(:delete).with(index_name).twice
310
+ expect(Person.generated_sphinx_indexes).to receive(:delete)
311
+ .with(index_name).twice
334
312
  Person.remove_generated_sphinx_indexes(index_name, index_name)
335
313
  end
336
314
 
337
315
  it "should remove the indexes from the configuration" do
338
- expect(config).to receive(:remove_generated_indexes).with([index_name, index_name])
316
+ expect(config).to receive(:remove_generated_indexes)
317
+ .with([index_name, index_name])
339
318
  Person.remove_generated_sphinx_indexes(index_name, index_name)
340
319
  end
341
320
  end
321
+
322
+ describe "map_to_mongoid" do
323
+ it "should return an result hash" do
324
+ allow(Person).to receive(:in) { Mongoid::Criteria.new(Person) }
325
+ expect(Person.send(:map_to_mongoid, matches: [])).to be_a_kind_of(Hash)
326
+ end
327
+
328
+ it "should add an entry with the Mongoid::Criteria Hash" do
329
+ allow(Person).to receive(:in) { Mongoid::Criteria.new(Person) }
330
+ result = Person.send(:map_to_mongoid, matches: [])
331
+ expect(result).to include(:Person)
332
+ end
333
+ end
342
334
  end
@@ -4,7 +4,7 @@
4
4
  # loaded once.
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
- require 'coveralls'
7
+ require "coveralls"
8
8
  Coveralls.wear!
9
9
 
10
10
  require "database_cleaner"
@@ -15,7 +15,7 @@ MONGOID_CONFIG = {
15
15
  sessions: {
16
16
  default: {
17
17
  database: "mongoid_giza_test",
18
- hosts: [ "localhost:27017" ]
18
+ hosts: ["localhost:27017"]
19
19
  }
20
20
  }
21
21
  }
@@ -25,7 +25,6 @@ Mongoid.configure do |config|
25
25
  end
26
26
 
27
27
  RSpec.configure do |config|
28
- config.treat_symbols_as_metadata_keys_with_true_values = true
29
28
  config.run_all_when_everything_filtered = true
30
29
  config.filter_run :focus
31
30
 
@@ -33,7 +32,7 @@ RSpec.configure do |config|
33
32
  # order dependency and want to debug it, you can fix the order by providing
34
33
  # the seed, which is printed after each run.
35
34
  # --seed 1234
36
- config.order = 'random'
35
+ config.order = "random"
37
36
 
38
37
  config.include Mongoid::Matchers, type: :model
39
38
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-giza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maurício Batista
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-05 00:00:00.000000000 Z
11
+ date: 2015-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,34 +94,48 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: 1.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 0.29.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.29.0
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: mongoid
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: '3.1'
117
+ version: '4.0'
104
118
  type: :runtime
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - ">="
109
123
  - !ruby/object:Gem::Version
110
- version: '3.1'
124
+ version: '4.0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: riddle
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - ">="
116
130
  - !ruby/object:Gem::Version
117
- version: '1.5'
131
+ version: 1.5.11
118
132
  type: :runtime
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
- version: '1.5'
138
+ version: 1.5.11
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: builder
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +170,14 @@ dependencies:
156
170
  requirements:
157
171
  - - ">="
158
172
  - !ruby/object:Gem::Version
159
- version: '0'
173
+ version: '4.0'
160
174
  type: :runtime
161
175
  prerelease: false
162
176
  version_requirements: !ruby/object:Gem::Requirement
163
177
  requirements:
164
178
  - - ">="
165
179
  - !ruby/object:Gem::Version
166
- version: '0'
180
+ version: '4.0'
167
181
  description: Mongoid layer for the Sphinx fulltext search server that supports block
168
182
  fields and dynamic indexes
169
183
  email:
@@ -173,6 +187,7 @@ extensions: []
173
187
  extra_rdoc_files: []
174
188
  files:
175
189
  - ".gitignore"
190
+ - ".rubocop.yml"
176
191
  - ".travis.yml"
177
192
  - CHANGELOG.md
178
193
  - Gemfile
@@ -186,7 +201,7 @@ files:
186
201
  - lib/mongoid/giza/index/attribute.rb
187
202
  - lib/mongoid/giza/index/field.rb
188
203
  - lib/mongoid/giza/indexer.rb
189
- - lib/mongoid/giza/models/giza_id.rb
204
+ - lib/mongoid/giza/models/id.rb
190
205
  - lib/mongoid/giza/railtie.rb
191
206
  - lib/mongoid/giza/search.rb
192
207
  - lib/mongoid/giza/version.rb
@@ -223,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
238
  version: '0'
224
239
  requirements: []
225
240
  rubyforge_project:
226
- rubygems_version: 2.2.2
241
+ rubygems_version: 2.4.6
227
242
  signing_key:
228
243
  specification_version: 4
229
244
  summary: Mongoid layer for the Sphinx fulltext search server