require 'test_helper' describe DataSeeder, :model do describe 'when run with txt files' do before do @name = 'test.txt' @seed_dir = setup_seed_dir(@name, 'countries.txt', 'states.txt') end after do cleanup_seed_dir(@name) end it 'should load seed files' do modify_seed_file(@name, 'states.txt') do |body| body.sub('KY Kentucky', 'KV Kentucky').sub('VT Vermont', 'VT Vermount') end DataSeeder.run(seed_dir: @seed_dir) assert_equal 50, State.count assert_equal 'United States', Country.find_by(code: 'US').try(:name) assert_equal 'Kentucky', State.find_by(code: 'KV').try(:name) assert_equal 'Vermount', State.find_by(code: 'VT').try(:name) modify_seed_file(@name, 'states.txt') do |body| body.sub('KV Kentucky', 'KY Kentucky').sub('VT Vermount', 'VT Vermont') end DataSeeder.run(seed_dir: @seed_dir) assert_equal 50, State.count assert_equal 'Kentucky', State.find_by(code: 'KY').try(:name) assert_equal 'Vermont', State.find_by(code: 'VT').try(:name) assert_nil State.find_by(code: 'KV') end end %w(csv json yml).each do |loader_type| describe "when run with #{loader_type} files" do before do @name = "test.#{loader_type}" @file = "states.#{loader_type}" @seed_dir = setup_seed_dir(@name, @file) end after do cleanup_seed_dir(@name) end it 'should load seed files' do modify_seed_file(@name, @file) do |body| body.sub('Alaska', 'Alaskaska') end DataSeeder.run(seed_dir: @seed_dir) assert_equal 50, State.count assert_equal 'Alaskaska', State.find_by(code: 'AK').try(:name) modify_seed_file(@name, @file) do |body| body.sub('Alaskaska', 'Alaska') end DataSeeder.run(seed_dir: @seed_dir) assert_equal 50, State.count assert_equal 'Alaska', State.find_by(code: 'AK').try(:name) end end end describe 'when run with a custom loader' do before do @name = 'test.custom' @seed_dir = setup_seed_dir(@name, 'states.txt', 'foo.err', 'bar.err') end after do cleanup_seed_dir(@name) end it 'should load seed files' do modify_seed_file(@name, 'states.txt') do |body| body.sub('KY Kentucky', 'KV Kentucky').sub('VT Vermont', 'VT Vermount') end DataSeeder.run(seed_dir: @seed_dir, loaders: {'err' => AppErrorDataSeeder.new}) assert_equal 50, State.count assert_equal 'Kentucky', State.find_by(code: 'KV').try(:name) assert_equal 'Vermount', State.find_by(code: 'VT').try(:name) assert_equal 2, App.count assert App.find_by(name: 'foo') bar = App.find_by(name: 'bar') assert bar assert 3, bar.app_errors.count assert_equal 'Error message for B1', bar.app_errors.find_by(code: 'B1').try(:message) modify_seed_file(@name, 'states.txt') do |body| body.sub('KV Kentucky', 'KY Kentucky').sub('VT Vermount', 'VT Vermont') end modify_seed_file(@name, 'bar.err') do |body| body.sub('B1 Error message for B1', 'C1 Error message for C1') end DataSeeder.run(seed_dir: @seed_dir, loaders: {'err' => AppErrorDataSeeder.new}) assert_equal 50, State.count assert_equal 'Kentucky', State.find_by(code: 'KY').try(:name) assert_equal 'Vermont', State.find_by(code: 'VT').try(:name) assert_nil State.find_by(code: 'KV') assert_equal 2, App.count assert App.find_by(name: 'foo') bar = App.find_by(name: 'bar') assert bar assert 3, bar.app_errors.count assert_nil bar.app_errors.find_by(code: 'B1') assert_equal 'Error message for C1', bar.app_errors.find_by(code: 'C1').try(:message) FileUtils.cp(Rails.root.join('db', 'seed.test', 'zulu.err'), @seed_dir) DataSeeder.run(seed_dir: @seed_dir, loaders: {'err' => AppErrorDataSeeder.new}) assert_equal 50, State.count assert_equal 3, App.count zulu = App.find_by(name: 'zulu') assert zulu assert_equal 2, zulu.app_errors.count assert zulu.app_errors.find_by(code: 'Z1') end end def setup_seed_dir(name, *files) dir_name = seed_dir_name(name) FileUtils.mkdir_p(dir_name) files.each do |file| FileUtils.cp(Rails.root.join('db', 'seed.test', file), dir_name) end return dir_name end def cleanup_seed_dir(name) FileUtils.rm_rf(seed_dir_name(name)) end def modify_seed_file(name, file, &block) file_name = seed_file_name(name, file) body = File.read(file_name) File.open(file_name, 'w') do |f| f.write yield(body) end end def seed_dir_name(name) Rails.root.join('tmp', "db.seed.#{name}.#{$$}") end def seed_file_name(name, file) File.join(seed_dir_name(name), file) end end