Class: Suture::Adapter::Dictaphone

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/suture/adapter/dictaphone.rb

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_info, #log_warn, logger, reset!

Constructor Details

#initialize(plan) ⇒ Dictaphone

Returns a new instance of Dictaphone.



10
11
12
13
14
15
16
17
18
# File 'lib/suture/adapter/dictaphone.rb', line 10

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

Instance Method Details

#delete(id) ⇒ Object



43
44
45
46
# File 'lib/suture/adapter/dictaphone.rb', line 43

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

#play(only_id = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/suture/adapter/dictaphone.rb', line 33

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

#record(result) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/suture/adapter/dictaphone.rb', line 20

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
  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