Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find out data leak from the rspec example
RSpec.configure do |config|
def count_rows_from_all_tables
Hash[
ActiveRecord::Base.connection.tables.map do |tbl|
[tbl, ActiveRecord::Base.connection.execute("select count(*) from #{tbl}").values.last.first.to_i]
end
]
end
config.around :example do |example|
old_counts = count_rows_from_all_tables
example.run
new_counts = count_rows_from_all_tables
diff_counts = Hash[new_counts.select { |tbl, new_count| new_count > old_counts[tbl] }]
if diff_counts.present?
FileUtils.mkdir_p('/tmp/test-results/rspec') unless File.exists?('/tmp/test-results/rspec')
File.open('/tmp/test-results/rspec/db_changes.json', 'a') do |file|
file << {
desc: example.full_description,
location: example.location,
result: {
runtime: example.clock.now.to_f - example.execution_result.started_at.to_f
},
changed_tables: diff_counts
}.to_json
file << "\n"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.