diff --git a/test/db/client.rb b/test/db/client.rb index 323f8ea..88662dc 100644 --- a/test/db/client.rb +++ b/test/db/client.rb @@ -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 diff --git a/test/db/datatype/datetime.rb b/test/db/datatype/datetime.rb index 37f1a2d..b7ed871 100644 --- a/test/db/datatype/datetime.rb +++ b/test/db/datatype/datetime.rb @@ -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