<commit_before>require 'ruby_event_store/rom'
require_relative '../../../support/helpers/rspec_defaults'
require 'dry/inflector'

require 'active_support/notifications'
ROM::SQL.load_extensions(:active_support_notifications, :rails_log_subscriber)

ENV['DATABASE_URL'] ||= 'sqlite:db.sqlite3'

module RubyEventStore
  module ROM
    class SpecHelper
      attr_reader :rom_container

      def initialize(database_uri = ENV['DATABASE_URL'])
        config = ::ROM::Configuration.new(
          :sql,
          database_uri,
          max_connections: database_uri =~ /sqlite/ ? 1 : 5,
          preconnect: :concurrently,
          fractional_seconds: true,
        )
        # $stdout.sync = true
        # config.default.use_logger Logger.new(STDOUT)
        config.default.run_migrations

        @rom_container = ROM.setup(config)
      end

      def run_lifecycle
        load_gateway_schema
        yield
      ensure
        drop_gateway_schema
      end

      def gateway
        rom_container.gateways.fetch(:default)
      end

      def supports_concurrent_auto?
        has_connection_pooling?
      end

      def supports_concurrent_any?
        has_connection_pooling?
      end

      def supports_binary?
        ENV['DATA_TYPE'] == 'text'
      end

      def supports_upsert?
        true
      end

      def has_connection_pooling?
        !gateway_type?(:sqlite)
      end

      def connection_pool_size
        gateway.connection.pool.size
      end

      def cleanup_concurrency_test
      end

      def rescuable_concurrency_test_errors
        [::ROM::SQL::Error]
      end

      def supports_position_queries?
        true
      end

      protected

      def gateway_type?(name)
        gateway.connection.database_type.eql?(name)
      end

      def load_gateway_schema
        gateway.run_migrations
      end

      def drop_gateway_schema
        gateway.connection.drop_table?('event_store_events')
        gateway.connection.drop_table?('event_store_events_in_streams')
        gateway.connection.drop_table?('schema_migrations')
      end
    end
  end
end

RSpec::Matchers.define :match_query_count_of do |expected_count|
  match do |query|
    count = 0
    ActiveSupport::Notifications.subscribed(
      lambda do |_name, _started, _finished, _unique_id, payload|
        unless %w[ CACHE SCHEMA ].include?(payload[:name])
          count += 1
        end
      end,
      "sql.rom",
      &actual
    )
    values_match?(expected_count, count)
  end
  supports_block_expectations
  diffable
end

<commit_msg>Follow pattern from RESAR specs<commit_after>require 'ruby_event_store/rom'
require_relative '../../../support/helpers/rspec_defaults'
require 'dry/inflector'

require 'active_support/notifications'
ROM::SQL.load_extensions(:active_support_notifications, :rails_log_subscriber)

ENV['DATABASE_URL'] ||= 'sqlite:db.sqlite3'

module RubyEventStore
  module ROM
    class SpecHelper
      attr_reader :rom_container

      def initialize(database_uri = ENV['DATABASE_URL'])
        config = ::ROM::Configuration.new(
          :sql,
          database_uri,
          max_connections: database_uri =~ /sqlite/ ? 1 : 5,
          preconnect: :concurrently,
          fractional_seconds: true,
        )
        config.default.use_logger(Logger.new(STDOUT)) if ENV.has_key?("VERBOSE")
        config.default.run_migrations

        @rom_container = ROM.setup(config)
      end

      def run_lifecycle
        load_gateway_schema
        yield
      ensure
        drop_gateway_schema
      end

      def gateway
        rom_container.gateways.fetch(:default)
      end

      def supports_concurrent_auto?
        has_connection_pooling?
      end

      def supports_concurrent_any?
        has_connection_pooling?
      end

      def supports_binary?
        ENV['DATA_TYPE'] == 'text'
      end

      def supports_upsert?
        true
      end

      def has_connection_pooling?
        !gateway_type?(:sqlite)
      end

      def connection_pool_size
        gateway.connection.pool.size
      end

      def cleanup_concurrency_test
      end

      def rescuable_concurrency_test_errors
        [::ROM::SQL::Error]
      end

      def supports_position_queries?
        true
      end

      protected

      def gateway_type?(name)
        gateway.connection.database_type.eql?(name)
      end

      def load_gateway_schema
        gateway.run_migrations
      end

      def drop_gateway_schema
        gateway.connection.drop_table?('event_store_events')
        gateway.connection.drop_table?('event_store_events_in_streams')
        gateway.connection.drop_table?('schema_migrations')
      end
    end
  end
end

RSpec::Matchers.define :match_query_count_of do |expected_count|
  match do |query|
    count = 0
    ActiveSupport::Notifications.subscribed(
      lambda do |_name, _started, _finished, _unique_id, payload|
        unless %w[ CACHE SCHEMA ].include?(payload[:name])
          count += 1
        end
      end,
      "sql.rom",
      &actual
    )
    values_match?(expected_count, count)
  end
  supports_block_expectations
  diffable
end


{'fraction_non_alphanumeric': 0.07982045277127245, 'fraction_numerical': 0.00195160031225605, 'mean_word_length': 2.4651791751183234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6808482', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1544, 'n_words': 289}