Skip to content

Instantly share code, notes, and snippets.

@we4tech
Created May 30, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save we4tech/e6bc8e5759ac67a75ab3357181074df4 to your computer and use it in GitHub Desktop.
Save we4tech/e6bc8e5759ac67a75ab3357181074df4 to your computer and use it in GitHub Desktop.
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