Module: RIMS::Test::KeyValueStoreTestUtility

Defined in:
lib/rims/test.rb

Instance Method Summary collapse

Instance Method Details

#db_closeObject



247
248
249
# File 'lib/rims/test.rb', line 247

def db_close
  @db.close
end

#db_closed?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/rims/test.rb', line 251

def db_closed?
  @db.closed?
end

#db_closed_delete_errorObject



282
283
284
# File 'lib/rims/test.rb', line 282

def db_closed_delete_error
  db_closed_error
end

#db_closed_each_key_errorObject



290
291
292
# File 'lib/rims/test.rb', line 290

def db_closed_each_key_error
  db_closed_error
end

#db_closed_each_pair_errorObject



298
299
300
# File 'lib/rims/test.rb', line 298

def db_closed_each_pair_error
  db_closed_error
end

#db_closed_each_value_errorObject



294
295
296
# File 'lib/rims/test.rb', line 294

def db_closed_each_value_error
  db_closed_error
end

#db_closed_errorObject



270
271
272
# File 'lib/rims/test.rb', line 270

def db_closed_error
  RuntimeError
end

#db_closed_fetch_errorObject



274
275
276
# File 'lib/rims/test.rb', line 274

def db_closed_fetch_error
  db_closed_error
end

#db_closed_key_errorObject



286
287
288
# File 'lib/rims/test.rb', line 286

def db_closed_key_error
  db_closed_error
end

#db_closed_store_errorObject



278
279
280
# File 'lib/rims/test.rb', line 278

def db_closed_store_error
  db_closed_error
end

#db_each_keyObject



263
264
265
266
267
268
# File 'lib/rims/test.rb', line 263

def db_each_key
  return enum_for(:db_each_key) unless block_given?
  @db.each_key do |key|
    yield(key)
  end
end

#db_fetch(key) ⇒ Object



255
256
257
# File 'lib/rims/test.rb', line 255

def db_fetch(key)
  @db[key]
end

#db_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/rims/test.rb', line 259

def db_key?(key)
  @db.key? key
end

#make_key_value_storeObject

Raises:

  • (NotImplementedError)


243
244
245
# File 'lib/rims/test.rb', line 243

def make_key_value_store
  raise NotImplementedError, 'not implemented.'
end

#open_databaseObject

Raises:

  • (NotImplementedError)


239
240
241
# File 'lib/rims/test.rb', line 239

def open_database
  raise NotImplementedError, "not implemented to open `#{@name}'"
end

#setupObject



302
303
304
305
306
307
308
309
# File 'lib/rims/test.rb', line 302

def setup
  @base_dir = 'kvs_test_dir'
  FileUtils.mkdir_p(@base_dir)
  @name = File.join(@base_dir, "kvs_test.db.#{$$}")

  @db = open_database
  @kvs = make_key_value_store
end

#teardownObject



311
312
313
314
# File 'lib/rims/test.rb', line 311

def teardown
  db_close unless db_closed?
  FileUtils.rm_rf(@base_dir)
end

#test_closeObject



398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/rims/test.rb', line 398

def test_close
  @kvs.close
  assert_equal(true, db_closed?)

  # closed exception
  assert_raise(db_closed_fetch_error) { @kvs['foo'] }
  assert_raise(db_closed_store_error) { @kvs['foo'] = 'apple' }
  assert_raise(db_closed_delete_error) { @kvs.delete('foo') }
  assert_raise(db_closed_key_error) { @kvs.key? 'foo' }
  assert_raise(db_closed_each_key_error) { @kvs.each_key.to_a }
  assert_raise(db_closed_each_value_error) { @kvs.each_value.to_a }
  assert_raise(db_closed_each_pair_error) { @kvs.each_pair.to_a }
end

#test_deleteObject



330
331
332
333
334
335
336
337
338
# File 'lib/rims/test.rb', line 330

def test_delete
  assert_nil(@kvs.delete('foo'))

  @kvs['foo'] = 'apple'
  assert_equal('apple', @kvs.delete('foo'))

  assert_nil(db_fetch('foo'))
  assert_nil(@kvs['foo'])
end

#test_each_keyObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/rims/test.rb', line 358

def test_each_key
  assert_equal(%w[], db_each_key.to_a)
  assert_equal(%w[], @kvs.each_key.to_a)

  @kvs['foo'] = 'apple'
  assert_equal(%w[ foo ], db_each_key.to_a)
  assert_equal(%w[ foo ], @kvs.each_key.to_a)
  assert_equal(%w[ apple ], @kvs.each_value.to_a)
  assert_equal([ %w[ foo apple ] ], @kvs.each_pair.to_a)

  @kvs['bar'] = 'banana'
  assert_equal(%w[ foo bar ].sort, db_each_key.sort)
  assert_equal(%w[ foo bar ].sort, @kvs.each_key.sort)
  assert_equal(%w[ apple banana ].sort, @kvs.each_value.sort)
  assert_equal([ %w[ foo apple ], %w[ bar banana ] ].sort, @kvs.each_pair.sort)

  @kvs['baz'] = 'orange'
  assert_equal(%w[ foo bar baz ].sort, db_each_key.sort)
  assert_equal(%w[ foo bar baz ].sort, @kvs.each_key.sort)
  assert_equal(%w[ apple banana orange ].sort, @kvs.each_value.sort)
  assert_equal([ %w[ foo apple ], %w[ bar banana ], %w[ baz orange ] ].sort, @kvs.each_pair.sort)

  @kvs.delete('bar')
  assert_equal(%w[ foo baz ].sort, db_each_key.sort)
  assert_equal(%w[ foo baz ].sort, @kvs.each_key.sort)
  assert_equal(%w[ apple orange ].sort, @kvs.each_value.sort)
  assert_equal([ %w[ foo apple ], %w[ baz orange ] ].sort, @kvs.each_pair.sort)

  # update
  @kvs['baz'] = 'melon'
  assert_equal(%w[ foo baz ].sort, db_each_key.sort)
  assert_equal(%w[ foo baz ].sort, @kvs.each_key.sort)
  assert_equal(%w[ apple melon ].sort, @kvs.each_value.sort)
  assert_equal([ %w[ foo apple ], %w[ baz melon ] ].sort, @kvs.each_pair.sort)
end

#test_key?Boolean

Returns:

  • (Boolean)


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/rims/test.rb', line 340

def test_key?
  assert_equal(false, (db_key? 'foo'))
  assert_equal(false, (@kvs.key? 'foo'))

  @kvs['foo'] = 'apple'
  assert_equal(true, (db_key? 'foo'))
  assert_equal(true, (@kvs.key? 'foo'))

  # update
  @kvs['foo'] = 'banana'
  assert_equal(true, (db_key? 'foo'))
  assert_equal(true, (@kvs.key? 'foo'))

  @kvs.delete('foo')
  assert_equal(false, (db_key? 'foo'))
  assert_equal(false, (@kvs.key? 'foo'))
end

#test_store_fetchObject



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rims/test.rb', line 316

def test_store_fetch
  assert_nil(db_fetch('foo'))
  assert_nil(@kvs['foo'])

  assert_equal('apple', (@kvs['foo'] = 'apple'))
  assert_equal('apple', db_fetch('foo'))
  assert_equal('apple', @kvs['foo'])

  # update
  assert_equal('banana', (@kvs['foo'] = 'banana'))
  assert_equal('banana', db_fetch('foo'))
  assert_equal('banana', @kvs['foo'])
end

#test_syncObject



394
395
396
# File 'lib/rims/test.rb', line 394

def test_sync
  @kvs.sync
end