Skip to content

Commit

Permalink
Add dbstat test
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Nov 19, 2024
1 parent 6020fc2 commit 3fae0d3
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_changes
assert_equal 1, @db.changes
@db.execute_batch(
"UPDATE items SET number = (number + :nn) WHERE (number = :n)",
{"nn" => 20, "n" => 10}
{ "nn" => 20, "n" => 10 }
)
assert_equal 1, @db.changes
assert_equal [[30]], @db.execute("select number from items")
Expand Down Expand Up @@ -168,38 +168,38 @@ def test_execute_batch2
INSERT INTO items (name) VALUES ("bar");
SELECT * FROM items;
EOSQL
assert_equal [{"id" => "1", "name" => "foo"}, {"id" => "2", "name" => "bar"}], return_value
assert_equal [{ "id" => "1", "name" => "foo" }, { "id" => "2", "name" => "bar" }], return_value

return_value = @db.execute_batch2("SELECT * FROM items;") do |result|
result["id"] = result["id"].to_i
result
end
assert_equal [{"id" => 1, "name" => "foo"}, {"id" => 2, "name" => "bar"}], return_value
assert_equal [{ "id" => 1, "name" => "foo" }, { "id" => 2, "name" => "bar" }], return_value

return_value = @db.execute_batch2('INSERT INTO items (name) VALUES ("oof")')
assert_empty return_value

return_value = @db.execute_batch2(
'CREATE TABLE employees (id integer PRIMARY KEY AUTOINCREMENT, name string, age integer(3));
"CREATE TABLE employees (id integer PRIMARY KEY AUTOINCREMENT, name string, age integer(3));
INSERT INTO employees (age) VALUES (30);
INSERT INTO employees (age) VALUES (40);
INSERT INTO employees (age) VALUES (20);
SELECT age FROM employees;'
SELECT age FROM employees;"
) do |result|
result["age"] = result["age"].to_i
result
end
assert_equal [{"age" => 30}, {"age" => 40}, {"age" => 20}], return_value
assert_equal [{ "age" => 30 }, { "age" => 40 }, { "age" => 20 }], return_value

return_value = @db.execute_batch2("SELECT name FROM employees")
assert_equal [{"name" => nil}, {"name" => nil}, {"name" => nil}], return_value
assert_equal [{ "name" => nil }, { "name" => nil }, { "name" => nil }], return_value

@db.results_as_hash = false
return_value = @db.execute_batch2(
'CREATE TABLE managers (id integer PRIMARY KEY AUTOINCREMENT, age integer(3));
"CREATE TABLE managers (id integer PRIMARY KEY AUTOINCREMENT, age integer(3));
INSERT INTO managers (age) VALUES (50);
INSERT INTO managers (age) VALUES (60);
SELECT id, age from managers;'
SELECT id, age from managers;"
) do |result|
result = result.map do |res|
res.to_i
Expand Down Expand Up @@ -356,15 +356,15 @@ def test_execute_returns_list_of_hash
db.execute("create table foo ( a integer primary key, b text )")
db.execute("insert into foo (b) values ('hello')")
rows = db.execute("select * from foo")
assert_equal [{"a" => 1, "b" => "hello"}], rows
assert_equal [{ "a" => 1, "b" => "hello" }], rows
end

def test_execute_yields_hash
db = SQLite3::Database.new(":memory:", results_as_hash: true)
db.execute("create table foo ( a integer primary key, b text )")
db.execute("insert into foo (b) values ('hello')")
db.execute("select * from foo") do |row|
assert_equal({"a" => 1, "b" => "hello"}, row)
assert_equal({ "a" => 1, "b" => "hello" }, row)
end
end

Expand All @@ -377,16 +377,16 @@ def test_table_info
"notnull" => 0,
"type" => "integer",
"dflt_value" => nil,
"cid" => 0
"cid" => 0,
},
{
"name" => "b",
"pk" => 0,
"notnull" => 0,
"type" => "text",
"dflt_value" => nil,
"cid" => 1
}]
{
"name" => "b",
"pk" => 0,
"notnull" => 0,
"type" => "text",
"dflt_value" => nil,
"cid" => 1,
}]
assert_equal info, db.table_info("foo")
end

Expand Down Expand Up @@ -415,7 +415,8 @@ def test_trace_with_block
def test_trace_with_object
obj = Class.new {
attr_accessor :result
def call sql

def call(sql)
@result = sql
end
}.new
Expand Down Expand Up @@ -516,7 +517,7 @@ def test_function_return_type_round_trip
def test_define_function_closed
@db.close
assert_raise(SQLite3::Exception) do
@db.define_function("foo") {}
@db.define_function("foo") { }
end
end

Expand All @@ -536,11 +537,12 @@ def test_define_aggregate
acc = Class.new {
attr_reader :sum
alias_method :finalize, :sum

def initialize
@sum = 0
end

def step a
def step(a)
@sum += a
end
}.new
Expand All @@ -554,14 +556,14 @@ def test_authorizer_ok
statements = []

@db.authorizer = Class.new {
def call action, a, b, c, d
def call(action, a, b, c, d)
true
end
}.new
statements << @db.prepare("select 'fooooo'")

@db.authorizer = Class.new {
def call action, a, b, c, d
def call(action, a, b, c, d)
0
end
}.new
Expand All @@ -572,7 +574,7 @@ def call action, a, b, c, d

def test_authorizer_ignore
@db.authorizer = Class.new {
def call action, a, b, c, d
def call(action, a, b, c, d)
nil
end
}.new
Expand All @@ -584,7 +586,7 @@ def call action, a, b, c, d

def test_authorizer_fail
@db.authorizer = Class.new {
def call action, a, b, c, d
def call(action, a, b, c, d)
false
end
}.new
Expand All @@ -595,7 +597,7 @@ def call action, a, b, c, d

def test_remove_auth
@db.authorizer = Class.new {
def call action, a, b, c, d
def call(action, a, b, c, d)
false
end
}.new
Expand Down Expand Up @@ -623,14 +625,14 @@ def test_execute_with_empty_bind_params
end

def test_query_with_named_bind_params
resultset = @db.query("select :n", {"n" => "foo"})
resultset = @db.query("select :n", { "n" => "foo" })
assert_equal [["foo"]], resultset.to_a
ensure
resultset&.close
end

def test_execute_with_named_bind_params
assert_equal [["foo"]], @db.execute("select :n", {"n" => "foo"})
assert_equal [["foo"]], @db.execute("select :n", { "n" => "foo" })
end

def test_strict_mode
Expand Down Expand Up @@ -681,10 +683,10 @@ def test_default_transaction_mode
end

test_cases = [
{mode: nil, read: true, write: true},
{mode: :deferred, read: true, write: true},
{mode: :immediate, read: true, write: false},
{mode: :exclusive, read: false, write: false}
{ mode: nil, read: true, write: true },
{ mode: :deferred, read: true, write: true },
{ mode: :immediate, read: true, write: false },
{ mode: :exclusive, read: false, write: false },
]

test_cases.each do |item|
Expand Down Expand Up @@ -721,5 +723,9 @@ def test_transaction_returns_block_result
result = @db.transaction { :foo }
assert_equal :foo, result
end

def test_dbstat_table_exists
assert_nothing_raised { @db.execute("select * from dbstat") }
end
end
end

0 comments on commit 3fae0d3

Please sign in to comment.