Skip to content

Instantly share code, notes, and snippets.

@artofhuman
Created September 17, 2013 08:58
Show Gist options
  • Save artofhuman/6591836 to your computer and use it in GitHub Desktop.
Save artofhuman/6591836 to your computer and use it in GitHub Desktop.
Spec for rake task
# coding: utf-8
require 'spec_helper'
require 'rake'
describe 'companies:destroy_from_csv' do
let(:filename) { 'old_companies.csv' }
let(:dir) { File.join(Rails.root, 'tmp', 'companies') }
let(:file) { File.join(dir, filename)}
let(:log) { File.join(dir, 'not_deleted.log')}
let(:company) { create :company }
let!(:company_is_not_to_remove ) { create :company }
let!(:contact_is_not_to_remove) { create(:contact, address: company_is_not_to_remove.addresses[0]) }
let!(:settings_is_not_to_remove) do
settings = build(:company_settings, company: company_is_not_to_remove)
settings.save(validate: false)
settings
end
let(:run_rake_task) do
Rake::Task["companies:destroy_from_csv"].reenable
Rake.application.invoke_task "companies:destroy_from_csv"
end
before do
Rake.application.rake_require "tasks/companies"
Rake::Task.define_task(:environment)
build(:company_settings, company: company).save(validate: false)
create(:contact, address: company.addresses[0])
end
context 'when file exists', fakefs: true do
before do
FileUtils.mkdir_p(Rails.root.join('log'))
FileUtils.mkdir_p(dir)
File.open(file, 'w') do |f|
f.puts('id')
f.puts(company.id)
end
end
context 'when delete all companies from csv' do
before { run_rake_task }
it 'only removes the companies that are in the file' do
expect(Company.select(:id).map(&:id)).to eq [company_is_not_to_remove.id]
end
it 'only removes the addresses of the companies that are in the file' do
expect(Address.select(:id).map(&:id)).to eq [company_is_not_to_remove.addresses[0].id]
end
it 'only removes the phones of the companies that are in the file' do
expect(Phone.select(:id).map(&:id)).to eq [company_is_not_to_remove.addresses[0].phones[0].id]
end
it 'only removes the contacts of the companies that are in the file' do
expect(Contact.select(:id).map(&:id)).to eq [contact_is_not_to_remove.id]
end
it 'only removes the regions of companies that are in the file' do
expect(CompanyRegion.select(:id).map(&:id))
.to match_array company_is_not_to_remove.company_regions.map(&:id)
end
it 'only removes the settings of the companies that are in the file' do
expect(CompanySettings.select(:id).map(&:id))
.to eq [settings_is_not_to_remove.id]
end
it 'only removes the company regions commercial of the companies that are in the file' do
expect(CompanyRegionCommercial.select(:id).map(&:id))
.to eq company_is_not_to_remove.company_region_commercials.map(&:id)
end
end
context 'when a company has associations' do
before do
create :user_favorite_company, company: company
run_rake_task
end
it 'does not remove the company' do
expect(Company.select(:id).map(&:id)).to include company.id
end
it 'does not removes the address of company' do
expect(Address.select(:id).map(&:id)).to include company.addresses[0].id
end
it { expect(File.read(log)).to eq "#{company.id}\n" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment