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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/giza/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-giza"
8
+ spec.version = Mongoid::Giza::VERSION
9
+ spec.authors = ["Maurício Batista"]
10
+ spec.email = ["eddloschi@gmail.com"]
11
+ spec.description = %q{Mongoid layer for the Sphinx fulltext search server that supports block fields and dynamic indexes}
12
+ spec.summary = %q{Mongoid layer for the Sphinx fulltext search server}
13
+ spec.homepage = "https://github.com/yadevteam/mongoid-giza"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", ">= 2.14"
24
+ spec.add_development_dependency "mongoid-rspec", ">= 1.9"
25
+ spec.add_development_dependency "yard", ">= 0.8.7"
26
+ spec.add_development_dependency "database_cleaner", ">= 1.2.0"
27
+
28
+ spec.add_runtime_dependency "mongoid", ">= 3.1"
29
+ spec.add_runtime_dependency "riddle", ">= 1.5"
30
+ spec.add_runtime_dependency "builder", ">= 3.0"
31
+ spec.add_runtime_dependency "docile", ">= 1.1"
32
+ end
@@ -0,0 +1,340 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Giza::Configuration do
4
+ before do
5
+ @default_source = double("default_source")
6
+ @default_index = double("default_index")
7
+ allow(Riddle::Configuration::XMLSource).to receive(:new).with(:source, :xmlpipe2) { @default_source }
8
+ allow(Riddle::Configuration::Index).to receive(:new).with(:index, @default_source) { @default_index }
9
+ @config = Mongoid::Giza::Configuration.send(:new)
10
+ end
11
+
12
+ describe "initialize" do
13
+ it "should create a Riddle::Configuration::Index for default settings" do
14
+ expect(@config.index).to be(@default_index)
15
+ end
16
+
17
+ it "should create a Riddle::Configuration::XMLSource for default settings" do
18
+ expect(@config.source).to be(@default_source)
19
+ end
20
+
21
+ describe "file section" do
22
+ it "should create a file section" do
23
+ expect(@config.file).to be_a_kind_of(OpenStruct)
24
+ end
25
+
26
+ it "should have output path setting" do
27
+ expect(@config.file.respond_to?(:output_path=)).to be(true)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "load" do
33
+ let(:file) { double("file") }
34
+
35
+ let(:file_open) { allow(File).to receive(:open).with("giza.yml") { file } }
36
+
37
+ it "should load the configuration file" do
38
+ expect(file).to receive(:read) { "test:\n searchd:\n address: localhost" }
39
+ expect(File).to receive(:open).with("giza.yml") { file }
40
+ @config.load("giza.yml", "test")
41
+ end
42
+
43
+ it "should set settings" do
44
+ allow(file).to receive(:read) { "test:\n searchd:\n address: localhost" }
45
+ file_open
46
+ @config.load("giza.yml", "test")
47
+ expect(@config.searchd.address).to eql("localhost")
48
+ end
49
+
50
+ it "should ignore non-existent sections" do
51
+ allow(file).to receive(:read) { "test:\n miss_section:\n address: localhost" }
52
+ file_open
53
+ expect { @config.load("giza.yml", "test") }.not_to raise_error
54
+ end
55
+
56
+ it "should ignore non-existent settings" do
57
+ allow(file).to receive(:read) { "test:\n searchd:\n miss_setting: false" }
58
+ expect(@config.searchd).not_to receive(:method_missing).with(:miss_setting=, false)
59
+ file_open
60
+ @config.load("giza.yml", "test")
61
+ end
62
+ end
63
+
64
+ describe "add_index" do
65
+ let(:indices) { double("indices") }
66
+
67
+ let(:index) { double("index") }
68
+
69
+ let(:riddle_index) { double("riddle index") }
70
+
71
+ let(:length) { double("length") }
72
+
73
+ let(:static) { double("static") }
74
+
75
+ let(:generated) { double("generated") }
76
+
77
+ before do
78
+ allow(@config).to receive(:indices) { indices }
79
+ allow(@config).to receive(:create_index) { riddle_index }
80
+ allow(@config).to receive(:register_index) { length }
81
+ allow(indices).to receive(:<<)
82
+ allow(indices).to receive(:[]=)
83
+ allow(index).to receive(:name)
84
+ end
85
+
86
+ it "should create a new riddle index" do
87
+ expect(@config).to receive(:create_index).with(index)
88
+ @config.add_index(index)
89
+ end
90
+
91
+ it "should add the index to the configuration indices" do
92
+ expect(indices).to receive(:[]=).with(length, riddle_index)
93
+ @config.add_index(index)
94
+ end
95
+
96
+ it "should register a static index on the static indexes hash" do
97
+ @config.instance_variable_set("@static_indexes", static)
98
+ expect(@config).to receive(:register_index).with(riddle_index, static)
99
+ @config.add_index(index)
100
+ end
101
+
102
+ it "should register a generated index on the generated indexes hash" do
103
+ @config.instance_variable_set("@generated_indexes", generated)
104
+ expect(@config).to receive(:register_index).with(riddle_index, generated)
105
+ @config.add_index(index, true)
106
+ end
107
+ end
108
+
109
+ describe "create_index" do
110
+ let(:index) do
111
+ index = double("index")
112
+ allow(index).to receive(:name) { :Person }
113
+ allow(index).to receive(:settings) { Hash.new }
114
+ index
115
+ end
116
+
117
+ let(:riddle_index) { double("riddle_index").as_null_object }
118
+
119
+ let(:source) { double("source").as_null_object }
120
+
121
+ before do
122
+ allow(Riddle::Configuration::Index).to receive(:settings) { [] }
123
+ allow(Riddle::Configuration::XMLSource).to receive(:new) { source }
124
+ allow(Riddle::Configuration::Index).to receive(:new).with(index.name, source) { riddle_index }
125
+ allow(@config).to receive(:apply_default_settings)
126
+ allow(@config).to receive(:apply_user_settings)
127
+ end
128
+
129
+ it "should create a xmlpipe2 source with the same name of the index" do
130
+ expect(Riddle::Configuration::XMLSource).to receive(:new).with(index.name, :xmlpipe2) { source }
131
+ allow(Riddle::Configuration::Index).to receive(:new) { double("riddle_index").as_null_object }
132
+ @config.create_index(index)
133
+ end
134
+
135
+ it "should set the path" do
136
+ allow(Riddle::Configuration::Index).to receive(:settings) { [:path] }
137
+ allow(@default_index).to receive(:path) { "/path/to/index" }
138
+ allow(riddle_index).to receive(:path=).with("/path/to/index")
139
+ allow(riddle_index).to receive(:path) { "/path/to/index" }
140
+ expect(riddle_index).to receive(:path=).with("/path/to/index/#{index.name}")
141
+ @config.create_index(index)
142
+ end
143
+
144
+ it "should apply default settings to the index" do
145
+ expect(@config).to receive(:apply_default_settings).with(@default_index, riddle_index, index)
146
+ @config.create_index(index)
147
+ end
148
+
149
+ it "should apply default settings to the source" do
150
+ expect(@config).to receive(:apply_default_settings).with(@default_source, source, index)
151
+ @config.create_index(index)
152
+ end
153
+
154
+ it "should apply user defined settings to the index" do
155
+ expect(@config).to receive(:apply_user_settings).with(index, riddle_index)
156
+ @config.create_index(index)
157
+ end
158
+
159
+ it "should apply user defined settings to the source" do
160
+ expect(@config).to receive(:apply_user_settings).with(index, source)
161
+ @config.create_index(index)
162
+ end
163
+
164
+ it "should return the index" do
165
+ expect(@config.create_index(index)).to be(riddle_index)
166
+ end
167
+ end
168
+
169
+ describe "register_index" do
170
+ let(:indices) { double("indices") }
171
+
172
+ let(:length) { double("length") }
173
+
174
+ let(:index) { double("index") }
175
+
176
+ let(:indexes) { double("indexes") }
177
+
178
+ before do
179
+ allow(@config).to receive(:indices) { indices }
180
+ allow(indices).to receive(:length) { length }
181
+ allow(index).to receive(:name) { :index }
182
+ allow(indexes).to receive(:[]=)
183
+ allow(indexes).to receive(:has_key?)
184
+ end
185
+
186
+ it "should add the index to the given hash" do
187
+ expect(indexes).to receive(:[]=).with(:index, index)
188
+ @config.register_index(index, indexes)
189
+ end
190
+
191
+ it "should return the position to replace the index on the configuration indices" do
192
+ position = double("position")
193
+ allow(indexes).to receive(:has_key?).with(:index) { true }
194
+ allow(indexes).to receive(:[]).with(:index) { index }
195
+ allow(indices).to receive(:index).with(index) { position }
196
+ expect(@config.register_index(index, indexes)).to be(position)
197
+ end
198
+
199
+ it "should return the position to add the index on the configuration indices" do
200
+ allow(indexes).to receive(:has_key?).with(:index) { false }
201
+ expect(@config.register_index(index, indexes)).to be(length)
202
+ end
203
+ end
204
+
205
+ describe "apply_default_settings" do
206
+ before do
207
+ @default = double("default")
208
+ @section = double("section")
209
+ @index = double("index")
210
+ allow(@default).to receive(:class) do
211
+ klass = double("class")
212
+ allow(klass).to receive(:settings) { [:html_strip] }
213
+ klass
214
+ end
215
+ end
216
+
217
+ it "should apply the settings from default to section" do
218
+ allow(@default).to receive(:html_strip) { 1 }
219
+ allow(@section).to receive(:respond_to?).with("html_strip=") { true }
220
+ expect(@section).to receive(:html_strip=).with(1)
221
+ @config.apply_default_settings(@default, @section, @index)
222
+ end
223
+
224
+ it "should not set nil values" do
225
+ allow(@default).to receive(:html_strip) { nil }
226
+ allow(@section).to receive(:respond_to?).with("html_strip=") { true }
227
+ expect(@section).not_to receive(:html_strip=)
228
+ @config.apply_default_settings(@default, @section, @index)
229
+ end
230
+
231
+ it "should not try to apply settings without a setter" do
232
+ allow(@default).to receive(:html_strip) { 1 }
233
+ allow(@section).to receive(:respond_to?).with("html_strip=") { false }
234
+ expect(@section).not_to receive(:html_strip=)
235
+ @config.apply_default_settings(@default, @section, @index)
236
+ end
237
+
238
+ it "should interpolate string values" do
239
+ allow(@default).to receive(:html_strip) { 1 }
240
+ allow(@section).to receive(:respond_to?).with("html_strip=") { true }
241
+ allow(@section).to receive(:html_strip=)
242
+ expect(@config).to receive(:interpolate_string).with(1, @index)
243
+ @config.apply_default_settings(@default, @section, @index)
244
+ end
245
+ end
246
+
247
+ describe "apply_user_settings" do
248
+ before do
249
+ @index = double("index")
250
+ @section = double("section")
251
+ allow(@index).to receive(:settings) { {html_strip: 1} }
252
+ allow(@section).to receive(:html_strip=)
253
+ end
254
+
255
+ it "should apply the the settings" do
256
+ allow(@section).to receive(:respond_to?).with("html_strip=") { true }
257
+ expect(@section).to receive(:html_strip=).with(1)
258
+ @config.apply_user_settings(@index, @section)
259
+ end
260
+
261
+ it "should not try to apply settings without a setter" do
262
+ allow(@section).to receive(:respond_to?).with("html_strip=") { false }
263
+ expect(@section).not_to receive(:html_strip=)
264
+ @config.apply_user_settings(@index, @section)
265
+ end
266
+
267
+ it "should interpolate the value" do
268
+ allow(@section).to receive(:respond_to?).with("html_strip=") { true }
269
+ expect(@config).to receive(:interpolate_string).with(1, @index)
270
+ @config.apply_user_settings(@index, @section)
271
+ end
272
+ end
273
+
274
+ describe "interpolate_string" do
275
+ let(:string) { double("string") }
276
+
277
+ let(:index) { double("index") }
278
+
279
+ let(:namespace) { double("namespace") }
280
+
281
+ let(:erb) { double("erb") }
282
+
283
+ let(:binding) { double("binding") }
284
+
285
+ let(:result) { double("result") }
286
+
287
+ before do
288
+ allow(string).to receive(:is_a?) { false }
289
+ allow(string).to receive(:is_a?).with(String) { true }
290
+ allow(ERB).to receive(:new).with(string) { erb }
291
+ allow(erb).to receive(:result) { result }
292
+ end
293
+
294
+ it "should ignore non string values" do
295
+ expect(@config.interpolate_string(1, index)).to eql(1)
296
+ end
297
+
298
+ it "should create the namespace" do
299
+ expect(OpenStruct).to receive(:new).with(index: index)
300
+ @config.interpolate_string(string, index)
301
+ end
302
+
303
+ it "should return the interpolation result" do
304
+ allow(namespace).to receive(:binding) { binding }
305
+ allow(erb).to receive(:result).with(binding) { result }
306
+ expect(@config.interpolate_string(string, index)).to be(result)
307
+ end
308
+ end
309
+
310
+ describe "render" do
311
+ it "should render the configuration to the specified output path" do
312
+ file = double("file")
313
+ index = double("index")
314
+ @config.indices << index
315
+ allow(@config.indexer).to receive(:render) { "indexer" }
316
+ allow(@config.searchd).to receive(:render) { "searchd" }
317
+ allow(index).to receive(:render) { "source\nindex" }
318
+ expect(File).to receive(:open).with(@config.file.output_path, "w").and_yield(file)
319
+ expect(file).to receive(:write).with("indexer\nsearchd\nsource\nindex")
320
+ @config.render
321
+ end
322
+ end
323
+
324
+ describe "clear_generated_indexes" do
325
+ let(:indices) { double("indices") }
326
+
327
+ it "should delete the generated riddle indexes" do
328
+ @config.instance_variable_set("@generated_indexes", {name: :index})
329
+ allow(@config).to receive(:indices) { indices }
330
+ expect(indices).to receive(:delete).with(:index)
331
+ @config.clear_generated_indexes
332
+ end
333
+
334
+ it "should clear the generated indexes collection" do
335
+ @config.instance_variable_set("@generated_indexes", {name: :index})
336
+ @config.clear_generated_indexes
337
+ expect(@config.instance_variable_get("@generated_indexes")).to eql({})
338
+ end
339
+ end
340
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Giza::DynamicIndex do
4
+ describe "initialize" do
5
+ it "should accept the class, settings and a proc" do
6
+ dynamic_index = Mongoid::Giza::DynamicIndex.new(Object, {}, Proc.new { })
7
+ expect(dynamic_index.klass).to be(Object)
8
+ expect(dynamic_index.settings).to eql({})
9
+ expect(dynamic_index.block).to be_a_kind_of(Proc)
10
+ end
11
+ end
12
+
13
+ describe "generate!" do
14
+ let(:dynamic_index) { Mongoid::Giza::DynamicIndex.new(Object, {}, Proc.new { |object| name(object[:name]) }) }
15
+
16
+ before do
17
+ klass = double("class")
18
+ index = double("index")
19
+ allow(klass).to receive(:all) { [{name: "object"}, {name: "other"}, {name: "other"}] }
20
+ allow(klass).to receive(:name) { :class }
21
+ allow(dynamic_index).to receive(:klass) { klass }
22
+ end
23
+
24
+ it "should return a collection of indexes" do
25
+ expect(dynamic_index.generate!).to be_a_kind_of(Hash)
26
+ end
27
+
28
+ it "should only return indexes with unique names" do
29
+ expect(dynamic_index.generate!.length).to eql(2)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Giza::Index::Attribute do
4
+ describe "name" do
5
+ it "should be mandatory" do
6
+ expect { Mongoid::Giza::Index::Attribute.new }.to raise_error(ArgumentError)
7
+ end
8
+
9
+ it "should be set on creation" do
10
+ name = "attribute"
11
+ attribute = Mongoid::Giza::Index::Attribute.new(name, :uint)
12
+ expect(attribute.name).to eql(name)
13
+ end
14
+ end
15
+
16
+ describe "type" do
17
+ it "should be mandatory" do
18
+ expect { Mongoid::Giza::Index::Attribute.new("attribute") }.to raise_error(ArgumentError)
19
+ end
20
+
21
+ it "should be set on creation" do
22
+ type = :uint
23
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute", type)
24
+ expect(attribute.type).to eql(type)
25
+ end
26
+
27
+ it "should be a valid type" do
28
+ expect { Mongoid::Giza::Index::Attribute.new("attribute", :type) }.to raise_error(TypeError)
29
+ end
30
+ end
31
+
32
+ it "should accept a block" do
33
+ attribute = Mongoid::Giza::Index::Attribute.new("attribute", :uint) { }
34
+ expect(attribute.block).to be_a(Proc)
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Giza::Index::Field do
4
+ describe "name" do
5
+ it "should be mandatory" do
6
+ expect { Mongoid::Giza::Index::Field.new }.to raise_error(ArgumentError)
7
+ end
8
+
9
+ it "should be set on creation" do
10
+ name = "field"
11
+ field = Mongoid::Giza::Index::Field.new(name)
12
+ expect(field.name).to eql(name)
13
+ end
14
+ end
15
+
16
+ it "should accept string attribute" do
17
+ field = Mongoid::Giza::Index::Field.new("field", true)
18
+ expect(field.attribute).to eql(true)
19
+ end
20
+
21
+ it "should accept a block" do
22
+ field = Mongoid::Giza::Index::Field.new("field") { }
23
+ expect(field.block).to be_a(Proc)
24
+ end
25
+ end