Skip to content

Commit

Permalink
Improved test structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 28, 2024
1 parent 9b7a063 commit 04fcc30
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 133 deletions.
208 changes: 104 additions & 104 deletions test/db/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,118 @@

require 'db/client_context'

AClient = Sus::Shared("a client") do |adapter|
include_context DB::ClientContext, adapter

it "can select version" do
context = client.context

result = context.call("SELECT VERSION()")
expect(result).to be_a(DB::Records)

row = result.rows.first
expect(row[0]).to be_a(String)
end

it "can execute multiple queries" do
context = client.context

query = <<~SQL * 2
SELECT 42 AS LIFE;
SQL

context.call(query) do |connection|
2.times do
result = connection.next_result
expect(result.to_a).to be == [[42]]
end
end
ensure
context.close
end

it "can generate a query with literal values" do
session = client.session

session.clause("SELECT").literal(42).clause("AS").identifier(:LIFE).call do |connection|
result = connection.next_result
expect(result.to_a).to be == [[42]]
end
ensure
session.close
end

it "can generate a query using interpolations" do
session = client.session

session.query("SELECT %{value} AS %{column}", value: 42, column: :LIFE).call do |connection|
result = connection.next_result
expect(result.to_a).to be == [[42]]
end
ensure
session.close
end

it "can execute a query in a transaction" do
transaction = client.transaction

transaction.call("SELECT 42 AS LIFE") do |connection|
result = connection.next_result
expect(result.to_a).to be == [[42]]
end

transaction.commit
ensure
transaction.close
end

with 'events table' do
def before
super

transaction = client.transaction

transaction.call("DROP TABLE IF EXISTS events")
describe DB::Client do
DB::Adapters.each do |name, klass|
describe klass, unique: name do
include_context DB::ClientContext, klass.new(**CREDENTIALS)

transaction.call("CREATE TABLE IF NOT EXISTS events (#{transaction.connection.key_column}, created_at TIMESTAMP NOT NULL, description TEXT NULL)")

transaction.commit
ensure
transaction.close
end

it 'can insert rows with timestamps' do
session = client.session

session.call("INSERT INTO events (created_at, description) VALUES ('2020-05-04 03:02:01', 'Hello World')")
it "can select version" do
context = client.context
result = context.call("SELECT VERSION()")
expect(result).to be_a(DB::Records)
row = result.rows.first
expect(row[0]).to be_a(String)
ensure
context.close
end

rows = session.call('SELECT * FROM events') do |connection|
connection.next_result.to_a
it "can execute multiple queries" do
context = client.context

query = <<~SQL * 2
SELECT 42 AS LIFE;
SQL

context.call(query) do |connection|
2.times do
result = connection.next_result
expect(result.to_a).to be == [[42]]
end
end
ensure
context.close
end

expect(rows).to be == [[1, Time.parse("2020-05-04 03:02:01 UTC"), "Hello World"]]
ensure
session.close
end

it 'can insert null fields' do
session = client.session
it "can generate a query with literal values" do
session = client.session

session.clause("SELECT").literal(42).clause("AS").identifier(:LIFE).call do |connection|
result = connection.next_result
expect(result.to_a).to be == [[42]]
end
ensure
session.close
end

session.call("INSERT INTO events (created_at, description) VALUES ('2020-05-04 03:02:01', NULL)")
it "can generate a query using interpolations" do
session = client.session

session.query("SELECT %{value} AS %{column}", value: 42, column: :LIFE).call do |connection|
result = connection.next_result
expect(result.to_a).to be == [[42]]
end
ensure
session.close
end

rows = session.call('SELECT * FROM events') do |connection|
connection.next_result.to_a
it "can execute a query in a transaction" do
transaction = client.transaction

transaction.call("SELECT 42 AS LIFE") do |connection|
result = connection.next_result
expect(result.to_a).to be == [[42]]
end

transaction.commit
ensure
transaction.close
end

expect(rows).to be == [[1, Time.parse("2020-05-04 03:02:01 UTC"), nil]]
ensure
session.close
with 'events table' do
def before
super

transaction = client.transaction

transaction.call("DROP TABLE IF EXISTS events")

transaction.call("CREATE TABLE IF NOT EXISTS events (#{transaction.connection.key_column}, created_at TIMESTAMP NOT NULL, description TEXT NULL)")

transaction.commit
ensure
transaction.close
end

it 'can insert rows with timestamps' do
session = client.session

session.call("INSERT INTO events (created_at, description) VALUES ('2020-05-04 03:02:01', 'Hello World')")

rows = session.call('SELECT * FROM events') do |connection|
connection.next_result.to_a
end

expect(rows).to be == [[1, Time.parse("2020-05-04 03:02:01 UTC"), "Hello World"]]
ensure
session.close
end

it 'can insert null fields' do
session = client.session

session.call("INSERT INTO events (created_at, description) VALUES ('2020-05-04 03:02:01', NULL)")

rows = session.call('SELECT * FROM events') do |connection|
connection.next_result.to_a
end

expect(rows).to be == [[1, Time.parse("2020-05-04 03:02:01 UTC"), nil]]
ensure
session.close
end
end
end
end
end

DB::Adapters.each do |name, klass|
describe klass do
it_behaves_like AClient, klass.new(**CREDENTIALS)
end
end
60 changes: 31 additions & 29 deletions test/db/datatype/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@

require 'db/datatype_context'

DatetimeDatatype = Sus::Shared("datetime datatype") do |adapter|
include_context DB::DatatypeContext, adapter, :datetime

it "can insert utc time" do
time = Time.utc(2020, 07, 02, 10, 11, 12)
session = client.session

session.query("INSERT INTO %{table_name} (value) VALUES (%{value})", table_name: table_name, value: time).call

row = session.query("SELECT * FROM %{table_name}", table_name: table_name).call.to_a.first

expect(row.first).to be == time
end

it "can insert local time" do
time = Time.new(2020, 07, 02, 10, 11, 12, "+12:00")
session = client.session

session.query("INSERT INTO %{table_name} (value) VALUES (%{value})", table_name: table_name, value: time).call

row = session.query("SELECT * FROM %{table_name}", table_name: table_name).call.to_a.first

expect(row.first).to be == time
end
end

DB::Adapters.each do |name, klass|
describe klass, unique: klass do
it_behaves_like DatetimeDatatype, klass.new(**CREDENTIALS)
describe "datetime datatype" do
DB::Adapters.each do |name, klass|
describe klass, unique: name do
include_context DB::DatatypeContext, klass.new(**CREDENTIALS), :datetime

it "can insert utc time" do
time = Time.utc(2020, 07, 02, 10, 11, 12)
session = client.session

session.query("INSERT INTO %{table_name} (value) VALUES (%{value})", table_name: table_name, value: time).call

row = session.query("SELECT * FROM %{table_name}", table_name: table_name).call.to_a.first

expect(row.first).to be == time
ensure
session.close
end

it "can insert local time" do
time = Time.new(2020, 07, 02, 10, 11, 12, "+12:00")
session = client.session

session.query("INSERT INTO %{table_name} (value) VALUES (%{value})", table_name: table_name, value: time).call

row = session.query("SELECT * FROM %{table_name}", table_name: table_name).call.to_a.first

expect(row.first).to be == time
ensure
session.close
end
end
end
end

0 comments on commit 04fcc30

Please sign in to comment.