Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mostlyobvious/En57. DCB-compatible event store library in Ruby with support for PostgreSQL.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161# frozen_string_literal: true
require "test_helper"
module En57 class TestActiveRecordAdapter < Minitest::Test cover ActiveRecordAdapter
def test_with_connection_checks_out_connection_and_yields_raw_connection with_mock_adapter do |pool, connection, raw_connection, adapter| pool.expect(:with_connection, :selected) do |&block| block.call(connection) true end connection.expect(:raw_connection, raw_connection) raw_connection.expect(:exec, :selected, ["SELECT 1"])
assert_equal :selected, adapter.with_connection { |conn| conn.exec("SELECT 1") } end end
def test_with_serializable_transaction_wraps_block_in_transaction with_mock_adapter do |pool, connection, raw_connection, adapter| pool.expect(:with_connection, :committed) do |&block| block.call(connection) true end connection.expect(:transaction, :committed) do |options, &block| assert_equal({ isolation: :serializable }, options) block.call true end connection.expect(:raw_connection, raw_connection) raw_connection.expect( :exec_params, :written, ["SELECT en57.append_events()", []], )
assert_equal( :committed, adapter.with_serializable_transaction do |conn| assert_equal :written, conn.exec_params("SELECT en57.append_events()", []) end, ) end end
def test_serialization_error_returns_active_record_serialization_failure with_mock_adapter do |_pool, _connection, _raw_connection, adapter| assert_same ActiveRecord::SerializationFailure, adapter.serialization_error end end
def test_with_serializable_transaction_reraises_block_errors error = RuntimeError.new("boom")
raised = assert_raises(RuntimeError) do with_mock_adapter do |pool, connection, raw_connection, adapter| pool.expect(:with_connection, nil) do |&block| block.call(connection) true end connection.expect(:transaction, nil) do |options, &block| assert_equal({ isolation: :serializable }, options) block.call true end connection.expect(:raw_connection, raw_connection) raw_connection.expect(:exec_params, nil) do |sql, params| assert_equal "SELECT en57.append_events()", sql assert_equal [], params raise error end
adapter.with_serializable_transaction do |conn| conn.exec_params("SELECT en57.append_events()", []) end end end
assert_same error, raised end
def test_with_transaction_wraps_block_in_transaction with_mock_adapter do |pool, connection, raw_connection, adapter| pool.expect(:with_connection, :committed) do |&block| block.call(connection) true end connection.expect(:transaction, :committed) do |&block| block.call true end connection.expect(:raw_connection, raw_connection) raw_connection.expect( :exec_params, :written, ["SELECT en57.append_events()", []], )
assert_equal( :committed, adapter.with_transaction do |conn| assert_equal :written, conn.exec_params("SELECT en57.append_events()", []) end, ) end end
def test_with_transaction_reraises_block_errors error = RuntimeError.new("boom")
raised = assert_raises(RuntimeError) do with_mock_adapter do |pool, connection, raw_connection, adapter| pool.expect(:with_connection, nil) do |&block| block.call(connection) true end connection.expect(:transaction, nil) do |&block| block.call true end connection.expect(:raw_connection, raw_connection) raw_connection.expect(:exec_params, nil) do |sql, params| assert_equal "SELECT en57.append_events()", sql assert_equal [], params raise error end
adapter.with_transaction do |conn| conn.exec_params("SELECT en57.append_events()", []) end end end
assert_same error, raised end
private
def with_mock_adapter pool = Minitest::Mock.new connection = Minitest::Mock.new raw_connection = Minitest::Mock.new
yield pool, connection, raw_connection, ActiveRecordAdapter.new(pool) ensure pool.verify connection.verify raw_connection.verify end endend