Austin Story

Ruby, Rails and Javascript Blog

Powered by Genesis

Script to generate fixtures from a database

February 7, 2015 By Austin Story 1 Comment

So I have used minitest for about 8 months now for testing and all in all it turns out that I loved it more than I expected.  I just moved back to an in progress in house project written using Rspec with FactoryGirl and realized I want my minitest and  fixtures back.  So i found a great script that will generate fixturess based on my database seed.

 

[ruby]
sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.tables.each do |table_name|
i = "000"
File.open("#{Rails.root}/test/fixtures/#{table_name}.yml", 'w') do |file|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
file.write data.inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end

[/ruby]

Filed Under: Minitest, Ruby, Ruby on Rails

Comments

  1. pablo says

    January 6, 2016 at 4:56 am

    Thanks for the script. There are a small typo, missing parenthesis at beginning of line 4

    Reply

Leave a Reply to pablo Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • AngularJS
  • Books
  • Devise
  • Elasticsearch
  • ES6
  • Information Security
  • Integrations
  • Javascript
  • Linux
  • Minitest
  • PhoneGap
  • Programming
  • React
  • Redux
  • Ruby
  • Ruby on Rails
  • Stripe
  • Testing
  • Theory
  • TypeScript
  • Uncategorized
  • Vue
  • Webpack