Behavior of cached database is now covered by a few tests.
parent
c475c4f584
commit
5ce24184be
108
spec/cached.cr
108
spec/cached.cr
|
@ -2,35 +2,25 @@ require "spec"
|
||||||
require "file_utils"
|
require "file_utils"
|
||||||
|
|
||||||
require "../src/dodb.cr"
|
require "../src/dodb.cr"
|
||||||
|
require "./spec-database.cr"
|
||||||
require "./test-data.cr"
|
require "./test-data.cr"
|
||||||
|
|
||||||
|
describe "DODB::CachedDataBase" do
|
||||||
class DODB::SpecDataBase < DODB::CachedDataBase(Ship)
|
|
||||||
def initialize(storage_ext = "", remove_previous_data = true)
|
|
||||||
storage_dir = "test-storage#{storage_ext}"
|
|
||||||
|
|
||||||
if remove_previous_data
|
|
||||||
::FileUtils.rm_rf storage_dir
|
|
||||||
end
|
|
||||||
|
|
||||||
super storage_dir
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "DODB::DataBase::Cached" do
|
|
||||||
describe "basics" do
|
describe "basics" do
|
||||||
it "store and get data" do
|
it "store and get data" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
Ship.all_ships.each do |ship|
|
Ship.all_ships.each do |ship|
|
||||||
db << ship
|
db << ship
|
||||||
end
|
end
|
||||||
|
|
||||||
db.to_a.sort.should eq(Ship.all_ships.sort)
|
db.to_a.sort.should eq(Ship.all_ships.sort)
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "rewrite already stored data" do
|
it "rewrite already stored data" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
ship = Ship.all_ships[0]
|
ship = Ship.all_ships[0]
|
||||||
|
|
||||||
key = db << ship
|
key = db << ship
|
||||||
|
@ -39,10 +29,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
db[key] = ship
|
db[key] = ship
|
||||||
|
|
||||||
db[key].should eq(ship)
|
db[key].should eq(ship)
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "properly remove data" do
|
it "properly remove data" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
Ship.all_ships.each do |ship|
|
Ship.all_ships.each do |ship|
|
||||||
db << ship
|
db << ship
|
||||||
|
@ -60,23 +52,28 @@ describe "DODB::DataBase::Cached" do
|
||||||
|
|
||||||
db[i]?.should be_nil
|
db[i]?.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "preserves data on reopening" do
|
it "preserves data on reopening" do
|
||||||
db1 = DODB::SpecDataBase.new
|
db1 = DODB::CachedSpecDataBase(Ship).new
|
||||||
db1 << Ship.kisaragi
|
db1 << Ship.kisaragi
|
||||||
|
|
||||||
db1.to_a.size.should eq(1)
|
db1.to_a.size.should eq(1)
|
||||||
|
|
||||||
db2 = DODB::SpecDataBase.new remove_previous_data: false
|
db2 = DODB::CachedSpecDataBase(Ship).new remove_previous_data: false
|
||||||
db2 << Ship.mutsuki
|
db2 << Ship.mutsuki
|
||||||
|
|
||||||
# Only difference with DODB::DataBase: for now, concurrent DB cannot coexists.
|
# Only difference with DODB::DataBase: concurrent DB cannot coexists.
|
||||||
db2.to_a.size.should eq(2)
|
db2.to_a.size.should eq(2)
|
||||||
|
|
||||||
|
db1.rm_storage_dir
|
||||||
|
db2.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "iterates in normal and reversed order" do
|
it "iterates in normal and reversed order" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
Ship.all_ships.each do |ship|
|
Ship.all_ships.each do |ship|
|
||||||
db << ship
|
db << ship
|
||||||
|
@ -93,10 +90,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
|
|
||||||
# Actual reversal is tested here.
|
# Actual reversal is tested here.
|
||||||
db.to_a(reversed: true).should eq db.to_a.reverse
|
db.to_a(reversed: true).should eq db.to_a.reverse
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "respects the provided offsets if any" do
|
it "respects the provided offsets if any" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
Ship.all_ships.each do |ship|
|
Ship.all_ships.each do |ship|
|
||||||
db << ship
|
db << ship
|
||||||
|
@ -109,12 +108,14 @@ describe "DODB::DataBase::Cached" do
|
||||||
db.to_a(start_offset: 0, end_offset: 2).should eq [
|
db.to_a(start_offset: 0, end_offset: 2).should eq [
|
||||||
Ship.mutsuki, Ship.kisaragi, Ship.yayoi
|
Ship.mutsuki, Ship.kisaragi, Ship.yayoi
|
||||||
]
|
]
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "indices" do
|
describe "indices" do
|
||||||
it "do basic indexing" do
|
it "do basic indexing" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
|
|
||||||
|
@ -125,10 +126,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
Ship.all_ships.each_with_index do |ship|
|
Ship.all_ships.each_with_index do |ship|
|
||||||
db_ships_by_name.get?(ship.name).should eq(ship)
|
db_ships_by_name.get?(ship.name).should eq(ship)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raise on index overload" do
|
it "raise on index overload" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
|
|
||||||
|
@ -139,10 +142,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
expect_raises(DODB::IndexOverload) do
|
expect_raises(DODB::IndexOverload) do
|
||||||
db << Ship.kisaragi
|
db << Ship.kisaragi
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "properly deindex" do
|
it "properly deindex" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
|
|
||||||
|
@ -157,10 +162,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
Ship.all_ships.each do |ship|
|
Ship.all_ships.each do |ship|
|
||||||
db_ships_by_name.get?(ship.name).should be_nil
|
db_ships_by_name.get?(ship.name).should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "properly reindex" do
|
it "properly reindex" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
|
|
||||||
|
@ -175,10 +182,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
db[key].should eq(some_new_ship)
|
db[key].should eq(some_new_ship)
|
||||||
|
|
||||||
db_ships_by_name.get?(some_new_ship.name).should eq(some_new_ship)
|
db_ships_by_name.get?(some_new_ship.name).should eq(some_new_ship)
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "properly updates" do
|
it "properly updates" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
|
|
||||||
|
@ -195,12 +204,14 @@ describe "DODB::DataBase::Cached" do
|
||||||
|
|
||||||
db_ships_by_name.get?("Kisaragi").should be_nil
|
db_ships_by_name.get?("Kisaragi").should be_nil
|
||||||
db_ships_by_name.get?(new_kisaragi.name).should eq new_kisaragi
|
db_ships_by_name.get?(new_kisaragi.name).should eq new_kisaragi
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "partitions" do
|
describe "partitions" do
|
||||||
it "do basic partitioning" do
|
it "do basic partitioning" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_class = db.new_partition "class", &.klass
|
db_ships_by_class = db.new_partition "class", &.klass
|
||||||
|
|
||||||
|
@ -224,11 +235,13 @@ describe "DODB::DataBase::Cached" do
|
||||||
}.should be_true
|
}.should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
db_ships_by_class.get("does-not-exist").should eq [] of Ship
|
db_ships_by_class.get?("does-not-exist").should be_nil
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "removes select elements from partitions" do
|
it "removes select elements from partitions" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_class = db.new_partition "class", &.klass
|
db_ships_by_class = db.new_partition "class", &.klass
|
||||||
|
|
||||||
|
@ -243,12 +256,14 @@ describe "DODB::DataBase::Cached" do
|
||||||
|
|
||||||
partition.any?(&.name.==("Kisaragi")).should be_false
|
partition.any?(&.name.==("Kisaragi")).should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "tags" do
|
describe "tags" do
|
||||||
it "do basic tagging" do
|
it "do basic tagging" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_tags = db.new_tags "tags", &.tags
|
db_ships_by_tags = db.new_tags "tags", &.tags
|
||||||
|
|
||||||
|
@ -265,11 +280,13 @@ describe "DODB::DataBase::Cached" do
|
||||||
.should be_true
|
.should be_true
|
||||||
|
|
||||||
# There shouldn’t be one in our data about WWII Japanese warships…
|
# There shouldn’t be one in our data about WWII Japanese warships…
|
||||||
db_ships_by_tags.get("starship").should eq([] of Ship)
|
db_ships_by_tags.get?("starship").should be_nil
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "properly removes tags" do
|
it "properly removes tags" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_tags = db.new_tags "tags", &.tags
|
db_ships_by_tags = db.new_tags "tags", &.tags
|
||||||
|
|
||||||
|
@ -278,7 +295,8 @@ describe "DODB::DataBase::Cached" do
|
||||||
end
|
end
|
||||||
|
|
||||||
# Removing the “flagship” tag, brace for impact.
|
# Removing the “flagship” tag, brace for impact.
|
||||||
flagship, index = db_ships_by_tags.get_with_indices("flagship")[0]
|
flagship, index = db_ships_by_tags.get_with_indice("flagship")[0]
|
||||||
|
flagship = flagship.clone
|
||||||
flagship.tags = [] of String
|
flagship.tags = [] of String
|
||||||
db[index] = flagship
|
db[index] = flagship
|
||||||
|
|
||||||
|
@ -290,10 +308,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
# end
|
# end
|
||||||
|
|
||||||
db_ships_by_tags.get("flagship").should eq([] of Ship)
|
db_ships_by_tags.get("flagship").should eq([] of Ship)
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "gets items that have multiple tags" do
|
it "gets items that have multiple tags" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_tags = db.new_tags "tags", &.tags
|
db_ships_by_tags = db.new_tags "tags", &.tags
|
||||||
|
|
||||||
|
@ -309,12 +329,14 @@ describe "DODB::DataBase::Cached" do
|
||||||
|
|
||||||
results = db_ships_by_tags.get(["flagship"])
|
results = db_ships_by_tags.get(["flagship"])
|
||||||
results.should eq([Ship.yamato])
|
results.should eq([Ship.yamato])
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "atomic operations" do
|
describe "atomic operations" do
|
||||||
it "safe_get and safe_get?" do
|
it "safe_get and safe_get?" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
|
|
||||||
|
@ -331,12 +353,14 @@ describe "DODB::DataBase::Cached" do
|
||||||
results.should eq(ship)
|
results.should eq(ship)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "tools" do
|
describe "tools" do
|
||||||
it "rebuilds indexes" do
|
it "rebuilds indexes" do
|
||||||
db = DODB::SpecDataBase.new
|
db = DODB::CachedSpecDataBase(Ship).new
|
||||||
|
|
||||||
db_ships_by_name = db.new_index "name", &.name
|
db_ships_by_name = db.new_index "name", &.name
|
||||||
db_ships_by_class = db.new_partition "class", &.klass
|
db_ships_by_class = db.new_partition "class", &.klass
|
||||||
|
@ -352,11 +376,12 @@ describe "DODB::DataBase::Cached" do
|
||||||
db_ships_by_name.get?(ship.name).should eq(ship)
|
db_ships_by_name.get?(ship.name).should eq(ship)
|
||||||
db_ships_by_class.get(ship.klass).should contain(ship)
|
db_ships_by_class.get(ship.klass).should contain(ship)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
db.rm_storage_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
it "migrates properly" do
|
it "migrates properly" do
|
||||||
::FileUtils.rm_rf "test-storage-migration-origin"
|
old_db = DODB::CachedSpecDataBase(PrimitiveShip).new "-migration-origin"
|
||||||
old_db = DODB::DataBase(PrimitiveShip).new "test-storage-migration-origin"
|
|
||||||
|
|
||||||
old_ships_by_name = old_db.new_index "name", &.name
|
old_ships_by_name = old_db.new_index "name", &.name
|
||||||
old_ships_by_class = old_db.new_partition "class", &.class_name
|
old_ships_by_class = old_db.new_partition "class", &.class_name
|
||||||
|
@ -368,7 +393,7 @@ describe "DODB::DataBase::Cached" do
|
||||||
# At this point, the “old” DB is filled. Now we need to convert
|
# At this point, the “old” DB is filled. Now we need to convert
|
||||||
# to the new DB.
|
# to the new DB.
|
||||||
|
|
||||||
new_db = DODB::SpecDataBase.new "-migration-target"
|
new_db = DODB::CachedSpecDataBase(Ship).new "-migration-target"
|
||||||
|
|
||||||
new_ships_by_name = new_db.new_index "name", &.name
|
new_ships_by_name = new_db.new_index "name", &.name
|
||||||
new_ships_by_class = new_db.new_partition "class", &.klass
|
new_ships_by_class = new_db.new_partition "class", &.klass
|
||||||
|
@ -396,6 +421,9 @@ describe "DODB::DataBase::Cached" do
|
||||||
|
|
||||||
ship.tags.any?(&.==("name ship")).should be_true if ship.name == ship.klass
|
ship.tags.any?(&.==("name ship")).should be_true if ship.name == ship.klass
|
||||||
end
|
end
|
||||||
|
|
||||||
|
old_db.rm_storage_dir
|
||||||
|
new_db.rm_storage_dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -215,7 +215,7 @@ describe "DODB::DataBase" do
|
||||||
}.should be_true
|
}.should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
db_ships_by_class.get?("does-not-exist").should eq nil
|
db_ships_by_class.get?("does-not-exist").should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "removes select elements from partitions" do
|
it "removes select elements from partitions" do
|
||||||
|
@ -256,7 +256,7 @@ describe "DODB::DataBase" do
|
||||||
.should be_true
|
.should be_true
|
||||||
|
|
||||||
# There shouldn’t be one in our data about WWII Japanese warships…
|
# There shouldn’t be one in our data about WWII Japanese warships…
|
||||||
db_ships_by_tags.get?("starship").should eq nil
|
db_ships_by_tags.get?("starship").should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "properly removes tags" do
|
it "properly removes tags" do
|
||||||
|
|
Loading…
Reference in New Issue