From 721891857858c05155827fafc258b84a9d2a3c0a Mon Sep 17 00:00:00 2001 From: whomingbird Date: Tue, 17 Sep 2024 12:39:11 +0200 Subject: [PATCH] add EmtExtractorTest for invalid json files --- .../invalid_emt_with_wrong_id.json | 27 +++++++++++++++ .../invalid_emt_with_wrong_type.json | 18 ++++++++++ .../extended_metadatas/emt_extractor_test.rb | 33 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_id.json create mode 100644 test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_type.json diff --git a/test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_id.json b/test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_id.json new file mode 100644 index 0000000000..7ad2b173d2 --- /dev/null +++ b/test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_id.json @@ -0,0 +1,27 @@ +{ + "title": "study metadata demo", + "supported_type": "Study", + "enabled": true, + "attributes": [ + { + "title": "resource_type_general", + "label": "Resource type general", + "description": "resource type general", + "type": "Controlled Vocabulary", + "required": true, + "pos": 1, + "ID": -1 + }, + { + "title": "resource_study_country", + "label": "Study country", + "description": "study country", + "type": "Controlled Vocabulary List", + "required": true, + "pos": 2, + "ID": -1 + } + ] +} + + diff --git a/test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_type.json b/test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_type.json new file mode 100644 index 0000000000..2b3be32fc4 --- /dev/null +++ b/test/fixtures/files/extended_metadata_type/invalid_emt_with_wrong_type.json @@ -0,0 +1,18 @@ +{ + "title": "person", + "supported_type": "ExtendedMetadata", + "enabled": true, + "attributes": [ + { + "title": "first_name", + "type": "String1", + "required": true + }, + { + "title": "last_name", + "label": "Last Name", + "type": "String", + "required": true + } + ] +} \ No newline at end of file diff --git a/test/unit/extended_metadatas/emt_extractor_test.rb b/test/unit/extended_metadatas/emt_extractor_test.rb index 2f5dbfdb76..5b382524cb 100644 --- a/test/unit/extended_metadatas/emt_extractor_test.rb +++ b/test/unit/extended_metadatas/emt_extractor_test.rb @@ -119,10 +119,43 @@ class EmtExtractorTest < ActiveSupport::TestCase errorfile = Rails.root.join(Seek::Config.append_filestore_path('emt_files'), 'result.error') assert File.exist?(errorfile) assert_equal '', File.read(errorfile) + end + + + test 'handles invalid json file with wrong attr type' do + invalid_emt_file = open_fixture_file('extended_metadata_type/invalid_emt_with_wrong_type.json') + + assert_no_difference('ExtendedMetadataType.count') do + Seek::ExtendedMetadataType::EMTExtractor.extract_extended_metadata_type(invalid_emt_file) + end + + errorfile = Rails.root.join(Seek::Config.append_filestore_path('emt_files'), 'result.error') + assert File.exist?(errorfile) + + + error_message = "The property '#/attributes/0/type' value \"String1\" did not match one of the following values: Date time, Date, Real number, Integer, Web link, Email address, Text, String, ChEBI, ECN, MetaNetX chemical, MetaNetX reaction, MetaNetX compartment, InChI, ENA custom date, Boolean, URI, DOI, NCBI ID, Registered Strain, Registered Data file" + assert_includes File.read(errorfile), error_message end + test 'handles invalid json file with wrong id' do + invalid_emt_file = open_fixture_file('extended_metadata_type/invalid_emt_with_wrong_id.json') + + assert_no_difference('ExtendedMetadataType.count') do + Seek::ExtendedMetadataType::EMTExtractor.extract_extended_metadata_type(invalid_emt_file) + end + + errorfile = Rails.root.join(Seek::Config.append_filestore_path('emt_files'), 'result.error') + assert File.exist?(errorfile) + + + error_message = "Couldn't find SampleControlledVocab with 'id'=-1" + assert_includes File.read(errorfile), error_message + end +end + +