Skip to content

Commit

Permalink
test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ermolaev committed Aug 22, 2024
1 parent 36635d7 commit 35e3eb6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 79 deletions.
2 changes: 2 additions & 0 deletions test/mini_sql/connection_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def test_can_query_single_multi
end

def test_can_deal_with_arrays
return if @connection.respond_to?(:array_encoder) && @connection.array_encoder

r = @connection.query_single("select :array as array", array: [1, 2, 3])
assert_equal([1, 2, 3], r)

Expand Down
6 changes: 3 additions & 3 deletions test/mini_sql/mysql/prepared_connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class MiniSql::Mysql::TestPreparedConnection < Minitest::Test

def setup
@unprepared_connection = mysql_connection
@prepared_connection = @unprepared_connection.prepared
@connection = @unprepared_connection.prepared

super
setup_tables

@unprepared_connection.exec("SET GLOBAL log_output = 'TABLE'")
@unprepared_connection.exec("SET GLOBAL general_log = 'ON'")
Expand All @@ -28,7 +28,7 @@ def assert_last_stmt(statement_sql)
end

def test_boolean_param
r = @prepared_connection.query("SELECT * FROM posts WHERE active = ?", true)
r = @connection.query("SELECT * FROM posts WHERE active = ?", true)

assert_last_stmt "SELECT * FROM posts WHERE active = $1"
assert_equal 2, r[0].id
Expand Down
42 changes: 42 additions & 0 deletions test/mini_sql/postgres/auto_encode_arrays_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "test_helper"
require_relative 'connection_test'
require_relative 'prepared_connection_test'

module MiniSql::Postgres::ArrayTests
def test_simple_params
nums, strings, empty_array = [1, 2, 3], %w[a b c], []

row = @connection.query_single("select ?::int[], ?::text[], ?::int[]", nums, strings, empty_array)

assert_equal(row, [nums, strings, empty_array])
end

def test_hash_params
nums, strings, empty_array = [1, 2, 3], %w[a b c], []

row = @connection.query_single("select :nums::int[], :strings::text[], :empty_array::int[]", nums: nums, strings: strings, empty_array: empty_array)

assert_equal(row, [nums, strings, empty_array])
end
end

class MiniSql::Postgres::TestAutoEncodeArraysPrepared < MiniSql::Postgres::TestPreparedConnection
include MiniSql::Postgres::ArrayTests

def setup
@unprepared_connection = pg_connection(auto_encode_arrays: true)
@connection = @unprepared_connection.prepared

setup_tables
end
end

class MiniSql::Postgres::TestAutoEncodeArraysUnprepared < MiniSql::Postgres::TestConnection
include MiniSql::Postgres::ArrayTests

def setup
@connection = pg_connection(auto_encode_arrays: true)
end
end
22 changes: 0 additions & 22 deletions test/mini_sql/postgres/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,4 @@ def test_unamed_query
assert_equal(row.column2, 3)
end

def test_array_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true)

ints = [1, 2, 3]
strings = %w[a b c]
empty_array = []
row = connection.query_single("select ?::int[], ?::text[], ?::int[]", ints, strings, empty_array)

assert_equal(row, [ints, strings, empty_array])
end

def test_simple_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true)

int = 1
str = "str"
date = Date.new(2020, 10, 10)
row = connection.query_single("select ?, ?, ?::date", int, str, date)

assert_equal(row, [int, str, date])
end

end
58 changes: 12 additions & 46 deletions test/mini_sql/postgres/prepared_connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ class MiniSql::Postgres::TestPreparedConnection < Minitest::Test

def setup
@unprepared_connection = pg_connection
@prepared_connection = @unprepared_connection.prepared
@connection = @unprepared_connection.prepared

super
setup_tables
end

def last_prepared_statement
@unprepared_connection.query("select * from pg_prepared_statements")[
0
]&.statement
@unprepared_connection.query("select * from pg_prepared_statements")[0]&.statement
end

def test_time
r =
@prepared_connection.query(
@connection.query(
"select :date::timestamp - '10 days'::interval AS funday",
date: Time.parse("2010-10-11T02:22:00Z")
)
Expand All @@ -31,7 +29,7 @@ def test_time

def test_date
r =
@prepared_connection.query(
@connection.query(
"select :date::date - 10 AS funday",
date: Date.parse("2010-10-11")
)
Expand All @@ -41,7 +39,7 @@ def test_date
end

def test_boolean_param
r = @prepared_connection.query("SELECT * FROM posts WHERE active = ?", true)
r = @connection.query("SELECT * FROM posts WHERE active = ?", true)

assert_last_stmt "SELECT * FROM posts WHERE active = $1"
assert_equal 2, r[0].id
Expand All @@ -50,7 +48,7 @@ def test_boolean_param

def test_numbers_param
r =
@prepared_connection.query(
@connection.query(
"select :price::decimal AS price, :quantity::int AS quantity",
price: 20.5,
quantity: 3
Expand All @@ -62,54 +60,22 @@ def test_numbers_param
end

def test_limit_prepared_cache
@prepared_connection.instance_variable_set(:@prepared_cache, MiniSql::Postgres::PreparedCache.new(@unprepared_connection, 1))
@connection.instance_variable_set(:@prepared_cache, MiniSql::Postgres::PreparedCache.new(@unprepared_connection, 1))

assert_equal @prepared_connection.query_single("SELECT ?", 1), %w[1]
assert_equal @prepared_connection.query_single("SELECT ?, ?", 1, 2), %w[1 2]
assert_equal @prepared_connection.query_single("SELECT ?, ?, ?", 1, 2, 3), %w[1 2 3]
assert_equal @connection.query_single("SELECT ?", 1), %w[1]
assert_equal @connection.query_single("SELECT ?, ?", 1, 2), %w[1 2]
assert_equal @connection.query_single("SELECT ?, ?, ?", 1, 2, 3), %w[1 2 3]

ps = @unprepared_connection.query("select * from pg_prepared_statements")
assert_equal ps.size, 1
assert_equal ps[0].statement, "SELECT $1, $2, $3"
end

def test_single_named_param
r = @prepared_connection.query_single("select :n, :n, :n", n: "test")
r = @connection.query_single("select :n, :n, :n", n: "test")

assert_last_stmt "select $1, $1, $1"
assert_equal %w[test test test], r
end

def test_array_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true).prepared

ints = [1, 2, 3]
strings = %w[a b c]
empty_array = []
row = connection.query_single("select ?::int[], ?::text[], ?::int[]", ints, strings, empty_array)

assert_equal(row, [ints, strings, empty_array])
end

def test_simple_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true).prepared

int = 1
str = "str"
date = Date.new(2020, 10, 10)
row = connection.query_single("select ?::int, ?, ?::date", int, str, date)

assert_equal(row, [int, str, date])
end

def test_hash_params_with_auto_encode_arrays
connection = pg_connection(auto_encode_arrays: true).prepared

num = 1
date = Date.new(2020, 10, 10)
ints = [1, 2, 3]
row = connection.query_single("select :num::int, :date::date, :ints::int[]", num: num, date: date, ints: ints)

assert_equal(row, [num, date, ints])
end
end
14 changes: 9 additions & 5 deletions test/mini_sql/prepared_connection_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module MiniSql::PreparedConnectionTests

def setup
def setup_tables
@unprepared_connection.exec "CREATE TEMPORARY table posts(id int, title text, active bool)"
@unprepared_connection.exec "INSERT INTO posts(id, title, active) VALUES(1, 'ruby', false), (2, 'super', true), (3, 'language', false)"
end
Expand All @@ -14,7 +14,7 @@ def assert_last_stmt(statement_sql, msg = nil)
end

def test_disable_prepared
@prepared_connection.prepared(false).exec('select 1')
@connection.prepared(false).exec('select 1')
assert_nil(last_prepared_statement)
end

Expand All @@ -33,7 +33,9 @@ def test_builder
end

def test_array_hash_params
r = @prepared_connection.query("SELECT id, title FROM posts WHERE id IN (:ids)", ids: [2, 3])
return if @unprepared_connection.respond_to?(:array_encoder) && @unprepared_connection.array_encoder

r = @connection.query("SELECT id, title FROM posts WHERE id IN (:ids)", ids: [2, 3])

assert_last_stmt "SELECT id, title FROM posts WHERE id IN ($1, $2)"
assert_equal 2, r[0].id
Expand All @@ -42,7 +44,9 @@ def test_array_hash_params
end

def test_array_simple_params
r = @prepared_connection.query("SELECT id, title FROM posts WHERE id IN (?)", [2, 3])
return if @unprepared_connection.respond_to?(:array_encoder) && @unprepared_connection.array_encoder

r = @connection.query("SELECT id, title FROM posts WHERE id IN (?)", [2, 3])

assert_last_stmt "SELECT id, title FROM posts WHERE id IN ($1, $2)"
assert_equal 2, r[0].id
Expand All @@ -51,7 +55,7 @@ def test_array_simple_params
end

def test_string_param
r = @prepared_connection.query_single('SELECT :title', title: 'The ruby')
r = @connection.query_single('SELECT :title', title: 'The ruby')

assert_last_stmt "SELECT $1"
assert_equal('The ruby', r[0])
Expand Down
6 changes: 3 additions & 3 deletions test/mini_sql/sqlite/prepared_connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class MiniSql::Sqlite::TestPreparedConnection < Minitest::Test

def setup
@unprepared_connection = sqlite3_connection
@prepared_connection = @unprepared_connection.prepared
@connection = @unprepared_connection.prepared

super
setup_tables
end

STMT_SQL = "select * from sqlite_stmt"
Expand All @@ -26,7 +26,7 @@ def assert_last_stmt(statement_sql)
end

def test_boolean_param
r = @prepared_connection.query("SELECT * FROM posts WHERE active = ?", 1)
r = @connection.query("SELECT * FROM posts WHERE active = ?", 1)

assert_last_stmt "SELECT * FROM posts WHERE active = $1"
assert_equal 2, r[0].id
Expand Down

0 comments on commit 35e3eb6

Please sign in to comment.