Method: Webhookdb::SpecHelpers::Postgres.wrap_example_in_transactions

Defined in:
lib/webhookdb/spec_helpers/postgres.rb

.wrap_example_in_transactions(example) ⇒ Object

Run the specified example in the context of a transaction for each loaded model superclass. Raises if any of the loaded superclasses aren’t configured.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/webhookdb/spec_helpers/postgres.rb', line 64

def self.wrap_example_in_transactions(example)
  txn_classes = Webhookdb::Postgres.model_superclasses
  Webhookdb::Postgres.logger.debug "Wrapping example for model superclasses: %p" %
    [txn_classes]

  wrapped_proc = txn_classes.inject(example.method(:run)) do |callback, txn_class|
    if (db = txn_class.db)
      Webhookdb::Postgres.logger.debug "DB: Running with an outer transaction"
      proc { db.transaction(auto_savepoint: :only, rollback: :always, &callback) }
    else
      raise "No database connection for %p configured! Add a %s section to the test config." %
        [txn_class, txn_class.config_key]
    end
  end

  wrapped_proc.call
  return if !SNIFF_LEAKY_TESTS || (Webhookdb::Customer.empty? && Webhookdb::Organization.empty?)
  puts "Customer is not cleaned up, failing for diagnosis."
  puts "Check the spec that ran before: #{example.metadata[:full_description]}"
  exit
end