Skip to content

Commit

Permalink
File import: Empty column fix (#813)
Browse files Browse the repository at this point in the history
* fix for remiving empty headers

* adding tests

* fixing typo

* fixing flaky test
  • Loading branch information
ksierks authored Oct 15, 2024
1 parent 633ca84 commit 6f4d62a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/services/samples/metadata/file_import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def validate_file
Roo::Spreadsheet.open(@file)
end

@headers = @spreadsheet.row(1)
@headers = @spreadsheet.row(1).compact

validate_file_headers

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/files/metadata/contains_empty_columns.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sample_name,metadatafield1,,,metadatafield3
Project 1 Sample 1,10,,,30
Project 1 Sample 2,15,,,35
3 changes: 3 additions & 0 deletions test/fixtures/files/metadata/contains_empty_header.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sample_name,metadatafield1,,metadatafield3
Project 1 Sample 1,10,20,30
Project 1 Sample 2,15,25,35
30 changes: 30 additions & 0 deletions test/services/samples/metadata/file_import_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,36 @@ def setup
I18n.t('services.samples.metadata.import_file.missing_metadata_row'))
end

test 'import sample metadata with an empty header' do
assert_equal({}, @sample1.metadata)
assert_equal({}, @sample2.metadata)
params = { file: File.new('test/fixtures/files/metadata/contains_empty_header.csv', 'r'),
sample_id_column: 'sample_name' }
response = Samples::Metadata::FileImportService.new(@project.namespace, @john_doe,
params).execute
assert_equal({ @sample1.name => { added: %w[metadatafield1 metadatafield3],
updated: [], deleted: [], not_updated: [], unchanged: [] },
@sample2.name => { added: %w[metadatafield1 metadatafield3],
updated: [], deleted: [], not_updated: [], unchanged: [] } }, response)
assert_equal({ 'metadatafield1' => '10', 'metadatafield3' => '30' }, @sample1.reload.metadata)
assert_equal({ 'metadatafield1' => '15', 'metadatafield3' => '35' }, @sample2.reload.metadata)
end

test 'import sample metadata with multiple empty columns' do
assert_equal({}, @sample1.metadata)
assert_equal({}, @sample2.metadata)
params = { file: File.new('test/fixtures/files/metadata/contains_empty_columns.csv', 'r'),
sample_id_column: 'sample_name' }
response = Samples::Metadata::FileImportService.new(@project.namespace, @john_doe,
params).execute
assert_equal({ @sample1.name => { added: %w[metadatafield1 metadatafield3],
updated: [], deleted: [], not_updated: [], unchanged: [] },
@sample2.name => { added: %w[metadatafield1 metadatafield3],
updated: [], deleted: [], not_updated: [], unchanged: [] } }, response)
assert_equal({ 'metadatafield1' => '10', 'metadatafield3' => '30' }, @sample1.reload.metadata)
assert_equal({ 'metadatafield1' => '15', 'metadatafield3' => '35' }, @sample2.reload.metadata)
end

test 'import sample metadata with empty values set to true' do
sample32 = samples(:sample32)
project29 = projects(:project29)
Expand Down
10 changes: 10 additions & 0 deletions test/system/projects/samples_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,18 @@ class SamplesTest < ApplicationSystemTestCase

Project.reset_counters(project2.id, :samples_count)
visit namespace_project_samples_url(namespace1, project2)

assert_text strip_tags(I18n.t(:'viral.pagy.limit_component.summary', from: 1, to: 20, count: 220,
locale: @user.locale))

click_button I18n.t(:'projects.samples.index.select_all_button')

within 'tfoot' do
sample_counts = all('strong')
total_samples = sample_counts[0].text.to_i
selected_samples = sample_counts[1].text.to_i
assert selected_samples <= total_samples
end
end

test 'empty state of transfer sample project selection' do
Expand Down

0 comments on commit 6f4d62a

Please sign in to comment.