-
Notifications
You must be signed in to change notification settings - Fork 10
/
config_spec.rb
252 lines (215 loc) · 11.3 KB
/
config_spec.rb
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require File.dirname(__FILE__) + '/spec_helper'
describe "Config" do
before(:each) do
@config = StrokeDB::Config.new
@root_path = TEMP_STORAGES + '/config/'
@paths = []
end
it "should be the default config if desired" do
@config = StrokeDB::Config.new(true)
::StrokeDB.default_config.should == @config
end
it "should have no storages or indexes on creation" do
@config.storages.should be_empty
@config.indexes.should be_empty
end
it "should add known storages without specific parameters" do
@config.add_storage :mem, :memory
@config.storages[:mem].should be_an_instance_of(MemoryStorage)
end
it "should add known storages with specific parameters" do
@paths << (@root_path + "file_storage")
@config.add_storage :fs, :file, :path => @paths.last
@config.storages[:fs].should be_an_instance_of(FileStorage)
@config.storages[:fs].path.should == @paths.last
end
it "should raise the correct exception when adding an unknown storage" do
lambda { @config.add_storage :unknown, :unknown_storage_type }.should raise_error(StrokeDB::UnknownStorageTypeError)
end
it "should raise an exception on chain with insufficient storages" do
lambda { @config.chain }.should raise_error
@config.add_storage :mem, :memory
lambda { @config.chain :mem }.should raise_error
end
it "should raise an exception on chain with undefined storages" do
lambda { @config.chain :not_here }.should raise_error
end
it "should chain storages together" do
@paths << (@root_path + "file_storage_chain")
@config.add_storage :mem1, :memory
@config.add_storage :mem2, :memory
@config.add_storage :mem3, :memory
@config.add_storage :fs, :file, :path => @paths.last
@config.chain :mem1, :mem2, :fs
@config.chain :mem2, :mem3
@config.storages[:mem1].should have_chained_storage(@config.storages[:mem2])
@config.storages[:mem2].should have_chained_storage(@config.storages[:fs])
@config.storages[:fs].should have_chained_storage(@config.storages[:mem2])
@config.storages[:mem2].should have_chained_storage(@config.storages[:mem1])
@config.storages[:mem1].should_not have_chained_storage(@config.storages[:mem1])
@config.storages[:mem2].should_not have_chained_storage(@config.storages[:mem2])
@config.storages[:fs].should_not have_chained_storage(@config.storages[:fs])
@config.storages[:mem1].should_not have_chained_storage(@config.storages[:fs])
@config.storages[:fs].should_not have_chained_storage(@config.storages[:mem1])
@config.storages[:mem2].should have_chained_storage(@config.storages[:mem3])
@config.storages[:mem3].should have_chained_storage(@config.storages[:mem2])
@config.storages[:mem1].should_not have_chained_storage(@config.storages[:mem3])
@config.storages[:fs].should_not have_chained_storage(@config.storages[:mem3])
end
it "should add an index" do
@paths << (@root_path + "file_storage_index")
@config.add_storage :fs, :file, :path => @paths.last
@paths << (@root_path + "inverted_list_file_index")
@config.add_storage :idx_st, :inverted_list_file, :path => @paths.last
@config.add_index :idx, :inverted_list, :idx_st
@config.indexes[:idx].should be_an_instance_of(InvertedListIndex)
end
it "should raise the correct exception when adding an unknown index" do
lambda { @config.add_index :unknown, :unknown_index_type, :idx_st }.should raise_error(StrokeDB::UnknownIndexTypeError)
end
it "should add a store" do
@paths << (@root_path + "file_storage")
@config.add_storage :fs, :file, :path => @paths.last
@config.add_store :store, nil, :storage => :fs, :cut_level => 4, :path => @paths.last
@config.stores[:store].should be_an_instance_of(Store)
@config.stores[:store].storage.should == @config.storages[:fs]
end
it "should add a default store with default index" do
@paths << (@root_path + "file_storage_default_index")
@config.add_storage :fs, :file, :path => @paths.last
@paths << (@root_path + "inverted_list_file_default_index")
@config.add_storage :index_storage, :inverted_list_file, :path => @paths.last
@config.add_index :default, :inverted_list, :index_storage
@config.add_store :default, nil, :storage => :fs, :cut_level => 4, :path => @paths.last
@config.stores[:default].should be_an_instance_of(Store)
@config.indexes[:default].document_store.should == @config.stores[:default]
end
after(:each) do
@paths.each {|path| FileUtils.rm_rf path}
end
end
describe "Config builder" do
before(:each) do
@base_path = TEMP_STORAGES + '/cfg_builder'
FileUtils.rm_rf @base_path
end
after(:each) do
FileUtils.rm_rf @base_path
end
it "should make config default if told so" do
config = StrokeDB::Config.build :default => true, :base_path => @base_path
StrokeDB.default_config.should == config
end
it "should not make config default if told so" do
config = StrokeDB::Config.build :base_path => @base_path
StrokeDB.default_config.should_not == config
end
it "should use Store by default" do
config = StrokeDB::Config.build :base_path => @base_path
config.stores[:default].should be_a_kind_of(Store)
end
it "should use specified store if told so" do
StrokeDB.send!(:remove_const,'SomeFunnyStore') if defined?(SomeFunnyStore)
StrokeDB::SomeFunnyStore = Class.new(Store)
pending("not that important now") do
config = StrokeDB::Config.build :store => :some_funny, :base_path => @base_path
config.stores[:default].should be_a_kind_of(SomeFunnyStore)
end
end
it "should add storages as he is told to" do
StrokeDB.send!(:remove_const,'Chunk1Storage') if defined?(Chunk1Storage)
StrokeDB.send!(:remove_const,'Chunk2Storage') if defined?(Chunk2Storage)
StrokeDB::Chunk1Storage = Class.new(MemoryStorage)
StrokeDB::Chunk2Storage = Class.new(MemoryStorage)
config = StrokeDB::Config.build :storages => [:chunk_1,:chunk_2], :base_path => @base_path
config.storages[:chunk_1].should be_a_kind_of(Chunk1Storage)
config.storages[:chunk_2].should be_a_kind_of(Chunk2Storage)
end
it "should initialize all storages with base_path+storage_name" do
StrokeDB.send!(:remove_const,'Chunk1Storage') if defined?(Chunk1Storage)
StrokeDB.send!(:remove_const,'Chunk2Storage') if defined?(Chunk2Storage)
StrokeDB::Chunk1Storage = Class.new(MemoryStorage)
StrokeDB::Chunk2Storage = Class.new(MemoryStorage)
Chunk1Storage.should_receive(:new).with(:path => @base_path + '/chunk_1').and_return(MemoryStorage.new)
Chunk2Storage.should_receive(:new).with(:path => @base_path + '/chunk_2').and_return(MemoryStorage.new)
config = StrokeDB::Config.build :storages => [:chunk_1,:chunk_2], :base_path => @base_path
end
it "should add :memory and :file by default" do
config = StrokeDB::Config.build :base_path => @base_path
config.storages[:memory].should be_a_kind_of(MemoryStorage)
config.storages[:file].should be_a_kind_of(FileStorage)
end
it "should chain given storages sequentially" do
StrokeDB.send!(:remove_const,'Chunk1Storage') if defined?(Chunk1Storage)
StrokeDB.send!(:remove_const,'Chunk2Storage') if defined?(Chunk2Storage)
StrokeDB.send!(:remove_const,'Chunk3Storage') if defined?(Chunk3Storage)
StrokeDB::Chunk1Storage = Class.new(MemoryStorage)
StrokeDB::Chunk2Storage = Class.new(MemoryStorage)
StrokeDB::Chunk3Storage = Class.new(MemoryStorage)
config = StrokeDB::Config.build :storages => [:chunk_1,:chunk_2,:chunk_3], :base_path => @base_path
config.storages[:chunk_1].should have_chained_storage(config[:chunk_2])
config.storages[:chunk_2].should have_chained_storage(config[:chunk_1])
config.storages[:chunk_3].should have_chained_storage(config[:chunk_2])
config.storages[:chunk_2].should have_chained_storage(config[:chunk_3])
end
it "should set authoritative sources for storages sequentially" do
StrokeDB.send!(:remove_const,'Chunk1Storage') if defined?(Chunk1Storage)
StrokeDB.send!(:remove_const,'Chunk2Storage') if defined?(Chunk2Storage)
StrokeDB.send!(:remove_const,'Chunk3Storage') if defined?(Chunk3Storage)
StrokeDB::Chunk1Storage = Class.new(MemoryStorage)
StrokeDB::Chunk2Storage = Class.new(MemoryStorage)
StrokeDB::Chunk3Storage = Class.new(MemoryStorage)
config = StrokeDB::Config.build :storages => [:chunk_1,:chunk_2,:chunk_3], :base_path => @base_path
config.storages[:chunk_1].authoritative_source.should == config[:chunk_2]
config.storages[:chunk_2].authoritative_source.should == config[:chunk_3]
config.storages[:chunk_3].authoritative_source.should be_nil
end
it "should use InvertedListIndex by default" do
config = StrokeDB::Config.build :base_path => @base_path
config.indexes[:default].should be_a_kind_of(InvertedListIndex)
end
it "should use specific index if told so by default" do
StrokeDB.send!(:remove_const,'SomeFunnyIndex') if defined?(SomeFunnyIndex)
StrokeDB::SomeFunnyIndex = Class.new(InvertedListIndex)
config = StrokeDB::Config.build :index => :some_funny, :base_path => @base_path
config.indexes[:default].should be_a_kind_of(SomeFunnyIndex)
end
it "should use InvertedListFileStorage index storage by default" do
config = StrokeDB::Config.build :base_path => @base_path
config.storages[:inverted_list_file].should be_a_kind_of(InvertedListFileStorage)
end
it "should use specific index storage if told so" do
StrokeDB.send!(:remove_const,'SomeFunnyIndexStorage') if defined?(SomeFunnyIndexStorage)
StrokeDB::SomeFunnyIndexStorage = Class.new(InvertedListFileStorage)
config = StrokeDB::Config.build :index_storages => [:some_funny_index], :base_path => @base_path
config.storages[:some_funny_index].should be_a_kind_of(SomeFunnyIndexStorage)
end
it "should initialize index storage with base_path+storage_name" do
StrokeDB.send!(:remove_const,'SomeFunnyIndexStorage') if defined?(SomeFunnyIndexStorage)
StrokeDB::SomeFunnyIndexStorage = Class.new(InvertedListFileStorage)
index_storage = StrokeDB::SomeFunnyIndexStorage.new(:path => @base_path + '/some_funny_index')
SomeFunnyIndexStorage.should_receive(:new).with(:path => @base_path + '/some_funny_index').and_return(index_storage)
config = StrokeDB::Config.build :index_storages => [:some_funny_index], :base_path => @base_path
end
it "should dump config in base_path (except 'default' key)" do
cfg = StrokeDB::Config.build :default => true, :base_path => @base_path
config = JSON.parse(IO.read(@base_path + '/config'))
config.should == cfg.build_config
end
it "should load dumped config" do
cfg = StrokeDB::Config.build :default => true, :base_path => @base_path
cfg.storages.values.each {|s| s.close! if s.respond_to?(:close!)}
config = JSON.parse(IO.read(@base_path + '/config'))
cfg = StrokeDB::Config.load(@base_path + '/config')
cfg.build_config.should == config
cfg.should_not == StrokeDB.default_config
end
it "should load dumped config and make it default if told so" do
cfg = StrokeDB::Config.build :default => true, :base_path => @base_path
cfg.storages.values.each {|s| s.close! if s.respond_to?(:close!)}
config = JSON.parse(IO.read(@base_path + '/config'))
cfg = StrokeDB::Config.load(@base_path + '/config',true)
cfg.build_config.should == config
cfg.should == StrokeDB.default_config
end
end