Module: Minitest::Instrument::Db

Defined in:
lib/minitest-instrument-db.rb,
lib/minitest-instrument-db/version.rb

Defined Under Namespace

Classes: MinitestTestMethod

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.configuration_at(database_yml, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/minitest-instrument-db.rb', line 20

def self.configuration_at(database_yml, options = {})
  options[:project] ||= "Unknown"
  options[:machine] ||= begin
    require 'socket'
    Socket.gethostname
  end
  
  connection = ActiveRecord::Base.connection rescue nil
  ActiveRecord::Base.configurations.merge! YAML::load(  IO.read( database_yml )  )
  ActiveRecord::Base.establish_connection("minitest-instrument-db")

  ActiveRecord::Migrator.migrate(  File.join( File.dirname(__FILE__), 'migrations' )  )

  MinitestTestMethod.establish_connection("minitest-instrument-db")
  
  ActiveSupport::Notifications.subscribe "test.finished" do |name, start, finish, id, payload|
    MinitestTestMethod.create!(
      project: options[:project],
      machine: options[:machine],
      test_id: id,
      klass: payload[:klass],
      method: payload[:test],
      exception: !!payload[:exception],
      execution_time: finish - start
      # created_at !!
    )
  end
ensure
  if connection
    config = ActiveRecord::Base.connection.instance_variable_get(:@config).stringify_keys
    name, configuration = ActiveRecord::Base.configurations.find{|k,v| v == config}
    ActiveRecord::Base.establish_connection(name)
  end
end

.try_configuration_at(*params) ⇒ Object



14
15
16
17
18
# File 'lib/minitest-instrument-db.rb', line 14

def self.try_configuration_at(*params)
  configuration_at(*params)
rescue
  puts "Instrumenting test methods is disabled due to exception"
end