Created
April 13, 2012 10:39
-
-
Save joakimk/2375699 to your computer and use it in GitHub Desktop.
RSpec: Separates id series for each table so that tests does not accidentally pass when rows in different tables have the same ID
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
class RSpec::SeparateIdSeries | |
# Separates id series for each table so that tests does not | |
# accidentally pass when rows in different tables have the same ID | |
def self.setup | |
# This takes almost a second when using spork for some reason, so skipping it there. | |
return if Spork.using_spork? | |
t = Time.now | |
tables.each_with_index do |table, i| | |
Item.connection.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{100000 * (i+1)}") | |
end | |
reset_time = Time.now - t | |
p "Long set id series time: #{reset_time}" if reset_time > 0.5 | |
end | |
def self.tables | |
Item.connection.execute("select tablename from pg_tables;").to_a. | |
map { |x| x["tablename"] }. | |
find_all { |x| !x.include?("pg_") && !x.include?("sql_") && !x.include?("schema_") } | |
end | |
end | |
# in config.before(:suite) do | |
# RSpec::SeparateIdSeries.setup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem this attempts to solve is if you for example have two models, Foo and Bar that is usally created at the same time, they get the same incrementing ID. If you accidentally check for Bar ids when a method return Foo ids, it will pass most of the time by accident.