Repository URL to install this package:
|
Version:
7.3.3 ▾
|
# frozen_string_literal: true
require 'delegate'
module Faculty
module DatabaseHelper
# RSpec helpers for database things
module Helpers
def with_cleanup(...)
around do |example|
runner = CleanupRunner.new(self)
runner.instance_eval(...)
DB.transaction(auto_savepoint: true, rollback: :always) { example.run }
ensure
runner.records.reverse_each(&:cleanup)
end
end
end
# Wrapper for running a set of cleanup commands and capturing the records
class CleanupRunner < SimpleDelegator
attr_reader :records
def initialize(...)
super
@records = []
end
def row(record)
@records << record
end
def rows(records)
# We can't use Array() to coerce, as Array({id: 10}) => [[:id, 10]], not [{ id: 10 }]
@records += records.is_a?(Array) ? records : [records]
end
end
end
end