/
relaxdb_spec.rb
372 lines (301 loc) · 10.6 KB
/
relaxdb_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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
require File.dirname(__FILE__) + '/spec_helper.rb'
require File.dirname(__FILE__) + '/spec_models.rb'
describe RelaxDB do
before(:each) do
setup_test_db
end
describe ".create_object" do
it "should return an instance of a known object if passed a hash with a class key" do
data = { "relaxdb_class" => "Item" }
obj = RelaxDB.create_object(data)
obj.should be_instance_of(Item)
end
it "should return an instance of a dynamically created object if no class key is provided" do
data = { "name" => "tesla coil", "strength" => 5000 }
obj = RelaxDB.create_object(data)
obj.name.should == "tesla coil"
obj.strength.should == 5000
end
end
# bulk_save and bulk_save! should match Document#save and Document#save! semantics
describe ".bulk_save" do
it "should be invokable multiple times" do
t1, t2 = Tag.new, Tag.new
RelaxDB.bulk_save(t1, t2)
RelaxDB.bulk_save(t1, t2)
end
it "should return the objects it was passed" do
t1, t2 = Tag.new, Tag.new
ta, tb = RelaxDB.bulk_save(t1, t2)
ta.should == t1
tb.should == t2
end
it "should return false on failure" do
c = Class.new(RelaxDB::Document) do
property :foo, :validator => lambda { false }
end
x = c.new
RelaxDB.bulk_save(x).should be_false
end
it "should not attempt to save if a pre-save stage fails" do
c = Class.new(RelaxDB::Document) do
property :foo, :validator => lambda { false }
end
x = c.new
RelaxDB.bulk_save(x)
x.should be_new_document
end
it "should invoke the after-save stage after a successful save" do
c = Class.new(RelaxDB::Document) do
attr_accessor :foo
after_save lambda { |c| c.foo = :bar }
end
x = c.new
RelaxDB.bulk_save(x).first.foo.should == :bar
end
it "should save non conflicting docs and mark conflicting docs" do
p1, p2 = Atom.new.save!, Atom.new.save!
p1.dup.save!
RelaxDB.bulk_save p1, p2
p1._rev.should =~ /1-/
p1.should be_update_conflict
p2._rev.should =~ /2-/
end
#
# This spec is as much a verification of my understanding of
# bulk_save semantics as it is a test of RelaxDB
#
# See http://mail-archives.apache.org/mod_mbox/couchdb-dev/200905.mbox/%3CF476A3D8-8F50-40A0-8668-C00D72196FBA@apache.org%3E
# for an explanation of the final section
#
describe "all-or-nothing" do
it "should save non conflicting and conflicting docs" do
p1, p2 = Primitives.new(:num => 1).save!, Primitives.new(:num => 2).save!
p1d = p1.dup
p1d.num = 11
p1d.save!
p1.num = 6
RelaxDB.bulk_save :all_or_nothing, p1, p2
p1._rev.should =~ /2-/
p2._rev.should =~ /2-/
p1 = RelaxDB.load p1._id, :conflicts => true
p1n1 = p1.num
p1 = RelaxDB.load p1._id, :rev => p1._conflicts[0]
p1n2 = p1.num
if p1n1 == 11
p1n2.should == 6
else
p1n1.should == 6 && p1n2.should == 11
end
end
#
# Test behind
# http://mail-archives.apache.org/mod_mbox/couchdb-dev/200905.mbox/%3CF476A3D8-8F50-40A0-8668-C00D72196FBA@apache.org%3E
# Effectively defunct
#
# it "non-deterministic winner" do
# p = Primitives.new(:num => 1).save!
# pd = p.dup
# p.num = 2
# p.save!
# pd.num = 3
# RelaxDB.bulk_save :all_or_nothing, pd
# RelaxDB.reload(p).num.should == 2
# end
end
end
describe ".bulk_save!" do
it "should succeed when passed no args" do
RelaxDB.bulk_save!
end
it "should raise when passed a nil value" do
lambda do
RelaxDB.bulk_save! *[nil]
end.should raise_error
end
it "should raise an exception if a obj fails validation" do
c = Class.new(RelaxDB::Document) do
property :foo, :validator => lambda { false }
end
lambda { RelaxDB.bulk_save!(c.new) }.should raise_error(RelaxDB::ValidationFailure)
end
it "should raise an exception on document conflict after all docs have been processed" do
p1, p2 = Atom.new.save!, Atom.new.save!
p1.dup.save!
lambda { RelaxDB.bulk_save!(p1, p2) }.should raise_error(RelaxDB::UpdateConflict)
p2._rev.should =~ /2-/
end
end
describe ".db_info" do
it "should return db info" do
RelaxDB.db_info.doc_count.should == 1
end
end
describe ".replicate_db" do
it "should replicate the named database" do
orig = "relaxdb_spec"
replica = "relaxdb_spec_replicate_test"
RelaxDB.delete_db replica rescue :ok
RelaxDB.enable_view_creation
class ::ReplicaTest < RelaxDB::Document; end
RelaxDB::View.design_doc.save
ReplicaTest.new.save # implicitly saved to orig
RelaxDB.replicate_db orig, replica
RelaxDB.use_db replica
ReplicaTest.all.size.should == 1
end
end
describe ".load" do
it "should load a single document" do
a = Atom.new.save
ar = RelaxDB.load a._id
ar.should == a
end
it "should load an arbitrary number of documents" do
a1, a2 = Atom.new.save, Atom.new.save
ar1, ar2 = RelaxDB.load [a1._id, a2._id]
ar1.should == a1
ar2.should == a2
end
it "should return nil when given a id for a non existant doc" do
RelaxDB.load("nothere").should be_nil
end
it "should return an array with correctly placed nils when given a list containing non existant doc ids" do
a1, a2 = Atom.new.save, Atom.new.save
res = RelaxDB.load [nil, a1._id, nil, a2._id, nil]
res.should == [nil, a1, nil, a2, nil]
end
end
describe ".load!" do
it "should load a single document" do
a = Atom.new.save
ar = RelaxDB.load! a._id
ar.should == a
end
it "should load multiple documents" do
a1, a2 = Atom.new.save, Atom.new.save
ar1, ar2 = RelaxDB.load! [a1._id, a2._id]
ar1.should == a1
ar2.should == a2
end
it "should load multiple documents in order" do
ns = (0...100).map { rand(1_000_000_000).to_s }
objs = ns.map { |n| Primitives.new :_id => n }
RelaxDB.bulk_save! *objs
ns = ns.reverse
objs = RelaxDB.load! ns
99.downto(0) do |i|
ns[i].should == objs[i]._id
end
end
it "should throw an exception if given a single id for a non-existant doc" do
lambda do
RelaxDB.load! "nothere"
end.should raise_error(RelaxDB::NotFound)
end
it "should throw an exception if any of a list of doc ids is for a non-existant doc" do
a = Atom.new.save
lambda do
RelaxDB.load! [nil, a._id]
end.should raise_error(RelaxDB::NotFound)
end
end
describe ".view" do
map_func = %Q<
function (doc) {
emit(doc._id, doc);
}
>
it "should request a view and return an array" do
RelaxDB::DesignDocument.get(RelaxDB.dd).add_view("simple", "map", map_func).save
data = RelaxDB.view("simple")
data.should == []
end
it "may accept query params" do
RelaxDB::DesignDocument.get(RelaxDB.dd).add_view("simple", "map", map_func).save
RelaxDB.db.put("x", {}.to_json)
RelaxDB.db.put("y", {}.to_json)
res = RelaxDB.view "simple", :key => "x"
res.first._id.should == "x"
end
it "should be queryable with a multi key post" do
Primitives.view_docs_by :num
5.times { |i| Primitives.new(:num => i).save }
Primitives.by_num
result = RelaxDB.view "Primitives_by_num", :keys => [0, 4], :reduce => false
result.map{ |p| p.num }.should == [0, 4]
end
it "should return nil for a reduce view with no results" do
Primitives.view_docs_by :num
RelaxDB.view("Primitives_by_num", :reduce => true).should be_nil
end
it "should return a single value for a reduce view with a single result" do
Primitives.view_docs_by :num
Primitives.new(:num => :x).save!
RelaxDB.view("Primitives_by_num", :reduce => true).should == 1
end
it "should return an array for a reduce view with multiple results" do
Primitives.view_docs_by :num
2.times { |i| Primitives.new(:num => i).save! }
res = RelaxDB.view("Primitives_by_num", :reduce => true, :group => true)
res.should be_an_instance_of(Array)
res.size.should == 2
end
end
describe ".merge" do
it "should merge rows sharing a common merge key into a single ViewObject" do
rows = [
{"value" => {"sculptor_id" => 1, "sculpture_name" => "strandbeesten"} },
{"value" => {"sculptor_id" => 1, "sculptor_name" => "hans"} },
{"value" => {"sculptor_id" => 2, "sculpture_name" => "parading dogs"} },
{"value" => {"sculptor_id" => 2, "sculptor_name" => "holmes"} }
]
data = {"rows" => rows}
result = RelaxDB.merge(data, "sculptor_id")
result = result.sort { |a, b| a.sculptor_name <=> b.sculptor_name }
result[0].sculptor_name.should == "hans"
result[0].sculpture_name.should == "strandbeesten"
result[1].sculptor_name.should == "holmes"
result[1].sculpture_name.should == "parading dogs"
end
end
describe "create_views disabled" do
before(:each) do
create_test_db
RelaxDB.enable_view_creation false
class CvdBar < RelaxDB::Document
view_docs_by :foo
has_one :foo1
has_many :foon
references_many :foor
end
end
it "should not create any views" do
dd = RelaxDB::DesignDocument.get "spec_doc"
dd.data["views"].should be_nil
end
end
describe "create_views enabled" do
before(:each) do
create_test_db
RelaxDB.enable_view_creation
class ::CveBar < RelaxDB::Document
view_docs_by :foo
has_one :foo1
has_many :foon
references_many :foor
end
RelaxDB::View.design_doc.save
end
it "should create all views" do
dd = RelaxDB::DesignDocument.get "spec_doc"
dd.data["views"]["CveBar_all"].should be
dd.data["views"]["CveBar_by_foo"].should be
dd.data["views"]["CveBar_foo1"].should be
dd.data["views"]["CveBar_foon"].should be
dd.data["views"]["CveBar_foor"].should be
end
end
# if caching is added
# it "should offer an example where behaviour is different with caching enabled and caching disabled"
end