Initial support/tests for SQLite flags and db dump

This commit adds a pass through for specifying SQLite3 flags. This is
an absolute requirement for being able to create in-memory DB
representations.
This commit is contained in:
Ben Jolitz 2016-03-14 19:53:51 -07:00
parent 67ef13caed
commit fa220a1c4c
3 changed files with 89 additions and 1 deletions

View file

@ -8,6 +8,12 @@ ensure
File.delete(DB_FILENAME)
end
private def with_db(name)
yield Database.new name
ensure
File.delete(name)
end
describe Database do
it "opens a database" do
with_db do |db|
@ -15,6 +21,41 @@ describe Database do
end
end
it "opens a database and then backs it up to another db" do
with_db do |db|
db.execute "create table person (name string, age integer)"
db.execute "insert into person values (\"foo\", 10)"
with_db("./test2.db") do |backup_database|
db.dump(backup_database)
backup_rows = backup_database.execute "select * from person"
source_rows = db.execute "select * from person"
backup_rows.should eq(source_rows)
end
end
end
it "opens a database, inserts records, dumps to an in-memory db, insers some more, then dumps to the source" do
with_db do |db|
db.execute "create table person (name string, age integer)"
db.execute "insert into person values (\"foo\", 10)"
in_memory_db = Database.new("file:memdb1?mode=memory&cache=shared",
LibSQLite3::Flag::URI | LibSQLite3::Flag::CREATE | LibSQLite3::Flag::READWRITE |
LibSQLite3::Flag::FULLMUTEX)
source_rows = db.execute "select * from person"
db.dump(in_memory_db)
in_memory_db_rows = in_memory_db.execute "select * from person"
in_memory_db_rows.should eq(source_rows)
in_memory_db.execute "insert into person values (\"bar\", 22)"
in_memory_db.dump(db)
in_memory_db_rows = in_memory_db.execute "select * from person"
source_rows = db.execute "select * from person"
in_memory_db_rows.should eq(source_rows)
end
end
[nil, 1, 1_i64, "hello", 1.5, 1.5_f32].each do |value|
it "executes and select #{value}" do
with_db(&.execute("select #{value ? value.inspect : "null"}")).should eq([[value]])