Skip to content

Commit

Permalink
update handling datasets, and when they move #1981
Browse files Browse the repository at this point in the history
currently an issue with one being saved unexpectedly though
  • Loading branch information
stuzart committed Sep 17, 2024
1 parent 8577f35 commit ee3b643
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
21 changes: 19 additions & 2 deletions lib/seek/fair_data_station/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def construct_isa(datastation_inv, contributor, projects, policy)
end

def update_isa(investigation, datastation_inv, contributor, projects, policy)
reset_data_file_cache
preload_data_file_cache(investigation.related_data_files)
update_entity(investigation, datastation_inv, contributor, projects, policy)
datastation_inv.studies.each do |datastation_study|
study = update_or_build_study(datastation_study, contributor, projects, policy, investigation)
Expand All @@ -52,6 +52,13 @@ def reset_data_file_cache
@data_file_cache = {}
end

def preload_data_file_cache(data_files)
reset_data_file_cache
data_files.each do |data_file|
@data_file_cache[data_file.external_identifier] = data_file
end
end

def build_assay(datastation_assay, contributor, projects, policy, sample, study)
samples = []
samples << sample if sample.valid?
Expand Down Expand Up @@ -131,6 +138,11 @@ def update_or_build_observation_unit(datastation_observation_unit, contributor,
observation_unit = ::ObservationUnit.by_external_identifier(datastation_observation_unit.external_id, projects)
if observation_unit
update_entity(observation_unit, datastation_observation_unit, contributor, projects, policy)
observation_unit.observation_unit_assets.clear
datastation_observation_unit.datasets.each do |datastation_dataset|
df = build_data_file(contributor, datastation_dataset, projects, policy)
observation_unit.observation_unit_assets.build(asset: df)
end
study.observation_units << observation_unit
else
observation_unit = build_observation_unit(datastation_observation_unit, contributor, projects, policy, study)
Expand All @@ -153,6 +165,11 @@ def update_or_build_assay(datastation_assay, contributor, projects, policy, samp
assay = ::Assay.by_external_identifier(datastation_assay.external_id, projects)
if assay
update_entity(assay, datastation_assay, contributor, projects, policy)
assay.assay_assets.clear
datastation_assay.datasets.each do |datastation_dataset|
df = build_data_file(contributor, datastation_dataset, projects, policy)
assay.assay_assets.build(asset: df)
end
study.assays << assay
else
assay = build_assay(datastation_assay, contributor, projects, policy, sample, study)
Expand Down Expand Up @@ -211,7 +228,7 @@ def detect_sample_type(datastation_sample)
end

def build_data_file(contributor, datastation_dataset, projects, policy)
@data_file_cache[datastation_dataset.identifier] ||= begin
@data_file_cache[datastation_dataset.external_id] ||= begin
blob = ContentBlob.new(url: datastation_dataset.content_url.to_s,
original_filename: datastation_dataset.identifier, external_link: true, is_webpage: true, content_type: 'application/octet-stream')
data_file_attributes = datastation_dataset.seek_attributes.merge({
Expand Down
23 changes: 17 additions & 6 deletions test/unit/fair_data_station/fair_data_station_writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,26 @@ class FairDataStationWriterTest < ActiveSupport::TestCase
path = "#{Rails.root}/test/fixtures/files/fairdatastation/seek-fair-data-station-modified-test-case.ttl"
inv = Seek::FairDataStation::Reader.new.parse_graph(path).first

investigation = Seek::FairDataStation::Writer.new.update_isa(investigation, inv, contributor, projects, policy)
#assert_no_difference('DataFile.count') do
investigation = Seek::FairDataStation::Writer.new.update_isa(investigation, inv, contributor, projects, policy)
#end

# on save check, 0 investigation created, 1 study created, 1 obs unit, 1 sample, 1 assay, 3 data file, 3 extended metadata

assert_difference("Investigation.count", 0) do
assert_no_difference("Investigation.count") do
assert_difference("Study.count", 1) do
assert_difference("ObservationUnit.count", 1) do
assert_difference("Sample.count", 1) do
assert_difference("Assay.count", 1) do
# assert_difference("DataFile.count", 3) do
assert_difference("ExtendedMetadata.count", 3) do
investigation.save!
#assert_difference("DataFile.count", 3) do
assert_difference("ObservationUnitAsset.count", 1) do
assert_difference("AssayAsset.count", 2) do # 1 for new df, the other is for the sample
assert_difference("ExtendedMetadata.count", 3) do
investigation.save!
end
end
end
# end
#end
end
end
end
Expand Down Expand Up @@ -282,12 +288,14 @@ class FairDataStationWriterTest < ActiveSupport::TestCase
assert_equal 1, study.observation_units.count
obs_unit = ObservationUnit.where(external_identifier: 'seek-test-obs-unit-1').first
assert_equal 'female', obs_unit.extended_metadata.get_attribute_value('Gender')
assert_equal ['test-file-6.csv'], obs_unit.data_files.collect(&:external_identifier)
obs_unit = ObservationUnit.where(external_identifier: 'seek-test-obs-unit-3').first
assert_equal 'test obs unit 3 - changed', obs_unit.title
obs_unit = ObservationUnit.where(external_identifier: 'seek-test-obs-unit-4').first
assert_equal 'testing testing testing testing testing testing testing testing testing testing obs unit 4', obs_unit.description
assert_equal '1005g', obs_unit.extended_metadata.get_attribute_value('Birth weight')
assert_equal 'seek-test-study-3', obs_unit.study.external_identifier
assert_equal ['test-file-7.csv'], obs_unit.data_files.collect(&:external_identifier)


# check:
Expand Down Expand Up @@ -318,13 +326,15 @@ class FairDataStationWriterTest < ActiveSupport::TestCase
# seek-test-assay-7 created, along with new test-file-8.csv data file
assay = Assay.where(external_identifier: 'seek-test-assay-1').first
assert_equal 'testing testing testing testing testing testing testing testing testing testing assay 1 - changed', assay.description
assert_equal ['test-file-6.csv'], assay.data_files.collect(&:external_identifier)
assay = Assay.where(external_identifier: 'seek-test-assay-6').first
assert_equal 'test facility - changed', assay.extended_metadata.get_attribute_value('Facility')
assay = Assay.where(external_identifier: 'seek-test-assay-7').first
assert_equal 'testing testing testing testing testing testing testing testing testing testing assay 7', assay.description
assert_equal 'new test facility', assay.extended_metadata.get_attribute_value('Facility')
assert_equal 1, assay.samples.count
assert_equal 'seek-test-sample-6', assay.samples.first.external_identifier
assert_equal ['test-file-8.csv'], assay.data_files.collect(&:external_identifier)

end

Expand All @@ -336,6 +346,7 @@ def setup_test_case_investigation
FactoryBot.create(:fairdata_test_case_obsv_unit_extended_metadata)
FactoryBot.create(:fairdata_test_case_assay_extended_metadata)
FactoryBot.create(:fairdatastation_test_case_sample_type)
FactoryBot.create(:experimental_assay_class)

contributor = FactoryBot.create(:person)
project = contributor.projects.first
Expand Down

0 comments on commit ee3b643

Please sign in to comment.