No Clean Feed - Stop Internet Censorship in Australia

Hack to reset auto increment on MySQL tables during RSelenese tests

If you have an RSelenese test (i.e. a test for Selenium on Rails) that creates an entity, it helps to know what the ID of the entity is. Unfortunately the auto-increment functionality in MySQL will prevent you from figuring it out; imagine the following test:

setup :fixtures => :all
delete_cookie '_dogserver_session', '/'
open '/selenium/setup'

login_as('quentin', 'monkey')
create_dog()
click_and_wait 'edit_dog_12'

Provided you know that your fixtures load 11 Dog objects, you're okay. However, what if you ran that test a second time? The fixture would be reloaded, but the auto-increment value for the dogs table would remain at 13 ... meaning that the test would fail as the Dog it creates would have an ID of 13.

A quick and dirty way of doing this is to reset the auto increment values for the tables we care about in the setup action on the Selenium controller. Adding the following code:


config = Rails::Configuration.new
str_database = config.database_configuration[RAILS_ENV]["database"]
str_username = config.database_configuration[RAILS_ENV]["username"]
str_password = config.database_configuration[RAILS_ENV]["password"]

conn = Mysql.real_connect("localhost", str_username, str_password, str_database)
# replace [ Dog, Owner, User] with the models whose tables you want to reset
for class_ar_each in [ Dog, Owner, User ]
  id = class_ar_each.find(:first, :order => "id DESC").id + 1
  conn.query("ALTER TABLE #{class_ar_each.table_name} AUTO_INCREMENT = #{id}")
end
conn.close()

... to the setup method in vendor/plugins/selenium_on_rails/lib/controllers/selenium_controller.rb and you're good to go.

To be perfectly clear: this is a hack, it only works on MySQL, there is no error handling, and it assumes there is at least one record loaded into each of the tables by fixtures. When / if time permits I'll look at submitting a patch for this and cleaning it up somewhat, but until then, you're on your own. YMMV, etc.