Created
November 15, 2010 09:21
-
-
Save peter/700194 to your computer and use it in GitHub Desktop.
Rails Migration With Tests
This file contains 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 ImportLegacyDevices < ActiveRecord::Migration | |
def self.up | |
return unless Rails.env.production? | |
legacy_devices.each do |device_id, issue| | |
if device = Device.find_by_hardware_id(device_id) | |
unless InclusiveIssue.exists?(:issue_id => issue.id, :device_id => device.id) | |
InclusiveIssue.create!( | |
:issue => issue, | |
:device => device, | |
:from_legacy_system => true | |
) | |
end | |
else | |
device = Device.create(:hardware_id => device_id) | |
unless device.valid? | |
puts "Device is not valid: #{device.errors.inspect}" | |
next | |
end | |
InclusiveIssue.create!( | |
:issue => issue, | |
:device => device, | |
:from_legacy_system => true | |
) | |
end | |
end | |
end | |
def self.down | |
end | |
private | |
def self.read_legacy_data | |
IO.readlines(File.join(Rails.root, "db", "legacy-devices.csv")) | |
end | |
def self.legacy_devices | |
read_legacy_data.map do |line| | |
device_id, issue_title = line.split("\t").map(&:strip) | |
if issue = issues[issue_title] | |
[device_id, issue] | |
else | |
puts "Could not find issue #{issue_title} - skipping!" | |
nil | |
end | |
end.compact | |
end | |
def self.brand | |
@@brand ||= Brand.lookup!("popular-science") | |
end | |
def self.issues | |
@issues ||= brand.issues.inject({}) do |hash, issue| | |
hash[issue.title] = issue | |
hash | |
end | |
end | |
end | |
require 'spec_helper' | |
require File.join(Rails.root, 'db', 'migrate', '20101110154036_import_legacy_devices') | |
describe ImportLegacyDevices do | |
before(:each) do | |
@brand = brands('popular-science') | |
@issue = @brand.issues.first | |
@device = devices(:quentins_pad) | |
end | |
describe "issues" do | |
it "returns a hash with all issues for the brand" do | |
klass.issues.keys.sort.should == @brand.issues.map(&:title).sort | |
klass.issues[@issue.title].should == @issue | |
end | |
end | |
describe "legacy_devices" do | |
it "returns all device ids and issues in the data file" do | |
klass.expects(:read_legacy_data).returns(legacy_data) | |
klass.legacy_devices.should == legacy_devices | |
end | |
end | |
describe "up" do | |
before(:each) do | |
@device.inclusive_issue_mappings.destroy_all | |
end | |
it "should not run unless in production env" do | |
Rails.expects(:env).returns(mock(:production? => false)) | |
klass.up_without_benchmarks.should == nil | |
end | |
it "should create inclusive issues and new devices" do | |
Rails.expects(:env).returns(mock(:production? => true)) | |
klass.expects(:legacy_devices).returns(legacy_devices) | |
lambda { | |
lambda { | |
klass.up_without_benchmarks | |
@device.reload.inclusive_issues.first.should == @issue | |
Device.find_by_hardware_id("new-device-id").inclusive_issues.first.should == @issue | |
}.should change(InclusiveIssue, :count).by(2) | |
}.should change(Device, :count).by(1) | |
end | |
end | |
def legacy_devices | |
[ | |
[@device.hardware_id, @issue], | |
["new-device-id", @issue] | |
] | |
end | |
def legacy_data | |
[ | |
[@device.hardware_id, @issue.title], | |
["new-device-id", @issue.title] | |
].map { |i| i.join("\t") } | |
end | |
def klass | |
ImportLegacyDevices | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment