/
meta_spec.rb
356 lines (280 loc) · 10.1 KB
/
meta_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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
require File.dirname(__FILE__) + '/spec_helper'
describe "Meta module", :shared => true do
it "should use Meta.default_nsurl if nsurl is not specified" do
Meta.default_nsurl = "http://some/"
SomeName.document.nsurl.should == "http://some/"
end
it "should not use Meta.default_nsurl if nsurl is specified" do
Meta.default_nsurl = "http://some/"
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new(:nsurl => "http://another/")
SomeName.document.nsurl.should == "http://another/"
end
it "should be able to instantiate new Document which is also SomeName" do
obj = SomeName.new
obj.should be_a_kind_of(Document)
obj.should be_a_kind_of(SomeName)
end
it "should be able to instantiate new Document and save it" do
new_doc = mock("new doc")
new_doc.should_receive(:save!)
SomeName.should_receive(:new).and_return(new_doc)
obj = SomeName.create!
end
it "should have corresponding document" do
doc = SomeName.document
doc.should_not be_nil
doc.should be_a_kind_of(Meta)
end
it "should find document instead of creating it" do
doc = SomeName.document
10.times {|i| SomeName.document.uuid.should == doc.uuid }
end
it "should save new document version if it was updated" do
doc = SomeName.document
version = doc.version.clone
new_doc = nil
2.times do |i|
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
@meta = Meta.new(:name => "SomeName", :description => "Something")
new_doc = SomeName.document
end
new_doc.uuid.should == doc.uuid
new_doc.previous_version.should_not be_nil
new_doc.previous_version.should == version
new_doc.version.should_not == new_doc.previous_version
new_doc.description.should == "Something"
end
it "should search for specified UUID with meta reference merged in" do
a = SomeName.create!
SomeName.find(a.uuid).should == a
end
it "should search for specified slots with meta reference merged in" do
a = SomeName.create!(:slot1 => 1, :slot2 => 2)
b = SomeName.create!(:slot1 => 1, :slot2 => 2)
c = SomeName.create!(:slot1 => 2, :slot2 => 2)
SomeName.find(:slot1 => 1, :slot2 =>2).sort_by {|d| d.uuid}.should == [a,b].sort_by {|d| d.uuid}
end
it "aliases Meta#all to Meta#find" do
a = SomeName.create!(:slot1 => 3)
SomeName.all(:slot1 => 3).should == SomeName.find(:slot1 => 3)
end
it "should return first found document matching given criteria on call to #first" do
a = SomeName.create!(:slot1 => 1)
b = SomeName.create!(:slot1 => 2)
SomeName.first(:slot1 => 1).should == a
end
it "should return first document if no args are passed to #first" do
a = SomeName.create!(:slot1 => 1)
b = SomeName.create!(:slot1 => 2)
SomeName.first.should == SomeName.find.first
end
it "correctly handles finding via UUID on call to #first" do
a = SomeName.create!(:slot1 => 5)
SomeName.first(a.uuid).should == a
end
it "should return last found document matching given criteria on call to #last" do
a = SomeName.create!(:slot1 => 1)
b = SomeName.create!(:slot1 => 1)
SomeName.last(:slot1 => 1).should == SomeName.find(:slot1 => 1).last
end
it "should return last document if no args are passed to #last" do
a = SomeName.create!(:slot1 => 1)
b = SomeName.create!(:slot1 => 2)
SomeName.last.should == SomeName.find.last
end
it "correctly handles finding via UUID on call to #last" do
a = SomeName.create!(:slot1 => 5)
SomeName.last(a.uuid).should == a
end
it "should raise ArgumentError unless args size is 1 or 2" do
a = SomeName.create!(:slot1 => 1, :slot2 => 2)
lambda { SomeName.find("foo","bar","foobar") }.should raise_error(ArgumentError)
end
it "should find first document for specified slots with meta reference merged in on #find_or_create" do
a = SomeName.create!(:slot1 => 1, :slot2 => 2)
b = SomeName.create!(:slot1 => 1, :slot2 => 2)
c = SomeName.create!(:slot1 => 2, :slot2 => 2)
search = SomeName.find_or_create(:slot1 => 1, :slot2 =>2)
(search == a || search == b).should be_true
end
it "should create document for specified slots with meta reference merged in on #find_or_create if such document was not found" do
a = SomeName.create!(:slot1 => 1, :slot2 => 2)
b = SomeName.create!(:slot1 => 1, :slot2 => 2)
c = SomeName.create!(:slot1 => 2, :slot2 => 2)
search = SomeName.find_or_create(:slot1 => 3, :slot2 =>2)
search.slot1.should == 3
search.should_not be_new
end
end
describe "Meta module with name" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new
end
it "should have document's UUID v5 based on nsurl and name" do
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new(:nsurl => "http://some/")
SomeName.document.uuid.should == Util.sha1_uuid('http://some/#SomeName')
end
it "should have specified UUID if it was specified" do
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
uuid = Util.random_uuid
SomeName = Meta.new(:nsurl => "http://some/", :uuid => uuid)
SomeName.document.uuid.should == uuid
end
it_should_behave_like "Meta module"
end
describe "Meta module without name" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
@some_meta = Meta.new(:nsurl => "http://some/")
end
it "should not have document's UUID v5 based on nsurl and name" do
@some_meta.document.uuid.should_not == Util.sha1_uuid('http://some/#SomeName')
end
end
describe "Meta module with on_initialization callback" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new do
on_initialization do |obj|
Kernel.send!(:on_initialization_called,obj.new?)
end
end
end
it "should receive this callback on meta instantiation" do
Kernel.should_receive(:on_initialization_called).with(true)
doc = SomeName.new
end
it "should be a sole meta receiving this callback when adding metas dynamically" do
Object.send!(:remove_const,'SomeOtherName') if defined?(SomeOtherName)
SomeOtherName = Meta.new do
on_initialization do |obj|
Kernel.send!(:other_on_initialization_called,obj.new?)
end
end
Kernel.should_receive(:other_on_initialization_called).with(true).once
doc = SomeOtherName.new
Kernel.should_receive(:on_initialization_called).with(true).once
doc.metas << SomeName
end
end
describe "Meta module with on_load callback" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new do
on_load do |obj|
Kernel.send!(:on_load_called,obj.new?)
end
end
end
it "should not receive this callback on meta instantiation" do
Kernel.should_not_receive(:on_load_called)
doc = SomeName.new
end
it "should receive this callback on document load" do
doc = SomeName.create!
Kernel.should_receive(:on_load_called).with(false)
SomeName.find(doc.uuid)
end
end
describe "Meta module with before_save callback" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new do
before_save do |obj|
Kernel.send!(:before_save_called,obj.new?)
end
end
end
it "should initiate callback on Document#save! (before actually saving it)" do
s = SomeName.new
Kernel.should_receive(:before_save_called).with(true)
s.save!
end
end
describe "Meta module with after_save callback" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new do
after_save do |obj|
Kernel.send!(:after_save_called,obj.new?)
end
end
end
it "should initiate callback on Document#save! (after actually saving it)" do
s = SomeName.new
Kernel.should_receive(:after_save_called).with(false)
s.save!
end
end
describe "Meta module with on_new_document callback" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new do
on_new_document do |obj|
Kernel.send!(:on_new_document,obj.new?)
end
end
end
it "should initiate callback on Document#new" do
Kernel.should_receive(:on_new_document).with(true)
s = SomeName.new
end
it "should not initiate callback on loaded Document" do
Kernel.should_receive(:on_new_document).with(true).once
s = SomeName.new
s.save!
s.reload
end
end
describe "Combined meta module" do
before(:each) do
setup_default_store
setup_index
Object.send!(:remove_const,'User') if defined?(User)
Object.send!(:remove_const,'Buyer') if defined?(Buyer)
Object.send!(:remove_const,'Seller') if defined?(Seller)
User = Meta.new(:x => 1)
Buyer = Meta.new(:x => 2)
Seller = Meta.new(:y => 3)
end
it "should initialize Document with all metas" do
d = (User+Buyer+Seller).new
d[:meta].should == [User.document,Buyer.document,Seller.document]
end
it "should be able to find respective documents" do
d0 = User.create!
User.find.should == [d0]
d1 = (User+Buyer).create!
(User+Buyer).find.should == [d1]
d2 = (User+Buyer+Seller).create!
(User+Buyer+Seller).find.should == [d2]
end
it "should merge #document" do
(User+Buyer+Seller).document.x.should == 2
(User+Buyer+Seller).document.y.should == 3
end
it "should make merged #document immutable" do
(User+Buyer+Seller).document.should be_a_kind_of(ImmutableDocument)
end
it "should raise error if trying to Meta + Document" do
user = User.create!
lambda { baddoc = (Buyer+user).create! }.should raise_error(RuntimeError)
end
end