mongoid-giza 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,282 @@
1
+ require "spec_helper"
2
+
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)
7
+
8
+ class Person
9
+ include Mongoid::Document
10
+ include Mongoid::Giza
11
+
12
+ field :name, type: String
13
+ field :age, type: Integer
14
+ end
15
+ end
16
+
17
+ after do
18
+ Object.send(:remove_const, :Person)
19
+ end
20
+
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
+ describe "sphinx_index" do
42
+ context "static index" do
43
+ it "should add the index" do
44
+ expect(Person).to receive(:add_static_sphinx_index).with({}, kind_of(Proc))
45
+ Person.sphinx_index { }
46
+ end
47
+ end
48
+
49
+ context "dynamic index" do
50
+ 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| }
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "add_static_sphinx_index" do
58
+ it "should create an index" do
59
+ expect(Mongoid::Giza::Index).to receive(:new).with(Person, {}) { index }
60
+ Person.add_static_sphinx_index({}, Proc.new { })
61
+ end
62
+
63
+ it "should call index methods" do
64
+ expect(index).to receive(:field).with(:name)
65
+ new_index
66
+ Person.add_static_sphinx_index({}, Proc.new { field :name })
67
+ end
68
+
69
+ it "should register the index on the class" do
70
+ sphinx_indexes = double("sphinx_indexes")
71
+ expect(sphinx_indexes).to receive(:[]=).with(index.name, index)
72
+ allow(Person).to receive(:static_sphinx_indexes) { sphinx_indexes }
73
+ new_index
74
+ Person.add_static_sphinx_index({}, Proc.new { })
75
+ end
76
+
77
+ 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 { })
80
+ end
81
+
82
+ it "should add the index to the configuration" do
83
+ expect(config).to receive(:add_index).with(index)
84
+ new_index
85
+ Person.add_static_sphinx_index({}, Proc.new { })
86
+ end
87
+ end
88
+
89
+ describe "add_dynamic_sphinx_index" do
90
+ let(:dynamic_index) { double("dynamic index") }
91
+
92
+ before do
93
+ allow(Person).to receive(:process_dynamic_sphinx_index)
94
+ end
95
+
96
+ 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 { })
100
+ end
101
+
102
+ it "should generate the indexes" do
103
+ 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 { })
106
+ end
107
+ end
108
+
109
+ describe "process_dynamic_sphinx_index" do
110
+ let(:dynamic_index) { double("dynamic index") }
111
+
112
+ let(:generated) { double("generated") }
113
+
114
+ let(:generated_sphinx_indexes) { double("sphinx generated indexes") }
115
+
116
+ before do
117
+ allow(generated).to receive(:each)
118
+ allow(Person).to receive(:generated_sphinx_indexes) { generated_sphinx_indexes }
119
+ allow(generated_sphinx_indexes).to receive(:merge!)
120
+ end
121
+
122
+ it "should generate the indexes" do
123
+ expect(dynamic_index).to receive(:generate!) { {} }
124
+ Person.process_dynamic_sphinx_index(dynamic_index)
125
+ end
126
+
127
+ it "should merge the generated indexes" do
128
+ allow(dynamic_index).to receive(:generate!) { generated }
129
+ expect(generated_sphinx_indexes).to receive(:merge!).with(generated)
130
+ Person.process_dynamic_sphinx_index(dynamic_index)
131
+ end
132
+
133
+ it "should add the generated indexes to the configuration" do
134
+ allow(dynamic_index).to receive(:generate!) { generated }
135
+ allow(generated).to receive(:each).and_yield(:name, :generated_index)
136
+ expect(config).to receive(:add_index).with(:generated_index, true)
137
+ Person.process_dynamic_sphinx_index(dynamic_index)
138
+ end
139
+ end
140
+
141
+ describe "search" do
142
+ before do
143
+ allow(Mongoid::Giza::Configuration.instance.searchd).to receive(:address) { "localhost" }
144
+ allow(Mongoid::Giza::Configuration.instance.searchd).to receive(:port) { 9132 }
145
+ end
146
+
147
+ 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 { }
150
+ Person.sphinx_index { name :Person_2 }
151
+ Person.search { }
152
+ end
153
+
154
+ it "should call search methods" do
155
+ search_run
156
+ search_indexes
157
+ expect(search).to receive(:fulltext).with("query")
158
+ Person.search { fulltext "query" }
159
+ end
160
+
161
+ 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)
188
+ end
189
+
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
205
+ end
206
+ end
207
+
208
+ describe "sphinx_indexes" do
209
+ it "should return an collection containg static indexes and generated indexes" do
210
+ static = double("static")
211
+ generated = double("generated")
212
+ merged = double("merged")
213
+ allow(Person).to receive(:static_sphinx_indexes) { static }
214
+ allow(Person).to receive(:generated_sphinx_indexes) { generated }
215
+ allow(static).to receive(:merge).with(generated) { merged }
216
+ expect(Person.sphinx_indexes).to be(merged)
217
+ end
218
+ end
219
+
220
+ describe "sphinx_indexer!" do
221
+ let(:indexer) { Mongoid::Giza::Indexer.instance }
222
+
223
+ it "should execute the index with all indexes from this class" do
224
+ expect(indexer).to receive(:index!).with(:Person, :Person_2)
225
+ Person.sphinx_index { }
226
+ Person.sphinx_index { name :Person_2 }
227
+ Person.sphinx_indexer!
228
+ end
229
+
230
+ it "should accept a list of indexes names" do
231
+ expect(indexer).to receive(:index!).with(:Person, :Person_3)
232
+ Person.sphinx_index { }
233
+ Person.sphinx_index { name :Person_2 }
234
+ Person.sphinx_index { name :Person_3 }
235
+ Person.sphinx_indexer!(:Person, :Person_3)
236
+ end
237
+
238
+ it "should not execute if the class has no indexes" do
239
+ expect(indexer).not_to receive(:index!)
240
+ Person.sphinx_indexer!
241
+ end
242
+
243
+ it "should not execute if the supplied names do not match any index name of the current class" do
244
+ expect(indexer).not_to receive(:index!)
245
+ Person.sphinx_index { }
246
+ Person.sphinx_indexer!(:Person_2)
247
+ end
248
+ end
249
+
250
+ describe "sphinx_indexes_names" do
251
+ it "should return the name of all indexes" do
252
+ static = double("static")
253
+ generated = double("generated")
254
+ merged = double("merged")
255
+ names = double("names")
256
+ allow(Person).to receive(:static_sphinx_indexes) { static }
257
+ allow(Person).to receive(:generated_sphinx_indexes) { generated }
258
+ allow(static).to receive(:merge).with(generated) { merged }
259
+ allow(merged).to receive(:keys) { names }
260
+ expect(Person.sphinx_indexes_names).to be(names)
261
+ end
262
+ end
263
+
264
+ describe "regenerate_dynamic_sphinx_indexes" do
265
+ let(:generated) { double("generated") }
266
+
267
+ let(:dynamic) { double("dynamic") }
268
+
269
+ it "should clear the generated indexes" do
270
+ allow(Person).to receive(:generated_sphinx_indexes) { generated }
271
+ expect(generated).to receive(:clear)
272
+ Person.regenerate_dynamic_sphinx_indexes
273
+ end
274
+
275
+ it "should process all dynamic indexes" do
276
+ allow(Person).to receive(:dynamic_sphinx_indexes) { dynamic }
277
+ allow(dynamic).to receive(:each).and_yield(:dynamic_index)
278
+ expect(Person).to receive(:process_dynamic_sphinx_index).with(:dynamic_index)
279
+ Person.regenerate_dynamic_sphinx_indexes
280
+ end
281
+ end
282
+ end
@@ -0,0 +1,51 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'coveralls'
8
+ Coveralls.wear!
9
+
10
+ require "database_cleaner"
11
+ require "mongoid-rspec"
12
+ require "mongoid/giza"
13
+
14
+ MONGOID_CONFIG = {
15
+ sessions: {
16
+ default: {
17
+ database: "mongoid_giza_test",
18
+ hosts: [ "localhost:27017" ]
19
+ }
20
+ }
21
+ }
22
+
23
+ Mongoid.configure do |config|
24
+ config.load_configuration(MONGOID_CONFIG)
25
+ end
26
+
27
+ RSpec.configure do |config|
28
+ config.treat_symbols_as_metadata_keys_with_true_values = true
29
+ config.run_all_when_everything_filtered = true
30
+ config.filter_run :focus
31
+
32
+ # Run specs in random order to surface order dependencies. If you find an
33
+ # order dependency and want to debug it, you can fix the order by providing
34
+ # the seed, which is printed after each run.
35
+ # --seed 1234
36
+ config.order = 'random'
37
+
38
+ config.include Mongoid::Matchers, type: :model
39
+
40
+ config.before(:suite) do
41
+ DatabaseCleaner.strategy = :truncation
42
+ end
43
+
44
+ config.before(:each) do
45
+ DatabaseCleaner.start
46
+ end
47
+
48
+ config.after(:each) do
49
+ DatabaseCleaner.clean
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-giza
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maurício Batista
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-20 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: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '1.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.7
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.7
83
+ - !ruby/object:Gem::Dependency
84
+ name: database_cleaner
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: mongoid
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '3.1'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '3.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: riddle
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '1.5'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '1.5'
125
+ - !ruby/object:Gem::Dependency
126
+ name: builder
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: docile
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '1.1'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '1.1'
153
+ description: Mongoid layer for the Sphinx fulltext search server that supports block
154
+ fields and dynamic indexes
155
+ email:
156
+ - eddloschi@gmail.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - .gitignore
162
+ - .travis.yml
163
+ - Gemfile
164
+ - LICENSE.txt
165
+ - README.md
166
+ - Rakefile
167
+ - lib/mongoid/giza.rb
168
+ - lib/mongoid/giza/configuration.rb
169
+ - lib/mongoid/giza/dynamic_index.rb
170
+ - lib/mongoid/giza/index.rb
171
+ - lib/mongoid/giza/index/attribute.rb
172
+ - lib/mongoid/giza/index/field.rb
173
+ - lib/mongoid/giza/indexer.rb
174
+ - lib/mongoid/giza/models/giza_id.rb
175
+ - lib/mongoid/giza/railtie.rb
176
+ - lib/mongoid/giza/search.rb
177
+ - lib/mongoid/giza/version.rb
178
+ - lib/mongoid/giza/xml_pipe2.rb
179
+ - mongoid-giza.gemspec
180
+ - spec/mongoid/giza/configuration_spec.rb
181
+ - spec/mongoid/giza/dynamic_index_spec.rb
182
+ - spec/mongoid/giza/index/attribute_spec.rb
183
+ - spec/mongoid/giza/index/field_spec.rb
184
+ - spec/mongoid/giza/index_spec.rb
185
+ - spec/mongoid/giza/indexer_spec.rb
186
+ - spec/mongoid/giza/models/giza_id_spec.rb
187
+ - spec/mongoid/giza/search_spec.rb
188
+ - spec/mongoid/giza/xml_pipe2_spec.rb
189
+ - spec/mongoid/giza_spec.rb
190
+ - spec/spec_helper.rb
191
+ homepage: https://github.com/yadevteam/mongoid-giza
192
+ licenses:
193
+ - MIT
194
+ metadata: {}
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - '>='
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project:
211
+ rubygems_version: 2.1.11
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: Mongoid layer for the Sphinx fulltext search server
215
+ test_files:
216
+ - spec/mongoid/giza/configuration_spec.rb
217
+ - spec/mongoid/giza/dynamic_index_spec.rb
218
+ - spec/mongoid/giza/index/attribute_spec.rb
219
+ - spec/mongoid/giza/index/field_spec.rb
220
+ - spec/mongoid/giza/index_spec.rb
221
+ - spec/mongoid/giza/indexer_spec.rb
222
+ - spec/mongoid/giza/models/giza_id_spec.rb
223
+ - spec/mongoid/giza/search_spec.rb
224
+ - spec/mongoid/giza/xml_pipe2_spec.rb
225
+ - spec/mongoid/giza_spec.rb
226
+ - spec/spec_helper.rb
227
+ has_rdoc: