diff --git a/test/unit/fair_data_station/fair_data_station_writer_test.rb b/test/unit/fair_data_station/fair_data_station_writer_test.rb index deca179d80..0084bee6b3 100644 --- a/test/unit/fair_data_station/fair_data_station_writer_test.rb +++ b/test/unit/fair_data_station/fair_data_station_writer_test.rb @@ -458,12 +458,12 @@ class FairDataStationWriterTest < ActiveSupport::TestCase assert_equal 'test study 1', study.title expected = HashWithIndifferentAccess.new({ - 'Experimental site name':'manchester test site', - 'study date details': { - 'End date of Study': '2024-08-08', - 'Start date of Study': '2024-08-01' - } - }) + 'Experimental site name': 'manchester test site', + 'study date details': { + 'End date of Study': '2024-08-08', + 'Start date of Study': '2024-08-01' + } + }) assert_equal expected, study.extended_metadata.data @@ -472,7 +472,7 @@ class FairDataStationWriterTest < ActiveSupport::TestCase assert_equal 'test study 2', study.title expected = HashWithIndifferentAccess.new({ - 'Experimental site name':'manchester test site', + 'Experimental site name': 'manchester test site', 'study date details': { 'End date of Study': '2024-08-18', 'Start date of Study': '2024-08-10' @@ -487,7 +487,7 @@ class FairDataStationWriterTest < ActiveSupport::TestCase assert_equal 'test obs unit 2', obs_unit.title expected = HashWithIndifferentAccess.new({ - 'Gender':'female', + 'Gender': 'female', 'obs_unit_birth_details': { 'Birth weight': '1235g', 'Date of birth': '2020-01-11' @@ -522,7 +522,7 @@ class FairDataStationWriterTest < ActiveSupport::TestCase assert_equal 'test study 1', study.title expected = HashWithIndifferentAccess.new({ - 'Experimental site name':'manchester test site', + 'Experimental site name': 'manchester test site', 'child': { 'End date of Study': '2024-08-08', 'grandchild': { @@ -534,6 +534,103 @@ class FairDataStationWriterTest < ActiveSupport::TestCase assert_equal expected, study.extended_metadata.data end + test 'update isa with nested metadata' do + FactoryBot.create(:fairdata_test_case_investigation_extended_metadata) + study_emt = FactoryBot.create(:fairdata_test_case_nested_study_extended_metadata) + obs_unit_emt = FactoryBot.create(:fairdata_test_case_nested_obsv_unit_extended_metadata) + FactoryBot.create(:fairdata_test_case_assay_extended_metadata) + FactoryBot.create(:fairdatastation_test_case_sample_type) + FactoryBot.create(:experimental_assay_class) + path = "#{Rails.root}/test/fixtures/files/fair_data_station/seek-fair-data-station-test-case.ttl" + inv = Seek::FairDataStation::Reader.new.parse_graph(path).first + contributor = FactoryBot.create(:person) + projects = contributor.projects + policy = Policy.default + + investigation = Seek::FairDataStation::Writer.new.construct_isa(inv, contributor, projects, policy) + assert investigation.valid? + + # setup + assert_difference('ExtendedMetadata.count', 12) do + User.with_current_user(contributor.user) do + investigation.save! + end + end + + path = "#{Rails.root}/test/fixtures/files/fair_data_station/seek-fair-data-station-modified-test-case.ttl" + inv = Seek::FairDataStation::Reader.new.parse_graph(path).first + + # on save check, 0 investigation created, 1 study created, 1 obs unit, 1 sample, 1 assay, 3 data file, 3 extended metadata + + 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('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 + User.with_current_user(contributor.user) do + investigation = Seek::FairDataStation::Writer.new.update_isa(investigation, inv, contributor, + projects, policy) + investigation.save! + end + end + end + end + end + end + end + end + end + end + + investigation.reload + + assert_equal 3, investigation.studies.count + study = investigation.studies.where(external_identifier: 'seek-test-study-1').first + expected = HashWithIndifferentAccess.new({ + 'Experimental site name': 'manchester test site - changed', + 'study date details': { + 'End date of Study': '2024-08-08', + 'Start date of Study': '2024-08-01' + } + }) + assert_equal expected, study.extended_metadata.data + + study = investigation.studies.where(external_identifier: 'seek-test-study-3').first + expected = HashWithIndifferentAccess.new({ + 'Experimental site name': 'birmingham-test-site', + 'study date details': { + 'End date of Study': '2024-09-13', + 'Start date of Study': '2024-09-12' + } + }) + assert_equal expected, study.extended_metadata.data + + obs_unit = ObservationUnit.where(external_identifier: 'seek-test-obs-unit-1').first + expected = HashWithIndifferentAccess.new({ + 'Gender': 'female', + 'obs_unit_birth_details': { + 'Birth weight': '1234g', + 'Date of birth': '2020-01-10' + } + }) + assert_equal expected, obs_unit.extended_metadata.data + obs_unit = ObservationUnit.where(external_identifier: 'seek-test-obs-unit-4').first + expected = HashWithIndifferentAccess.new({ + 'Gender': 'female', + 'obs_unit_birth_details': { + 'Birth weight': '1005g', + 'Date of birth': '2020-02-12' + } + }) + assert_equal expected, obs_unit.extended_metadata.data + + end + private def setup_test_case_investigation