Module: TestProf::AnyFixture

Defined in:
lib/test_prof/any_fixture.rb

Overview

Make DB fixtures from blocks.

Defined Under Namespace

Classes: Cache

Constant Summary collapse

INSERT_RXP =
/^INSERT INTO ([\S]+)/

Class Method Summary collapse

Class Method Details

.cleanObject

Clean all affected tables (but do not reset cache)



36
37
38
39
40
41
42
# File 'lib/test_prof/any_fixture.rb', line 36

def clean
  tables_cache.keys.reverse_each do |table|
    ActiveRecord::Base.connection.execute %(
      DELETE FROM #{table}
    )
  end
end

.register(id) ⇒ Object

Register a block of code as a fixture, returns the result of the block execution



27
28
29
30
31
32
33
# File 'lib/test_prof/any_fixture.rb', line 27

def register(id)
  cache.fetch(id) do
    ActiveSupport::Notifications.subscribed(method(:subscriber), "sql.active_record") do
      yield
    end
  end
end

.resetObject

Reset all information and clean tables



45
46
47
48
49
# File 'lib/test_prof/any_fixture.rb', line 45

def reset
  clean
  tables_cache.clear
  cache.clear
end

.subscriber(_event, _start, _finish, _id, data) ⇒ Object



51
52
53
54
# File 'lib/test_prof/any_fixture.rb', line 51

def subscriber(_event, _start, _finish, _id, data)
  matches = data.fetch(:sql).match(INSERT_RXP)
  tables_cache[matches[1]] = true if matches
end