require 'spec_helper'
require 'ruby_event_store'
require 'ruby_event_store/spec/event_repository_lint'

module RailsEventStoreActiveRecord
  RSpec.describe LegacyEventRepository do
    include SchemaHelper

    around(:each) do |example|
      begin
        establish_database_connection
        load_legacy_database_schema
        example.run
      ensure
        drop_legacy_database
      end
    end

    let(:test_race_conditions_auto) { false }
    let(:test_race_conditions_any)  { !ENV['DATABASE_URL'].include?("sqlite") }

    it_behaves_like :event_repository, LegacyEventRepository

    def cleanup_concurrency_test
      ActiveRecord::Base.connection_pool.disconnect!
    end

    def verify_conncurency_assumptions
      expect(ActiveRecord::Base.connection.pool.size).to eq(5)
    end
  end
end

require 'spec_helper'
require 'ruby_event_store'
require 'ruby_event_store/spec/event_repository_lint'

module RailsEventStoreActiveRecord
  RSpec.describe LegacyEventRepository do
    include SchemaHelper

    around(:each) do |example|
      begin
        establish_database_connection
        load_legacy_database_schema
        example.run
      ensure
        drop_legacy_database
      end
    end

    let(:test_race_conditions_auto) { false }
    let(:test_race_conditions_any)  { !ENV['DATABASE_URL'].include?("sqlite") }

    it_behaves_like :event_repository, LegacyEventRepository

    def cleanup_concurrency_test
      ActiveRecord::Base.connection_pool.disconnect!
    end

    def verify_conncurency_assumptions
      expect(ActiveRecord::Base.connection.pool.size).to eq(5)
    end

    specify "read_stream_events_forward explicit ORDER BY id" do
      expect_query(/SELECT.*FROM.*event_store_events.*WHERE.*event_store_events.*stream.*=.*ORDER BY id ASC.*/) do
        repository = LegacyEventRepository.new
        repository.read_stream_events_forward('stream')
      end
    end

    specify "read_events_forward explicit ORDER BY id" do
      expect_query(/SELECT.*FROM.*event_store_events.*WHERE.*event_store_events.*stream.*=.*ORDER BY id ASC LIMIT.*/) do
        repository = LegacyEventRepository.new
        repository.read_events_forward('stream', :head, 1)
      end
    end

    specify "read_all_streams_forward explicit ORDER BY id" do
      expect_query(/SELECT.*FROM.*event_store_events.*ORDER BY id ASC LIMIT.*/) do
        repository = LegacyEventRepository.new
        repository.read_all_streams_forward(:head, 1)
      end
    end

    private

    def expect_query(match, &block)
      count = 0
      counter_f = ->(_name, _started, _finished, _unique_id, payload) {
        count +=1 if match === payload[:sql]
      }
      ActiveSupport::Notifications.subscribed(counter_f, "sql.active_record", &block)
      expect(count).to eq(1)
    end
  end
end

Make sure database adapter implementation is abstracted for mutation.
Make sure database adapter implementation is abstracted for mutation.

Database engine implementation might come with default sorting order for
certain queries. While RES adapter specifies desired order, mutant can
remove this part of call and see that in fact result is the same
(because default kicked in).

In such case we verify that adapter call produces relevant SQL query.
This effectively hides database engine implementation details.
