test/test.rb in daybreak-0.2.0 vs test/test.rb in daybreak-0.2.1
- old
+ new
@@ -9,49 +9,67 @@
before do
@db = Daybreak::DB.new DB_PATH
end
it 'should insert' do
+ assert_equal @db[1], nil
+ assert_equal @db.include?(1), false
@db[1] = 1
assert_equal @db[1], 1
assert @db.has_key?(1)
@db[1] = '2'
assert_equal @db[1], '2'
assert_equal @db.length, 1
end
+ it 'should support batch inserts' do
+ @db.update(1 => :a, 2 => :b)
+ assert_equal @db[1], :a
+ assert_equal @db[2], :b
+ assert_equal @db.length, 2
+ end
+
it 'should persist values' do
@db['1'] = '4'
@db['4'] = '1'
- @db.sync
+ assert_equal @db.sync, @db
assert_equal @db['1'], '4'
db2 = Daybreak::DB.new DB_PATH
assert_equal db2['1'], '4'
assert_equal db2['4'], '1'
- db2.close
+ assert_equal db2.close, nil
end
+ it 'should persist after batch update' do
+ @db.update!(1 => :a, 2 => :b)
+
+ db2 = Daybreak::DB.new DB_PATH
+ assert_equal db2[1], :a
+ assert_equal db2[2], :b
+ assert_equal db2.close, nil
+ end
+
it 'should persist after clear' do
@db['1'] = 'xy'
- @db.clear
+ assert_equal @db.clear, @db
@db['1'] = '4'
@db['4'] = '1'
- @db.close
+ assert_equal @db.close, nil
@db = Daybreak::DB.new DB_PATH
assert_equal @db['1'], '4'
assert_equal @db['4'], '1'
end
it 'should persist after compact' do
@db['1'] = 'xy'
@db['1'] = 'z'
- @db.compact
+ assert_equal @db.compact, @db
@db['1'] = '4'
@db['4'] = '1'
- @db.close
+ assert_equal @db.close, nil
@db = Daybreak::DB.new DB_PATH
assert_equal @db['1'], '4'
assert_equal @db['4'], '1'
end
@@ -59,14 +77,14 @@
it 'should reload database file in sync after compact' do
db = Daybreak::DB.new DB_PATH
@db['1'] = 'xy'
@db['1'] = 'z'
- @db.compact
+ assert_equal @db.compact, @db
@db['1'] = '4'
@db['4'] = '1'
- @db.flush
+ assert_equal @db.flush, @db
db.sync
assert_equal db['1'], '4'
assert_equal db['4'], '1'
db.close
@@ -98,20 +116,28 @@
assert_equal @db[1], 1
assert size > File.stat(DB_PATH).size
end
it 'should allow for default values' do
- default_db = Daybreak::DB.new(DB_PATH, :default => 0)
- assert_equal default_db[1], 0
- default_db[1] = 1
- assert_equal default_db[1], 1
- default_db.close
+ db = Daybreak::DB.new(DB_PATH, :default => 0)
+ assert_equal db[1], 0
+ assert db.include? '1'
+ db[1] = 1
+ assert_equal db[1], 1
+ db.default = 42
+ assert_equal db['x'], 42
+ db.close
end
it 'should handle default values that are procs' do
- db = Daybreak::DB.new(DB_PATH) {|key| Set.new }
+ db = Daybreak::DB.new(DB_PATH) {|key| set = Set.new; set << key }
assert db['foo'].is_a? Set
+ assert db.include? 'foo'
+ assert db['bar'].include? 'bar'
+ db.default = proc {|key| [key] }
+ assert db[1].is_a? Array
+ assert db[2] == ['2']
db.close
end
it 'should be able to sync competing writes' do
@db.set! '1', 4
@@ -190,12 +216,12 @@
1000.times do |i|
db.lock { db[1] += 1 }
db["b#{i}"] = i
sleep 0.01 if i % 100 == 0
end
- db.close
- end
+ db.close
+ end
Process.wait a
Process.wait b
@db = Daybreak::DB.new DB_PATH
1000.times do |i|
assert_equal @db["a#{i}"], i
@@ -308,14 +334,29 @@
@db[1] = 2
@db.flush
end
end
- it 'should support set! in lock' do
+ it 'should support set! and delete! in lock' do
@db[1] = 2
@db.lock do
@db.set!(1, 2)
+ @db.delete!(1)
end
+ end
+
+ it 'should allow for inheritance' do
+ class Subclassed < Daybreak::DB
+ def increment(key, amount = 1)
+ lock { self[key] += amount }
+ end
+ end
+
+ db = Subclassed.new DB_PATH
+ db[1] = 1
+ assert_equal db.increment(1), 2
+ db.clear
+ db.close
end
after do
@db.clear
@db.close