Move parsing and deleting test to RSpec

This commit is contained in:
Helge Rausch
2014-08-17 14:23:51 +02:00
parent 97d8b49dd7
commit 7dc7a7c045
5 changed files with 134 additions and 115 deletions

13
Gemfile
View File

@@ -22,9 +22,9 @@ gem 'newrelic_rpm'
# workaround for bug in Fedora
# gem 'sqlite3'
# use postgresql instead:
gem 'pg', :require => 'pg'
# DB
gem 'pg'
gem 'activerecord-import', '~> 0.2.11'
# for solr (indexing, searching)
gem 'sunspot_rails'#, '2.0.0'
@@ -37,7 +37,6 @@ gem "will_paginate"
gem 'nested_form', github: 'ryanb/nested_form'
gem 'json'
gem 'mediawiki-gateway'
gem 'activerecord-import', '~> 0.2.11'
gem 'paperclip', '~> 4.0 '
gem 'friendly_id', github: 'FriendlyId/friendly_id', branch: '4.0-stable' # the branch is for Rails 3
gem 'recommendify', github: 'paulasmuth/recommendify', :ref => "34308c4"
@@ -81,15 +80,13 @@ group :test do
gem 'webmock'
gem 'vcr'
gem 'capybara'
end
group :debug do
gem 'pry-rails'
gem 'database_cleaner'
end
group :development, :test do
gem 'uuidtools'
gem 'rspec-rails'
gem 'pry-rails' unless ENV['CI']
end
group :development do

View File

@@ -127,6 +127,7 @@ GEM
connection_pool (1.1.0)
crack (0.4.1)
safe_yaml (~> 0.9.0)
database_cleaner (1.3.0)
devise (3.0.0)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
@@ -164,7 +165,7 @@ GEM
activesupport (>= 3.2, < 5)
highline (1.6.20)
hike (1.2.3)
i18n (0.6.9)
i18n (0.6.11)
inherited_resources (1.4.1)
has_scope (~> 0.6.0.rc)
responders (~> 1.0.0.rc)
@@ -359,7 +360,7 @@ GEM
execjs
rails (>= 3.1)
railties (>= 3.1)
tzinfo (0.3.39)
tzinfo (0.3.41)
uglifier (2.3.0)
execjs (>= 0.3.0)
json (>= 1.8.0)
@@ -392,6 +393,7 @@ DEPENDENCIES
capistrano (~> 2.0)
capybara
coffee-script
database_cleaner
devise (= 3.0.0)
dynamic_form
exceptional

View File

@@ -0,0 +1,104 @@
require 'spec_helper'
describe 'genotype parsing' do
let(:file_23andMe) { Rails.root.join('test/data/23andMe_test.csv') }
let(:genotype_23andme) do
create(:genotype,
genotype_file_name: file_23andMe.basename,
filetype: '23andme')
end
let(:file_deCODEme) { Rails.root.join('test/data/deCODEme_test.csv') }
let(:genotype_decodeme) do
create(:genotype,
genotype_file_name: file_deCODEme.basename,
filetype: 'decodeme')
end
let(:temp_file) { Rails.root.join('tmp/snp_file.txt') }
before do
allow(Sidekiq::Client).to receive(:enqueue).with(Preparsing, an_instance_of(Fixnum))
FileUtils.rm(temp_file) if File.exist?(temp_file)
end
it "parse 23andMe data", truncate: true do
FileUtils.cp(file_23andMe, temp_file)
Parsing.new.perform(genotype_23andme.id, temp_file)
# Snp
snp_data = Snp.all.map do |s|
[ s.name, s.position, s.chromosome, s.genotype_frequency, s.allele_frequency, s.ranking ]
end.sort_by { |s| s[0] }
expected =
[ [ "rs11240777", "788822", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs12124819", "766409", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3094315", "742429", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3131972", "742584", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs4477212", "72017", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ]]
expect(snp_data).to eq(expected)
# UserSnp
user_snps = UserSnp.all
user_snp_genotypes = user_snps.map(&:local_genotype)
expected_genotypes = %w[ AA AA GG AG AG ]
expect(user_snp_genotypes).to eq(expected_genotypes)
user_snps.each do |s|
expect(s.genotype_id).to eq(genotype_23andme.id)
expect(Snp.pluck(:name)).to include(s.snp_name)
end
end
# could put these deleting tests into their own file;
# however, the genotyping exists at this point in time and we don't have to do any extra work
# to pull it from the test DB
it "delete 23andMe data" do
DeleteGenotype.new.perform(genotype_23andme)
expected = 0
number_of_snps = Snp.all.count
expect(number_of_snps).to eq(expected)
end
it "parse deCODEme data", truncate: true do
FileUtils.cp file_deCODEme, temp_file
Parsing.new.perform(genotype_decodeme.id, temp_file)
# Snp
snp_data = Snp.all.map do |s|
[ s.name, s.position, s.chromosome, s.genotype_frequency, s.allele_frequency, s.ranking, s.user_snps_count ]
end.sort_by { |s| s[0] }
expected =
[ [ "rs11240767", "718814", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs2185539", "556738", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs3094315", "742429", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs4477212", "72017", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs6681105", "581938", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1] ]
expect(snp_data).to eq(expected)
# UserSnp
user_snps = UserSnp.all
user_snp_genotypes = user_snps.map(&:local_genotype)
expected_genotypes = %w[ AA CC TT CC TT ]
expect(user_snp_genotypes).to eq(expected_genotypes)
user_snps.each do |s|
expect(s.genotype_id).to eq(genotype_decodeme.id)
expect(Snp.pluck(:name)).to include(s.snp_name)
end
end
it "delete deCODEme data" do
DeleteGenotype.new.perform(genotype_decodeme)
expected = 0
number_of_snps = Snp.all.count
expect(number_of_snps).to eq(expected)
end
end

View File

@@ -30,7 +30,7 @@ RSpec.configure do |config|
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
@@ -44,4 +44,24 @@ RSpec.configure do |config|
config.order = "random"
config.infer_spec_type_from_file_location!
config.before(:suite) do
RSolr::Connection.any_instance.stubs(:execute)
end
config.before(:example) do
DatabaseCleaner.strategy = :transaction
end
config.before(:example, truncate: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:example) do
DatabaseCleaner.start
end
config.after(:example) do
DatabaseCleaner.clean
end
end

View File

@@ -1,104 +0,0 @@
require_relative '../test_helper'
class ParsingTest < ActiveSupport::TestCase
context "parser" do
setup do
stub_solr
Snp.delete_all
UserSnp.delete_all
@file_23andMe = "#{Rails.root}/test/data/23andMe_test.csv"
Sidekiq::Client.stubs(:enqueue).with(Preparsing, instance_of(Fixnum))
@genotype_23andme = FactoryGirl.create(:genotype,
genotype_file_name: @file_23andMe.split('/').last, filetype: '23andme')
@file_deCODEme = "#{Rails.root}/test/data/deCODEme_test.csv"
@genotype_decodeme = FactoryGirl.create(:genotype,
genotype_file_name: @file_deCODEme.split('/').last, filetype: 'decodeme')
@temp_file = "#{Rails.root}/tmp/snp_file.txt"
FileUtils.rm(@temp_file) if File.exist?(@temp_file)
end
should "parse 23andMe data" do
FileUtils.cp @file_23andMe, @temp_file
Parsing.new.perform(@genotype_23andme.id, @temp_file)
# Snp
snp_data = Snp.all.map do |s|
[ s.name, s.position, s.chromosome, s.genotype_frequency, s.allele_frequency, s.ranking ]
end.sort_by { |s| s[0] }
expected =
[ [ "rs11240777", "788822", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs12124819", "766409", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3094315", "742429", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs3131972", "742584", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ],
[ "rs4477212", "72017", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0 ]]
assert_equal expected, snp_data
# UserSnp
user_snps = UserSnp.all
user_snp_genotypes = user_snps.map(&:local_genotype)
expected_genotypes = %w[ AA AA GG AG AG ]
assert_equal expected_genotypes, user_snp_genotypes
user_snps.each do |s|
assert_equal @genotype_23andme.id, s.genotype_id
assert Snp.pluck(:name).include?(s.snp_name)
end
end
# could put these deleting tests into their own file;
# however, the genotyping exists at this point in time and we don't have to do any extra work
# to pull it from the test DB
should "delete 23andMe data" do
DeleteGenotype.new.perform(@genotype_23andme)
expected = 0
number_of_snps = Snp.all.count
assert_equal expected, number_of_snps
end
should "parse deCODEme data" do
FileUtils.cp @file_deCODEme, @temp_file
Parsing.new.perform(@genotype_decodeme.id, @temp_file)
# Snp
snp_data = Snp.all.map do |s|
[ s.name, s.position, s.chromosome, s.genotype_frequency, s.allele_frequency, s.ranking, s.user_snps_count ]
end.sort_by { |s| s[0] }
expected =
[ [ "rs11240767", "718814", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs2185539", "556738", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs3094315", "742429", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs4477212", "72017", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1],
[ "rs6681105", "581938", "1", {}, {"A"=>0, "T"=>0, "G"=>0, "C"=>0}, 0, 1] ]
assert_equal expected, snp_data
# UserSnp
user_snps = UserSnp.all
user_snp_genotypes = user_snps.map(&:local_genotype)
expected_genotypes = %w[ AA CC TT CC TT ]
assert_equal expected_genotypes, user_snp_genotypes
user_snps.each do |s|
assert_equal @genotype_decodeme.id, s.genotype_id
assert Snp.pluck(:name).include?(s.snp_name)
end
end
should "delete deCODEme data" do
DeleteGenotype.new.perform(@genotype_decodeme)
expected = 0
number_of_snps = Snp.all.count
assert_equal expected, number_of_snps
end
end
end