Skip to content

Commit 3fae0d3

Browse files
committed
Add dbstat test
1 parent 6020fc2 commit 3fae0d3

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

test/test_database.rb

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_changes
138138
assert_equal 1, @db.changes
139139
@db.execute_batch(
140140
"UPDATE items SET number = (number + :nn) WHERE (number = :n)",
141-
{"nn" => 20, "n" => 10}
141+
{ "nn" => 20, "n" => 10 }
142142
)
143143
assert_equal 1, @db.changes
144144
assert_equal [[30]], @db.execute("select number from items")
@@ -168,38 +168,38 @@ def test_execute_batch2
168168
INSERT INTO items (name) VALUES ("bar");
169169
SELECT * FROM items;
170170
EOSQL
171-
assert_equal [{"id" => "1", "name" => "foo"}, {"id" => "2", "name" => "bar"}], return_value
171+
assert_equal [{ "id" => "1", "name" => "foo" }, { "id" => "2", "name" => "bar" }], return_value
172172

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

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

182182
return_value = @db.execute_batch2(
183-
'CREATE TABLE employees (id integer PRIMARY KEY AUTOINCREMENT, name string, age integer(3));
183+
"CREATE TABLE employees (id integer PRIMARY KEY AUTOINCREMENT, name string, age integer(3));
184184
INSERT INTO employees (age) VALUES (30);
185185
INSERT INTO employees (age) VALUES (40);
186186
INSERT INTO employees (age) VALUES (20);
187-
SELECT age FROM employees;'
187+
SELECT age FROM employees;"
188188
) do |result|
189189
result["age"] = result["age"].to_i
190190
result
191191
end
192-
assert_equal [{"age" => 30}, {"age" => 40}, {"age" => 20}], return_value
192+
assert_equal [{ "age" => 30 }, { "age" => 40 }, { "age" => 20 }], return_value
193193

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

197197
@db.results_as_hash = false
198198
return_value = @db.execute_batch2(
199-
'CREATE TABLE managers (id integer PRIMARY KEY AUTOINCREMENT, age integer(3));
199+
"CREATE TABLE managers (id integer PRIMARY KEY AUTOINCREMENT, age integer(3));
200200
INSERT INTO managers (age) VALUES (50);
201201
INSERT INTO managers (age) VALUES (60);
202-
SELECT id, age from managers;'
202+
SELECT id, age from managers;"
203203
) do |result|
204204
result = result.map do |res|
205205
res.to_i
@@ -356,15 +356,15 @@ def test_execute_returns_list_of_hash
356356
db.execute("create table foo ( a integer primary key, b text )")
357357
db.execute("insert into foo (b) values ('hello')")
358358
rows = db.execute("select * from foo")
359-
assert_equal [{"a" => 1, "b" => "hello"}], rows
359+
assert_equal [{ "a" => 1, "b" => "hello" }], rows
360360
end
361361

362362
def test_execute_yields_hash
363363
db = SQLite3::Database.new(":memory:", results_as_hash: true)
364364
db.execute("create table foo ( a integer primary key, b text )")
365365
db.execute("insert into foo (b) values ('hello')")
366366
db.execute("select * from foo") do |row|
367-
assert_equal({"a" => 1, "b" => "hello"}, row)
367+
assert_equal({ "a" => 1, "b" => "hello" }, row)
368368
end
369369
end
370370

@@ -377,16 +377,16 @@ def test_table_info
377377
"notnull" => 0,
378378
"type" => "integer",
379379
"dflt_value" => nil,
380-
"cid" => 0
380+
"cid" => 0,
381381
},
382-
{
383-
"name" => "b",
384-
"pk" => 0,
385-
"notnull" => 0,
386-
"type" => "text",
387-
"dflt_value" => nil,
388-
"cid" => 1
389-
}]
382+
{
383+
"name" => "b",
384+
"pk" => 0,
385+
"notnull" => 0,
386+
"type" => "text",
387+
"dflt_value" => nil,
388+
"cid" => 1,
389+
}]
390390
assert_equal info, db.table_info("foo")
391391
end
392392

@@ -415,7 +415,8 @@ def test_trace_with_block
415415
def test_trace_with_object
416416
obj = Class.new {
417417
attr_accessor :result
418-
def call sql
418+
419+
def call(sql)
419420
@result = sql
420421
end
421422
}.new
@@ -516,7 +517,7 @@ def test_function_return_type_round_trip
516517
def test_define_function_closed
517518
@db.close
518519
assert_raise(SQLite3::Exception) do
519-
@db.define_function("foo") {}
520+
@db.define_function("foo") { }
520521
end
521522
end
522523

@@ -536,11 +537,12 @@ def test_define_aggregate
536537
acc = Class.new {
537538
attr_reader :sum
538539
alias_method :finalize, :sum
540+
539541
def initialize
540542
@sum = 0
541543
end
542544

543-
def step a
545+
def step(a)
544546
@sum += a
545547
end
546548
}.new
@@ -554,14 +556,14 @@ def test_authorizer_ok
554556
statements = []
555557

556558
@db.authorizer = Class.new {
557-
def call action, a, b, c, d
559+
def call(action, a, b, c, d)
558560
true
559561
end
560562
}.new
561563
statements << @db.prepare("select 'fooooo'")
562564

563565
@db.authorizer = Class.new {
564-
def call action, a, b, c, d
566+
def call(action, a, b, c, d)
565567
0
566568
end
567569
}.new
@@ -572,7 +574,7 @@ def call action, a, b, c, d
572574

573575
def test_authorizer_ignore
574576
@db.authorizer = Class.new {
575-
def call action, a, b, c, d
577+
def call(action, a, b, c, d)
576578
nil
577579
end
578580
}.new
@@ -584,7 +586,7 @@ def call action, a, b, c, d
584586

585587
def test_authorizer_fail
586588
@db.authorizer = Class.new {
587-
def call action, a, b, c, d
589+
def call(action, a, b, c, d)
588590
false
589591
end
590592
}.new
@@ -595,7 +597,7 @@ def call action, a, b, c, d
595597

596598
def test_remove_auth
597599
@db.authorizer = Class.new {
598-
def call action, a, b, c, d
600+
def call(action, a, b, c, d)
599601
false
600602
end
601603
}.new
@@ -623,14 +625,14 @@ def test_execute_with_empty_bind_params
623625
end
624626

625627
def test_query_with_named_bind_params
626-
resultset = @db.query("select :n", {"n" => "foo"})
628+
resultset = @db.query("select :n", { "n" => "foo" })
627629
assert_equal [["foo"]], resultset.to_a
628630
ensure
629631
resultset&.close
630632
end
631633

632634
def test_execute_with_named_bind_params
633-
assert_equal [["foo"]], @db.execute("select :n", {"n" => "foo"})
635+
assert_equal [["foo"]], @db.execute("select :n", { "n" => "foo" })
634636
end
635637

636638
def test_strict_mode
@@ -681,10 +683,10 @@ def test_default_transaction_mode
681683
end
682684

683685
test_cases = [
684-
{mode: nil, read: true, write: true},
685-
{mode: :deferred, read: true, write: true},
686-
{mode: :immediate, read: true, write: false},
687-
{mode: :exclusive, read: false, write: false}
686+
{ mode: nil, read: true, write: true },
687+
{ mode: :deferred, read: true, write: true },
688+
{ mode: :immediate, read: true, write: false },
689+
{ mode: :exclusive, read: false, write: false },
688690
]
689691

690692
test_cases.each do |item|
@@ -721,5 +723,9 @@ def test_transaction_returns_block_result
721723
result = @db.transaction { :foo }
722724
assert_equal :foo, result
723725
end
726+
727+
def test_dbstat_table_exists
728+
assert_nothing_raised { @db.execute("select * from dbstat") }
729+
end
724730
end
725731
end

0 commit comments

Comments
 (0)