Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ZipGenotypingFiles #527

Merged
merged 6 commits into from
Apr 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for Zipgenotypingfiles worker
  • Loading branch information
tsujigiri committed Apr 18, 2020
commit 8be25bd8822be87c545eead612d159b9304b07dc
114 changes: 114 additions & 0 deletions spec/workers/zipgenotypingfiles_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true

RSpec.describe Zipgenotypingfiles do
subject(:worker) { described_class.new }

let!(:tentacle) { create(:phenotype, characteristic: 'tentacle') }

context 'when there are genotypes found' do
# User with matching phenotype and variant
let!(:user_1) { create(:user) }
let!(:genotype_1) do
create(:genotype, user: user_1, genotype: genotype_file_1)
end
let(:genotype_file_1) do
StringIO.new('user 1 genotype')
end
let!(:user_phenotype_1) do
create(
:user_phenotype,
user: user_1,
phenotype: tentacle,
variation: 'purple'
)
end

# Second user with matching phenotype and variant
let!(:user_2) { create(:user) }
let!(:genotype_2) do
create(:genotype, user: user_2, genotype: genotype_file_2)
end
let(:genotype_file_2) do
StringIO.new('user 2 genotype')
end
let!(:user_phenotype_2) do
create(
:user_phenotype,
user: user_2,
phenotype: tentacle,
variation: 'also purple'
)
end

# User with other variant
let!(:user_3) { create(:user) }
let!(:genotype_3) do
create(:genotype, user: user_3, genotype: genotype_file_3)
end
let(:genotype_file_3) do
StringIO.new('user 3 genotype')
end
let!(:user_phenotype_3) do
create(
:user_phenotype,
user: user_3,
phenotype: tentacle,
variation: 'green'
)
end

let!(:slime) { create(:phenotype, characteristic: 'slime') }

# User with other phenotype
let!(:user_4) { create(:user) }
let!(:genotype_4) do
create(:genotype, user: user_4, genotype: genotype_file_4)
end
let(:genotype_file_4) do
StringIO.new('user 4 genotype')
end
let!(:user_phenotype_4) do
create(
:user_phenotype,
user: user_4,
phenotype: slime,
variation: 'purple'
)
end

it 'zips genotyping files' do
worker.perform(
tentacle.id,
user_phenotype_1.variation,
'user@example.com'
)

mail = ActionMailer::Base.deliveries.last
expect(mail.subject)
.to eq('openSNP.org: The data you requested is ready to be downloaded')

file = mail.text_part.decoded[%r{data/zip/.*?\.zip}]

Zip::File.open(Rails.root.join('public', file)) do |zip_file|
expect(zip_file.entries.count).to eq(2)
expect(zip_file.glob("user#{user_1.id}_*.txt").first.get_input_stream.read)
.to eq(File.read(genotype_1.genotype.path))
expect(zip_file.glob("user#{user_2.id}_*.txt").first.get_input_stream.read)
.to eq(File.read(genotype_2.genotype.path))
end
end
end

context 'when there are no genotypes found' do
it 'tells the user' do
worker.perform(
tentacle.id,
'blue',
'user@example.com'
)

expect(ActionMailer::Base.deliveries.last.subject)
.to eq('openSNP.org: No genotyping files match your search')
end
end
end