<commit_before>require "suture/wrap/sqlite"
require "suture/adapter/log"
require "suture/value/observation"
require "suture/error/observation_conflict"

module Suture::Adapter
  class Dictaphone
    include Suture::Adapter::Log

    def initialize(plan)
      @db = Suture::Wrap::Sqlite.init(plan.database_path)
      @name = plan.name
      @comparator = plan.comparator
      if plan.respond_to?(:args) # does not apply to TestPlan objects
        @args_inspect = plan.args.inspect
        @args_dump = Marshal.dump(plan.args)
      end
    end

    def record(result)
      Suture::Wrap::Sqlite.insert(@db, :observations, [:name, :args, :result],
                                  [@name.to_s, @args_dump, Marshal.dump(result)])
      log_info("recorded call for seam #{@name.inspect} with args `#{@args_inspect}` and result `#{result.inspect}`")
    rescue SQLite3::ConstraintException => e
      old_observation = known_observation
      if @comparator.call(old_observation.result, result)
        log_debug("skipped recording of duplicate call for seam #{@name.inspect} with args `#{@args_inspect}` and result `#{result.inspect}`")
      else
        raise Suture::Error::ObservationConflict.new(@name, @args_inspect, result, old_observation)
      end
    end

    def play(only_id = nil)
      rows = if only_id
        Suture::Wrap::Sqlite.select(@db, :observations, "where name = ? and id = ?", [@name.to_s, only_id])
      else
        Suture::Wrap::Sqlite.select(@db, :observations, "where name = ?", [@name.to_s])
      end
      log_debug("found #{rows.size} recorded calls for seam #{@name.inspect}.")
      rows.map do |row|
        Suture::Value::Observation.new(
          row[0],
          row[1].to_sym,
          Marshal.load(row[2]),
          Marshal.load(row[3])
        )
      end
    end

    def delete(id)
      log_info("deleting call with ID: #{id}")
      Suture::Wrap::Sqlite.delete(@db, :observations, "where id = ?", [id])
    end

  private

    def row_to_observation(row)
      Suture::Value::Observation.new(
        row[0],
        row[1].to_sym,
        Marshal.load(row[2]),
        Marshal.load(row[3])
      )
    end

    def known_observation
      row_to_observation(Suture::Wrap::Sqlite.select(
        @db,
        :observations,
        "where name = ? and args = ?",
        [@name.to_s, @args_dump]
      ).first)
    end
  end
end
<commit_msg>Code climate caught a thing!<commit_after>require "suture/wrap/sqlite"
require "suture/adapter/log"
require "suture/value/observation"
require "suture/error/observation_conflict"

module Suture::Adapter
  class Dictaphone
    include Suture::Adapter::Log

    def initialize(plan)
      @db = Suture::Wrap::Sqlite.init(plan.database_path)
      @name = plan.name
      @comparator = plan.comparator
      if plan.respond_to?(:args) # does not apply to TestPlan objects
        @args_inspect = plan.args.inspect
        @args_dump = Marshal.dump(plan.args)
      end
    end

    def record(result)
      Suture::Wrap::Sqlite.insert(@db, :observations, [:name, :args, :result],
                                  [@name.to_s, @args_dump, Marshal.dump(result)])
      log_info("recorded call for seam #{@name.inspect} with args `#{@args_inspect}` and result `#{result.inspect}`")
    rescue SQLite3::ConstraintException => e
      old_observation = known_observation
      if @comparator.call(old_observation.result, result)
        log_debug("skipped recording of duplicate call for seam #{@name.inspect} with args `#{@args_inspect}` and result `#{result.inspect}`")
      else
        raise Suture::Error::ObservationConflict.new(@name, @args_inspect, result, old_observation)
      end
    end

    def play(only_id = nil)
      rows = if only_id
        Suture::Wrap::Sqlite.select(@db, :observations, "where name = ? and id = ?", [@name.to_s, only_id])
      else
        Suture::Wrap::Sqlite.select(@db, :observations, "where name = ?", [@name.to_s])
      end
      log_debug("found #{rows.size} recorded calls for seam #{@name.inspect}.")
      rows.map(&self.method(:row_to_observation))
    end

    def delete(id)
      log_info("deleting call with ID: #{id}")
      Suture::Wrap::Sqlite.delete(@db, :observations, "where id = ?", [id])
    end

  private

    def row_to_observation(row)
      Suture::Value::Observation.new(
        row[0],
        row[1].to_sym,
        Marshal.load(row[2]),
        Marshal.load(row[3])
      )
    end

    def known_observation
      row_to_observation(Suture::Wrap::Sqlite.select(
        @db,
        :observations,
        "where name = ? and args = ?",
        [@name.to_s, @args_dump]
      ).first)
    end
  end
end

{'fraction_non_alphanumeric': 0.1356331690593529, 'fraction_numerical': 0.0029997857295907433, 'mean_word_length': 2.8356614626129826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4418324', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1533, 'n_words': 320}