mirror of
https://gitea.invidious.io/iv-org/shard-crystal-sqlite3.git
synced 2024-08-15 00:53:26 +00:00
migrate to crystal std db. keeping old code side by side
This commit is contained in:
parent
67ef13caed
commit
efa010e2ad
7 changed files with 188 additions and 13 deletions
|
@ -1,8 +1,6 @@
|
|||
require "./spec_helper"
|
||||
|
||||
DB_FILENAME = "./test.db"
|
||||
|
||||
private def with_db
|
||||
private def with_db_old
|
||||
yield Database.new DB_FILENAME
|
||||
ensure
|
||||
File.delete(DB_FILENAME)
|
||||
|
@ -10,43 +8,43 @@ end
|
|||
|
||||
describe Database do
|
||||
it "opens a database" do
|
||||
with_db do |db|
|
||||
with_db_old do |db|
|
||||
File.exists?(DB_FILENAME).should be_true
|
||||
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]])
|
||||
with_db_old(&.execute("select #{value ? value.inspect : "null"}")).should eq([[value]])
|
||||
end
|
||||
|
||||
it "executes with bind #{value}" do
|
||||
with_db(&.execute(%(select ?), value)).should eq([[value]])
|
||||
with_db_old(&.execute(%(select ?), value)).should eq([[value]])
|
||||
end
|
||||
|
||||
it "executes with bind #{value} as array" do
|
||||
with_db(&.execute(%(select ?), [value])).should eq([[value]])
|
||||
with_db_old(&.execute(%(select ?), [value])).should eq([[value]])
|
||||
end
|
||||
end
|
||||
|
||||
it "executes and selects blob" do
|
||||
rows = with_db(&.execute(%(select X'53514C697465')))
|
||||
rows = with_db_old(&.execute(%(select X'53514C697465')))
|
||||
row = rows[0]
|
||||
cell = row[0] as Slice(UInt8)
|
||||
cell.to_a.should eq([0x53, 0x51, 0x4C, 0x69, 0x74, 0x65])
|
||||
end
|
||||
|
||||
it "executes with named bind using symbol" do
|
||||
with_db(&.execute(%(select :value), {value: "hello"})).should eq([["hello"]])
|
||||
with_db_old(&.execute(%(select :value), {value: "hello"})).should eq([["hello"]])
|
||||
end
|
||||
|
||||
it "executes with named bind using string" do
|
||||
with_db(&.execute(%(select :value), {"value": "hello"})).should eq([["hello"]])
|
||||
with_db_old(&.execute(%(select :value), {"value": "hello"})).should eq([["hello"]])
|
||||
end
|
||||
|
||||
it "executes with bind blob" do
|
||||
ary = UInt8[0x53, 0x51, 0x4C, 0x69, 0x74, 0x65]
|
||||
rows = with_db(&.execute(%(select cast(? as BLOB)), Slice.new(ary.to_unsafe, ary.size)))
|
||||
rows = with_db_old(&.execute(%(select cast(? as BLOB)), Slice.new(ary.to_unsafe, ary.size)))
|
||||
row = rows[0]
|
||||
cell = row[0] as Slice(UInt8)
|
||||
cell.to_a.should eq(ary)
|
||||
|
@ -102,11 +100,11 @@ describe Database do
|
|||
end
|
||||
|
||||
it "gets first row" do
|
||||
with_db(&.get_first_row(%(select 1))).should eq([1])
|
||||
with_db_old(&.get_first_row(%(select 1))).should eq([1])
|
||||
end
|
||||
|
||||
it "gets first value" do
|
||||
with_db(&.get_first_value(%(select 1))).should eq(1)
|
||||
with_db_old(&.get_first_value(%(select 1))).should eq(1)
|
||||
end
|
||||
|
||||
it "ensures statements are closed" do
|
||||
|
|
65
spec/driver_spec.cr
Normal file
65
spec/driver_spec.cr
Normal file
|
@ -0,0 +1,65 @@
|
|||
require "./spec_helper"
|
||||
|
||||
def with_db
|
||||
yield DB.open "sqlite3", {"database": DB_FILENAME}
|
||||
ensure
|
||||
File.delete(DB_FILENAME)
|
||||
end
|
||||
|
||||
def sql(s : String)
|
||||
"#{s.inspect}"
|
||||
end
|
||||
|
||||
def sql(s)
|
||||
"#{s}"
|
||||
end
|
||||
|
||||
describe Driver do
|
||||
it "should register sqlite3 name" do
|
||||
DB.driver_class("sqlite3").should eq(SQLite3::Driver)
|
||||
end
|
||||
|
||||
it "should use database option as file to open" do
|
||||
with_db do |db|
|
||||
db.driver_class.should eq(SQLite3::Driver)
|
||||
File.exists?(DB_FILENAME).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
||||
it "executes and select {{value.id}}" do
|
||||
with_db do |db|
|
||||
result_set = db.exec("select #{sql({{value}})}")
|
||||
result_set.move_next.should be_true
|
||||
result_set.read(typeof({{value}})).should eq({{value}})
|
||||
result_set.move_next.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "executes and select {{value.id}} as nillable" do
|
||||
with_db do |db|
|
||||
result_set = db.exec("select #{sql({{value}})}")
|
||||
result_set.move_next.should be_true
|
||||
result_set.read?(typeof({{value}})).should eq({{value}})
|
||||
result_set.move_next.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "executes and select nil as type of {{value.id}}" do
|
||||
with_db do |db|
|
||||
result_set = db.exec("select null")
|
||||
result_set.move_next.should be_true
|
||||
result_set.read?(typeof({{value}})).should be_nil
|
||||
result_set.move_next.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
# it "executes with bind #{value}" do
|
||||
# with_db(&.execute(%(select ?), value)).should eq([[value]])
|
||||
# end
|
||||
|
||||
# it "executes with bind #{value} as array" do
|
||||
# with_db(&.execute(%(select ?), [value])).should eq([[value]])
|
||||
# end
|
||||
{% end %}
|
||||
end
|
|
@ -2,3 +2,5 @@ require "spec"
|
|||
require "../src/sqlite3"
|
||||
|
||||
include SQLite3
|
||||
|
||||
DB_FILENAME = "./test.db"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue