perobs 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,10 +23,7 @@
23
23
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
24
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
25
 
26
- $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
27
-
28
- require 'fileutils'
29
- require 'time'
26
+ require 'spec_helper'
30
27
  require 'perobs'
31
28
 
32
29
  class POSError < RuntimeError
@@ -61,47 +58,51 @@ end
61
58
  describe PEROBS::Store do
62
59
 
63
60
  before(:all) do
64
- FileUtils.rm_rf('test_db')
61
+ @db_file = generate_db_name(__FILE__)
65
62
  end
66
63
 
67
64
  after(:each) do
68
65
  @store.gc
69
- lambda { @store.check }.should_not raise_error
70
- lambda { @store.delete_store }.should_not raise_error
66
+ expect { @store.check }.to_not raise_error
67
+ expect { @store.delete_store }.to_not raise_error
68
+ end
69
+
70
+ after(:all) do
71
+ FileUtils.rm_rf(@db_file)
71
72
  end
72
73
 
73
74
  it 'should @store simple objects' do
74
- @store = PEROBS::Store.new('test_db', { :serializer => :yaml })
75
- @store['john'] = john = Person.new(@store)
75
+ @store = PEROBS::Store.new(@db_file, { :serializer => :yaml })
76
+ @store['john'] = john = @store.new(Person)
76
77
  john.name = 'John'
77
78
  john.zip = 4060
78
79
  john.bmi = 25.5
79
- @store['jane'] = jane = Person.new(@store)
80
+ @store['jane'] = jane = @store.new(Person)
80
81
  jane.name = 'Jane'
81
82
  jane.related = john
82
83
  jane.married = true
83
84
  jane.relatives = 'test'
84
85
 
85
- john.name.should == 'John'
86
- john.zip.should == 4060
87
- john.bmi.should == 25.5
88
- john.married.should be_false
89
- john.related.should be_nil
86
+ expect(john.name).to eq('John')
87
+ expect(john.zip).to eq(4060)
88
+ expect(john.bmi).to eq(25.5)
89
+ expect(john.married).to be false
90
+ expect(john.related).to be_nil
90
91
  jane = @store['jane']
91
- jane.name.should == 'Jane'
92
- jane.related.should == john
93
- jane.married.should be_true
92
+ expect(jane.name).to eq('Jane')
93
+ expect(jane.related).to eq(john)
94
+ expect(jane.married).to be true
94
95
  end
95
96
 
96
97
  it 'should @store and retrieve simple objects' do
97
98
  [ :marshal, :json, :yaml ].each do |serializer|
98
- FileUtils.rm_rf('test_db')
99
- @store = PEROBS::Store.new('test_db', { :serializer => serializer })
100
- @store['john'] = john = Person.new(@store)
99
+ FileUtils.rm_rf(@db_file)
100
+ @store = PEROBS::Store.new(@db_file, { :serializer => serializer })
101
+ @store['john'] = john = @store.new(Person)
101
102
  john.name = 'John'
102
103
  john.zip = 4060
103
104
  john.bmi = 25.5
104
- @store['jane'] = jane = Person.new(@store)
105
+ @store['jane'] = jane = @store.new(Person)
105
106
  jane.name = 'Jane'
106
107
  jane.related = john
107
108
  jane.married = true
@@ -109,44 +110,49 @@ describe PEROBS::Store do
109
110
 
110
111
  @store.sync
111
112
 
112
- @store = PEROBS::Store.new('test_db')
113
+ @store = PEROBS::Store.new(@db_file)
113
114
  john = @store['john']
114
- john.name.should == 'John'
115
- john.zip.should == 4060
116
- john.bmi.should == 25.5
117
- john.married.should be_false
118
- john.related.should be_nil
115
+ expect(john.name).to eq('John')
116
+ expect(john.zip).to eq(4060)
117
+ expect(john.bmi).to eq(25.5)
118
+ expect(john.married).to be false
119
+ expect(john.related).to be_nil
119
120
  jane = @store['jane']
120
- jane.name.should == 'Jane'
121
- jane.related.should == john
122
- jane.married.should be_true
121
+ expect(jane.name).to eq('Jane')
122
+ expect(jane.related).to eq(john)
123
+ expect(jane.married).to be true
123
124
  end
124
125
  end
125
126
 
127
+ it 'should not allow calls to BasicObject.new()' do
128
+ @store = PEROBS::Store.new(@db_file)
129
+ expect { Person.new(@store) }.to raise_error RuntimeError
130
+ end
131
+
126
132
  it 'should flush cached objects when necessary' do
127
- @store = PEROBS::Store.new('test_db', :cache_bits => 3)
133
+ @store = PEROBS::Store.new(@db_file, :cache_bits => 3)
128
134
  last_obj = nil
129
135
  0.upto(20) do |i|
130
- @store["person#{i}"] = obj = Person.new(@store)
131
- @store["person#{i}"].should == obj
136
+ @store["person#{i}"] = obj = @store.new(Person)
137
+ expect(@store["person#{i}"]).to eq(obj)
132
138
  obj.name = "Person #{i}"
133
- obj.name.should == "Person #{i}"
139
+ expect(obj.name).to eq("Person #{i}")
134
140
  obj.related = last_obj
135
- obj.related.should == last_obj
141
+ expect(obj.related).to eq(last_obj)
136
142
  last_obj = obj
137
143
  end
138
144
  0.upto(20) do |i|
139
- @store["person#{i}"].name.should == "Person #{i}"
145
+ expect(@store["person#{i}"].name).to eq("Person #{i}")
140
146
  end
141
147
  end
142
148
 
143
149
  it 'should support renaming of classes' do
144
- @store = PEROBS::Store.new('test_db')
145
- @store['john'] = john = Person.new(@store)
150
+ @store = PEROBS::Store.new(@db_file)
151
+ @store['john'] = john = @store.new(Person)
146
152
  john.name = 'John'
147
153
  john.zip = 4060
148
154
  john.bmi = 25.5
149
- @store['jane'] = jane = Person.new(@store)
155
+ @store['jane'] = jane = @store.new(Person)
150
156
  jane.name = 'Jane'
151
157
  jane.related = john
152
158
  jane.married = true
@@ -154,142 +160,142 @@ describe PEROBS::Store do
154
160
 
155
161
  @store.sync
156
162
 
157
- @store = PEROBS::Store.new('test_db')
163
+ @store = PEROBS::Store.new(@db_file)
158
164
  @store.rename_classes({ 'Person' => 'PersonN' })
159
165
  john = @store['john']
160
- john.name.should == 'John'
161
- john.zip.should == 4060
162
- john.bmi.should == 25.5
163
- john.married.should be_false
164
- john.related.should be_nil
166
+ expect(john.name).to eq('John')
167
+ expect(john.zip).to eq(4060)
168
+ expect(john.bmi).to eq(25.5)
169
+ expect(john.married).to be false
170
+ expect(john.related).to be_nil
165
171
  jane = @store['jane']
166
- jane.name.should == 'Jane'
167
- jane.related.should == john
168
- jane.married.should be_true
172
+ expect(jane.name).to eq('Jane')
173
+ expect(jane.related).to eq(john)
174
+ expect(jane.married).to be true
169
175
  end
170
176
 
171
177
  it 'should detect modification to non-working objects' do
172
- @store = PEROBS::Store.new('test_db', :cache_bits => 3)
178
+ @store = PEROBS::Store.new(@db_file, :cache_bits => 3)
173
179
  0.upto(20) do |i|
174
- @store["person#{i}"] = obj = Person.new(@store)
180
+ @store["person#{i}"] = obj = @store.new(Person)
175
181
  obj.name = "Person #{i}"
176
182
  end
177
183
  0.upto(20) do |i|
178
184
  @store["person#{i}"].name = "New Person #{i}"
179
185
  end
180
186
  @store.sync
181
- @store = PEROBS::Store.new('test_db')
187
+ @store = PEROBS::Store.new(@db_file)
182
188
  0.upto(20) do |i|
183
- @store["person#{i}"].name.should == "New Person #{i}"
189
+ expect(@store["person#{i}"].name).to eq("New Person #{i}")
184
190
  end
185
191
  end
186
192
 
187
193
  it 'should garbage collect unlinked objects' do
188
- @store = PEROBS::Store.new('test_db')
189
- @store['person1'] = obj = Person.new(@store)
194
+ @store = PEROBS::Store.new(@db_file)
195
+ @store['person1'] = obj = @store.new(Person)
190
196
  id1 = obj._id
191
- @store['person2'] = obj = Person.new(@store)
197
+ @store['person2'] = obj = @store.new(Person)
192
198
  id2 = obj._id
193
- obj.related = obj = Person.new(@store)
199
+ obj.related = obj = @store.new(Person)
194
200
  id3 = obj._id
195
201
  @store.sync
196
202
  @store['person1'] = nil
197
203
  @store.gc
198
- @store = PEROBS::Store.new('test_db')
199
- @store.object_by_id(id1).should be_nil
200
- @store['person2']._id.should == id2
201
- @store['person2'].related._id.should == id3
204
+ @store = PEROBS::Store.new(@db_file)
205
+ expect(@store.object_by_id(id1)).to be_nil
206
+ expect(@store['person2']._id).to eq(id2)
207
+ expect(@store['person2'].related._id).to eq(id3)
202
208
  end
203
209
 
204
210
  it 'should handle cyclicly linked objects' do
205
- @store = PEROBS::Store.new('test_db')
206
- @store['person0'] = p0 = Person.new(@store)
211
+ @store = PEROBS::Store.new(@db_file)
212
+ @store['person0'] = p0 = @store.new(Person)
207
213
  id0 = p0._id
208
- p1 = Person.new(@store)
214
+ p1 = @store.new(Person)
209
215
  id1 = p1._id
210
- p2 = Person.new(@store)
216
+ p2 = @store.new(Person)
211
217
  id2 = p2._id
212
218
  p1.related = p2
213
219
  p2.related = p1
214
220
  p0.related = p1
215
221
  @store.sync
216
222
  @store.gc
217
- @store = PEROBS::Store.new('test_db')
218
- @store['person0']._id.should == id0
219
- @store['person0'].related._id.should == id1
220
- @store['person0'].related.related._id.should == id2
223
+ @store = PEROBS::Store.new(@db_file)
224
+ expect(@store['person0']._id).to eq(id0)
225
+ expect(@store['person0'].related._id).to eq(id1)
226
+ expect(@store['person0'].related.related._id).to eq(id2)
221
227
 
222
228
  @store['person0'].related = nil
223
229
  @store.gc
224
- @store.object_by_id(id1).should be_nil
225
- @store.object_by_id(id2).should be_nil
230
+ expect(@store.object_by_id(id1)).to be_nil
231
+ expect(@store.object_by_id(id2)).to be_nil
226
232
 
227
- @store = PEROBS::Store.new('test_db')
228
- @store.object_by_id(id1).should be_nil
229
- @store.object_by_id(id2).should be_nil
233
+ @store = PEROBS::Store.new(@db_file)
234
+ expect(@store.object_by_id(id1)).to be_nil
235
+ expect(@store.object_by_id(id2)).to be_nil
230
236
  end
231
237
 
232
238
  it 'should support a successful transaction' do
233
- @store = PEROBS::Store.new('test_db')
239
+ @store = PEROBS::Store.new(@db_file)
234
240
  @store.transaction do
235
- @store['person0'] = p0 = Person.new(@store)
241
+ @store['person0'] = p0 = @store.new(Person)
236
242
  p0.name = 'Jimmy'
237
243
  end
238
- @store['person0'].name.should == 'Jimmy'
244
+ expect(@store['person0'].name).to eq('Jimmy')
239
245
  end
240
246
 
241
247
  it 'should handle a failed transaction 1' do
242
- @store = PEROBS::Store.new('test_db')
248
+ @store = PEROBS::Store.new(@db_file)
243
249
  begin
244
250
  @store.transaction do
245
- @store['person0'] = p0 = Person.new(@store)
251
+ @store['person0'] = p0 = @store.new(Person)
246
252
  p0.name = 'Jimmy'
247
253
  raise POSError
248
254
  end
249
255
  rescue POSError
250
256
  end
251
- @store['person0'].should be_nil
257
+ expect(@store['person0']).to be_nil
252
258
  end
253
259
 
254
260
  it 'should handle a failed transaction 2' do
255
- @store = PEROBS::Store.new('test_db')
256
- @store['person1'] = p1 = Person.new(@store)
261
+ @store = PEROBS::Store.new(@db_file)
262
+ @store['person1'] = p1 = @store.new(Person)
257
263
  p1.name = 'Joe'
258
264
  begin
259
265
  @store.transaction do
260
- @store['person0'] = p0 = Person.new(@store)
266
+ @store['person0'] = p0 = @store.new(Person)
261
267
  p0.name = 'Jimmy'
262
268
  raise POSError
263
269
  end
264
270
  rescue POSError
265
271
  end
266
- @store['person1'].name.should == 'Joe'
267
- @store['person0'].should be_nil
272
+ expect(@store['person1'].name).to eq('Joe')
273
+ expect(@store['person0']).to be_nil
268
274
  end
269
275
 
270
276
  it 'should support a successful nested transaction' do
271
- @store = PEROBS::Store.new('test_db')
277
+ @store = PEROBS::Store.new(@db_file)
272
278
  @store.transaction do
273
- @store['person0'] = p0 = Person.new(@store)
279
+ @store['person0'] = p0 = @store.new(Person)
274
280
  p0.name = 'Jimmy'
275
281
  @store.transaction do
276
- @store['person1'] = p1 = Person.new(@store)
282
+ @store['person1'] = p1 = @store.new(Person)
277
283
  p1.name = 'Joe'
278
284
  end
279
285
  end
280
- @store['person0'].name.should == 'Jimmy'
281
- @store['person1'].name.should == 'Joe'
286
+ expect(@store['person0'].name).to eq('Jimmy')
287
+ expect(@store['person1'].name).to eq('Joe')
282
288
  end
283
289
 
284
290
  it 'should handle a failed nested transaction 1' do
285
- @store = PEROBS::Store.new('test_db')
291
+ @store = PEROBS::Store.new(@db_file)
286
292
  begin
287
293
  @store.transaction do
288
- @store['person0'] = p0 = Person.new(@store)
294
+ @store['person0'] = p0 = @store.new(Person)
289
295
  p0.name = 'Jimmy'
290
296
  begin
291
297
  @store.transaction do
292
- @store['person1'] = p1 = Person.new(@store)
298
+ @store['person1'] = p1 = @store.new(Person)
293
299
  p1.name = 'Joe'
294
300
  raise POSError
295
301
  end
@@ -298,58 +304,58 @@ describe PEROBS::Store do
298
304
  end
299
305
  rescue POSError
300
306
  end
301
- @store['person0'].name.should == 'Jimmy'
302
- @store['person1'].should be_nil
307
+ expect(@store['person0'].name).to eq('Jimmy')
308
+ expect(@store['person1']).to be_nil
303
309
  end
304
310
 
305
311
  it 'should handle a failed nested transaction 2' do
306
- @store = PEROBS::Store.new('test_db')
312
+ @store = PEROBS::Store.new(@db_file)
307
313
  begin
308
314
  @store.transaction do
309
- @store['person0'] = p0 = Person.new(@store)
315
+ @store['person0'] = p0 = @store.new(Person)
310
316
  p0.name = 'Jimmy'
311
317
  @store.transaction do
312
- @store['person1'] = p1 = Person.new(@store)
318
+ @store['person1'] = p1 = @store.new(Person)
313
319
  p1.name = 'Joe'
314
320
  end
315
321
  raise POSError
316
322
  end
317
323
  rescue POSError
318
324
  end
319
- @store['person0'].should be_nil
320
- @store['person1'].should be_nil
325
+ expect(@store['person0']).to be_nil
326
+ expect(@store['person1']).to be_nil
321
327
  end
322
328
 
323
329
  it 'should support a successful 2-level nested transaction' do
324
- @store = PEROBS::Store.new('test_db')
330
+ @store = PEROBS::Store.new(@db_file)
325
331
  @store.transaction do
326
- @store['person0'] = p0 = Person.new(@store)
332
+ @store['person0'] = p0 = @store.new(Person)
327
333
  p0.name = 'Jimmy'
328
334
  @store.transaction do
329
- @store['person1'] = p1 = Person.new(@store)
335
+ @store['person1'] = p1 = @store.new(Person)
330
336
  p1.name = 'Joe'
331
337
  @store.transaction do
332
- @store['person2'] = p2 = Person.new(@store)
338
+ @store['person2'] = p2 = @store.new(Person)
333
339
  p2.name = 'Jane'
334
340
  end
335
341
  end
336
342
  end
337
- @store['person0'].name.should == 'Jimmy'
338
- @store['person1'].name.should == 'Joe'
339
- @store['person2'].name.should == 'Jane'
343
+ expect(@store['person0'].name).to eq('Jimmy')
344
+ expect(@store['person1'].name).to eq('Joe')
345
+ expect(@store['person2'].name).to eq('Jane')
340
346
  end
341
347
 
342
348
  it 'should handle a failed 2-level nested transaction 1' do
343
- @store = PEROBS::Store.new('test_db')
349
+ @store = PEROBS::Store.new(@db_file)
344
350
  @store.transaction do
345
- @store['person0'] = p0 = Person.new(@store)
351
+ @store['person0'] = p0 = @store.new(Person)
346
352
  p0.name = 'Jimmy'
347
353
  @store.transaction do
348
- @store['person1'] = p1 = Person.new(@store)
354
+ @store['person1'] = p1 = @store.new(Person)
349
355
  p1.name = 'Joe'
350
356
  begin
351
357
  @store.transaction do
352
- @store['person2'] = p2 = Person.new(@store)
358
+ @store['person2'] = p2 = @store.new(Person)
353
359
  p2.name = 'Jane'
354
360
  raise POSError
355
361
  end
@@ -357,22 +363,22 @@ describe PEROBS::Store do
357
363
  end
358
364
  end
359
365
  end
360
- @store['person0'].name.should == 'Jimmy'
361
- @store['person1'].name.should == 'Joe'
362
- @store['person2'].should be_nil
366
+ expect(@store['person0'].name).to eq('Jimmy')
367
+ expect(@store['person1'].name).to eq('Joe')
368
+ expect(@store['person2']).to be_nil
363
369
  end
364
370
 
365
371
  it 'should handle a failed 2-level nested transaction 2' do
366
- @store = PEROBS::Store.new('test_db')
372
+ @store = PEROBS::Store.new(@db_file)
367
373
  @store.transaction do
368
- @store['person0'] = p0 = Person.new(@store)
374
+ @store['person0'] = p0 = @store.new(Person)
369
375
  p0.name = 'Jimmy'
370
376
  @store.transaction do
371
- @store['person1'] = p1 = Person.new(@store)
377
+ @store['person1'] = p1 = @store.new(Person)
372
378
  p1.name = 'Joe'
373
379
  begin
374
380
  @store.transaction do
375
- @store['person2'] = p2 = Person.new(@store)
381
+ @store['person2'] = p2 = @store.new(Person)
376
382
  p2.name = 'Jane'
377
383
  raise POSError
378
384
  end
@@ -381,14 +387,14 @@ describe PEROBS::Store do
381
387
  p1.name = 'Jane'
382
388
  end
383
389
  end
384
- @store['person0'].name.should == 'Jimmy'
385
- @store['person1'].name.should == 'Jane'
386
- @store['person2'].should be_nil
390
+ expect(@store['person0'].name).to eq('Jimmy')
391
+ expect(@store['person1'].name).to eq('Jane')
392
+ expect(@store['person2']).to be_nil
387
393
  end
388
394
 
389
395
  it 'should survive a real world usage test' do
390
396
  options = { :engine => PEROBS::BTreeDB, :dir_bits => 4 }
391
- @store = PEROBS::Store.new('test_db', options)
397
+ @store = PEROBS::Store.new(@db_file, options)
392
398
  ref = {}
393
399
 
394
400
  0.upto(2000) do |i|
@@ -396,13 +402,13 @@ describe PEROBS::Store do
396
402
  case i % 8
397
403
  when 0
398
404
  value = 'A' * rand(512)
399
- @store[key] = p = Person.new(@store)
405
+ @store[key] = p = @store.new(Person)
400
406
  p.name = value
401
407
  ref[key] = value
402
408
  @store.sync
403
409
  when 1
404
410
  value = 'B' * rand(128)
405
- @store[key] = p = Person.new(@store)
411
+ @store[key] = p = @store.new(Person)
406
412
  p.name = value
407
413
  ref[key] = value
408
414
  when 2
@@ -417,14 +423,14 @@ describe PEROBS::Store do
417
423
  when 4
418
424
  if rand(15) == 0
419
425
  @store.sync
420
- @store = PEROBS::Store.new('test_db', options)
426
+ @store = PEROBS::Store.new(@db_file, options)
421
427
  end
422
428
  when 5
423
429
  index = i - rand(10)
424
430
  if rand(3) == 0 && index >= 0
425
431
  key = "o#{i - rand(10)}"
426
432
  value = 'C' * rand(1024)
427
- @store[key] = p = Person.new(@store)
433
+ @store[key] = p = @store.new(Person)
428
434
  p.name = value
429
435
  ref[key] = value
430
436
  end
@@ -436,17 +442,17 @@ describe PEROBS::Store do
436
442
  when 7
437
443
  index = rand(i)
438
444
  if ref[key]
439
- @store[key].name.should == ref[key]
445
+ expect(@store[key].name).to eq(ref[key])
440
446
  end
441
447
  end
442
448
 
443
449
  if ref[key]
444
- @store[key].name.should == ref[key]
450
+ expect(@store[key].name).to eq(ref[key])
445
451
  end
446
452
  end
447
453
 
448
454
  ref.each do |k, v|
449
- @store[k].name.should == v
455
+ expect(@store[k].name).to eq(v)
450
456
  end
451
457
  end
452
458