diff --git a/Gemfile b/Gemfile index 126fb83..90202e7 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 7c15fcc..f45dbb5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/spec/integration/genotype_parsing_spec.rb b/spec/integration/genotype_parsing_spec.rb new file mode 100644 index 0000000..3f89e91 --- /dev/null +++ b/spec/integration/genotype_parsing_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 66c900f..c2fccc5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/test/unit/parsing_and_deleting_test.rb b/test/unit/parsing_and_deleting_test.rb deleted file mode 100644 index ab8df2a..0000000 --- a/test/unit/parsing_and_deleting_test.rb +++ /dev/null @@ -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