Skip to content

Commit

Permalink
Added test for process_lineage_variants_data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Valiec committed Jul 22, 2024
1 parent 5ecd4c2 commit 166bb41
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
11 changes: 8 additions & 3 deletions app/models/organism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ def lineage_variants_data(data_server, organism_slug)
return { data_fetched: false }
end

primer_sets = JSON.parse(tracks_req.body)
process_lineage_variants_data tracks_req.body, defaults_req.body, lineages_req.body
end

def process_lineage_variants_data(tracks_json, defaults_json, lineages_json)

primer_sets = JSON.parse(tracks_json)

defaults_parsed = JSON.parse(defaults_req.body)
defaults_parsed = JSON.parse(defaults_json)
default_tracks = defaults_parsed['tracks']
default_lineage = defaults_parsed['lineage']

lineage_sets = JSON.parse(lineages_req.body)
lineage_sets = JSON.parse(lineages_json)

{ data_fetched: true, primer_sets:, default_tracks:, lineage_sets:,
default_lineage: }
Expand Down
1 change: 1 addition & 0 deletions app/models/organism_taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

class OrganismTaxon < ApplicationRecord
belongs_to :lineage_caller
belongs_to :organism
end
5 changes: 5 additions & 0 deletions test/fixtures/lineage_callers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default:
name: "default"
version_specifiers: ""
pending_version_specifiers: ""
script_name: "default"
6 changes: 6 additions & 0 deletions test/fixtures/organism_taxa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sars_cov_2_taxon:
name: "SARS-CoV-2"
organism: sars_cov_2
reference_accession: "NC_045512.2"
ncbi_taxon_id: 2697049
lineage_caller: default
5 changes: 3 additions & 2 deletions test/fixtures/organisms.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sars_cov_2:
name: SARS-CoV-2
ncbi_taxon_id: 1
name: "SARS-CoV-2"
slug: "sars-cov-2"
public: true
44 changes: 43 additions & 1 deletion test/models/organism_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,49 @@
require 'test_helper'

class OrganismTest < ActiveSupport::TestCase
setup do
@organism = Organism.new
end

test 'instantiation' do
assert_not_nil Organism.new
assert_not_nil @organism
end

test 'process_lineage_variants_data returns correct data structure on valid JSON' do
tracks_json = '{"track1": "value1"}'
defaults_json = '{"tracks": "default_tracks", "lineage": "default_lineage"}'
lineages_json = '{"lineage1": "value1"}'

expected = {
data_fetched: true,
primer_sets: {'track1' => 'value1'},
default_tracks: 'default_tracks',
lineage_sets: {'lineage1' => 'value1'},
default_lineage: 'default_lineage'
}

assert_equal expected, @organism.process_lineage_variants_data(tracks_json, defaults_json, lineages_json)
end

test 'process_lineage_variants_data handles JSON parsing errors gracefully' do
invalid_json = 'invalid_json'

assert_raises(JSON::ParserError) do
@organism.process_lineage_variants_data(invalid_json, invalid_json, invalid_json)
end
end

test 'process_lineage_variants_data returns empty hashes for empty JSON strings' do
empty_json = '{}'

expected = {
data_fetched: true,
primer_sets: {},
default_tracks: nil,
lineage_sets: {},
default_lineage: nil
}

assert_equal expected, @organism.process_lineage_variants_data(empty_json, empty_json, empty_json)
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require_relative '../config/environment'
require 'rails/test_help'

require 'mocha/minitest'

module ActiveSupport
class TestCase
# Run tests in parallel with specified workers
Expand Down

0 comments on commit 166bb41

Please sign in to comment.