Skip to content

Commit

Permalink
WIP Allow to freely override PLs via file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kammerer committed Feb 27, 2018
1 parent e2ced0f commit 84cd1ff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
47 changes: 31 additions & 16 deletions app/services/submission/plant_line_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ def call
@upload.log "Detected wrong file format - please make sure the file is not encoded (e.g. you are uploading an xls, instead of a text file)."
end

if @upload.errors.empty?
@upload.submission.content.append(:step03,
plant_line_list: @plant_line_names,
new_plant_lines: @new_plant_lines,
new_plant_varieties: @new_plant_varieties,
new_plant_accessions: @new_plant_accessions)
@upload.submission.content.update(:step03, uploaded_plant_lines: @plant_line_names)
@upload.submission.save!
@upload.submission.content.save!
end
update_submission_content if @upload.errors.empty?
end

private
Expand Down Expand Up @@ -79,6 +70,36 @@ def parse_plant_lines
@upload.log "Detected #{@new_plant_varieties.size} plant line(s) of new (i.e. not existing in BIP) plant variety(ies)."
end

def update_submission_content
remove_overriden_content
append_uploaded_content

@upload.submission.save!
end

def remove_overriden_content
current_new_plant_lines = @upload.submission.content.new_plant_lines || []
current_new_plant_varieties = @upload.submission.content.new_plant_varieties || {}
current_new_plant_accessions = @upload.submission.content.new_plant_accessions || {}

overriden_new_plant_lines =
current_new_plant_lines.select { |npl| @plant_line_names.include?(npl.fetch("plant_line_name")) }

@upload.submission.content.update(:step03,
new_plant_lines: current_new_plant_lines - overriden_new_plant_lines,
new_plant_varieties: current_new_plant_varieties.except(*@plant_line_names),
new_plant_accessions: current_new_plant_accessions.except(*@plant_line_names))
end

def append_uploaded_content
@upload.submission.content.append(:step03,
plant_line_list: @plant_line_names,
new_plant_lines: @new_plant_lines,
new_plant_varieties: @new_plant_varieties,
new_plant_accessions: @new_plant_accessions)
@upload.submission.content.update(:step03, uploaded_plant_lines: @plant_line_names)
end

def csv
@csv ||= CSV.new(input)
end
Expand All @@ -97,8 +118,6 @@ def correct_input?(row)

if reused_plant_line?(plant_line_name)
@upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already defined in the uploaded file."
elsif current_new_plant_lines.include?(plant_line_name)
@upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already defined. Please clear the 'Plant line list' field before re-uploading a CSV file."
elsif existing_plant_line?(plant_line_name) && !existing_plant_line_match?(pl_attrs, pv_attrs)
@upload.log "Ignored row for #{plant_line_name} since a plant line with that name is already present in BIP "\
"but uploaded data does not match existing record."
Expand Down Expand Up @@ -192,10 +211,6 @@ def current_plant_lines
@current_plant_lines ||= @upload.submission.content.plant_line_list || []
end

def current_new_plant_lines
@current_new_plant_lines ||= (@upload.submission.content.new_plant_lines || []).map { |pl| pl["plant_line_name"] }
end

def header_columns
[
"Species",
Expand Down
57 changes: 32 additions & 25 deletions spec/services/submission/plant_line_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,52 +140,59 @@
end

context "override of previously defined content" do
before { allow(subject).to receive(:parse_header) }

it 'allows override of existing plant line with existing plant line' do
pl = create(:plant_line)

submission.content.update(:step03, plant_line_list: pl.plant_line_name)
submission.content.update(:step03, plant_line_list: [pl.plant_line_name])
submission.save!

input_is "#{pl.taxonomy_term.name},,,#{pl.plant_line_name}"
subject.send(:parse_plant_lines)
expect(subject.plant_line_names).to eq [pl.plant_line_name]
expect(subject.new_plant_lines).to eq []
subject.call
expect(submission.reload.content.plant_line_list).to eq [pl.plant_line_name]
expect(submission.content.new_plant_varieties).to be_blank
expect(submission.content.new_plant_accessions).to be_blank
end

it 'blocks override of new plant line with new plant line' do
submission.content.update(:step03, plant_line_list: ["new-pl"],
new_plant_lines: [{ plant_line_name: "new-pl",
plant_variety_name: "new-pv",
taxonomy_term: "Brassica napus" }])
it 'allows override of new plant line with new plant line' do
submission.content.update(:step03,
plant_line_list: ["new-pl"],
new_plant_lines: [{ plant_line_name: "new-pl", plant_variety_name: "new-pv", taxonomy_term: "Brassica napus" }],
new_plant_varieties: { "new-pl" => { "plant_variety_name" => "new-pv" } },
new_plant_accessions: { "new-pl" => { "plant_accession" => "new-pa" } }
)
submission.save!

input_is "Brassica napus,,,new-pl"
subject.send(:parse_plant_lines)
input_is "Brassica napus,new-pv-2,,new-pl"
subject.call
expect(upload.errors).to be_blank
expect(submission.reload.content.plant_line_list).to eq ["new-pl"]
expect(submission.content.new_plant_lines).
to eq [{ "plant_line_name" => "new-pl", "plant_variety_name" => "new-pv", "taxonomy_term" => "Brassica napus" }]
to eq [{ "plant_line_name" => "new-pl", "plant_variety_name" => "new-pv-2", "taxonomy_term" => "Brassica napus" }]

expect(upload.logs).
to include "Ignored row for new-pl since a plant line with that name is already defined. "\
"Please clear the 'Plant line list' field before re-uploading a CSV file."
expect(submission.content.new_plant_varieties).to eq("new-pl" => { "plant_variety_name" => "new-pv-2" })
expect(submission.content.new_plant_accessions).to be_blank
end

it 'blocks override of new plant line by existing plant line' do
it 'allows override of new plant line by existing plant line' do
pl = create(:plant_line)

submission.content.update(:step03, plant_line_list: [pl.plant_line_name],
new_plant_lines: [{ plant_line_name: pl.plant_line_name,
taxonomy_term: "Brassica napus" }])
submission.content.update(:step03,
plant_line_list: [pl.plant_line_name],
new_plant_lines: [{ plant_line_name: pl.plant_line_name, plant_variety_name: "new-pv", taxonomy_term: "Brassica napus" }],
new_plant_varieties: { pl.plant_line_name => { "plant_variety_name" => "new-pv" } },
new_plant_accessions: { pl.plant_line_name => { "plant_accession" => "new-pa" } }
)
submission.save!

input_is "#{pl.taxonomy_term.name},,,#{pl.plant_line_name}"
subject.send(:parse_plant_lines)
subject.call
expect(upload.errors).to be_blank
expect(submission.reload.content.plant_line_list).to eq [pl.plant_line_name]
expect(submission.content.new_plant_lines).to eq [{ "plant_line_name" => pl.plant_line_name,
"taxonomy_term" => "Brassica napus" }]
expect(upload.logs).
to include "Ignored row for #{pl.plant_line_name} since a plant line with that name is already defined. "\
"Please clear the 'Plant line list' field before re-uploading a CSV file."
expect(submission.content.new_plant_lines).to be_blank
expect(submission.content.new_plant_varieties).to be_blank
expect(submission.content.new_plant_accessions).to be_blank
end
end

Expand Down

0 comments on commit 84cd1ff

Please sign in to comment.