Created
May 30, 2018 14:17
-
-
Save we4tech/e6bc8e5759ac67a75ab3357181074df4 to your computer and use it in GitHub Desktop.
Find out data leak from the rspec example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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