Skip to content

Commit

Permalink
Add more tests for the JSON format.
Browse files Browse the repository at this point in the history
Add a new test for the JSONReader, to check that non-string-typed slots
(e.g. double-typed and enum-typed slots) are parsed correctly.

Also add some roundtrip tests for both the JSONReader and the
JSONWriter, including some TSV-to-JSON-to-TSV roundtripping tests.
  • Loading branch information
gouttegd committed Jul 27, 2024
1 parent bbb706e commit dde8ea2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public MappingSet convertMappingSet(Map<String, Object> rawMap) throws SSSOMForm
// Process the bulk of the metadata slots
Map<String, ExtensionValue> extensionSlots = new HashMap<String, ExtensionValue>();
for ( String key : rawMap.keySet() ) {
if ( key.equals("mappings") ) { // To be processed separately
continue;
}
if ( setSlotMaps.containsKey(key) ) {
setSlotValue(setSlotMaps.get(key), ms, rawMap.get(key));
} else {
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/org/incenp/obofoundry/sssom/JSONReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.time.LocalDate;

import org.incenp.obofoundry.sssom.model.EntityType;
import org.incenp.obofoundry.sssom.model.Mapping;
import org.incenp.obofoundry.sssom.model.MappingSet;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -160,4 +161,13 @@ void testSlotPropagation() throws IOException, SSSOMFormatException {
// Second mapping has its own mapping tool value
Assertions.assertEquals("bar mapper", ms.getMappings().get(1).getMappingTool());
}

@Test
void testParsingNonStringValues() throws IOException, SSSOMFormatException {
JSONReader reader = new JSONReader("src/test/resources/sets/test-non-string-values.sssom.json");
MappingSet ms = reader.read();

Assertions.assertEquals(EntityType.OWL_CLASS, ms.getMappings().get(0).getSubjectType());
Assertions.assertEquals(0.7, ms.getMappings().get(0).getConfidence());
}
}
48 changes: 48 additions & 0 deletions core/src/test/java/org/incenp/obofoundry/sssom/JSONWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,54 @@ void testEscapingJSON() throws IOException, SSSOMFormatException {
assertWrittenAsExpected(ms, "test-escaping-json", null, null);
}

@Test
void testBasicRoundtrip() throws IOException, SSSOMFormatException {
JSONReader reader = new JSONReader("src/test/resources/sets/exo2c.sssom.json");
MappingSet ms = reader.read();

assertWrittenAsExpected(ms, "exo2c", "test-basic-roundtrip", null);
}

@Test
void testTSVAndJSONRoundtrips() throws IOException, SSSOMFormatException {
TSVtoJSONtoTSVRoundtrip("exo2c", ExtraMetadataPolicy.NONE);
TSVtoJSONtoTSVRoundtrip("test-extensions-defined", ExtraMetadataPolicy.DEFINED);
}

private void TSVtoJSONtoTSVRoundtrip(String tsvFilename, ExtraMetadataPolicy policy)
throws IOException, SSSOMFormatException {
File origTSV = new File("src/test/resources/sets/" + tsvFilename + ".sssom.tsv");
if ( !origTSV.exists() ) {
origTSV = new File("src/test/resources/output/" + tsvFilename + ".sssom.tsv");
}

TSVReader tsvReader = new TSVReader(origTSV);
tsvReader.setExtraMetadataPolicy(policy);
MappingSet ms = tsvReader.read();

File json = new File("src/test/resources/output/" + tsvFilename + ".sssom.json.out");
JSONWriter jsonWriter = new JSONWriter(json);
jsonWriter.setShortenIRIs(true);
jsonWriter.setExtraMetadataPolicy(policy);
jsonWriter.write(ms);

JSONReader jsonReader = new JSONReader(json);
jsonReader.setExtraMetadataPolicy(policy);
ms = jsonReader.read();

File newTSV = new File("src/test/resources/output/" + tsvFilename + ".sssom.tsv.out");
TSVWriter tsvWriter = new TSVWriter(newTSV);
tsvWriter.setExtraMetadataPolicy(policy);
tsvWriter.write(ms);

boolean same = FileUtils.contentEquals(origTSV, newTSV);
Assertions.assertTrue(same);
if ( same ) {
json.delete();
newTSV.delete();
}
}

/*
* Checks that a mapping set is written exactly as we expect. This method will
* write the provided set to a temporary file and compares the written file with
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/resources/sets/test-non-string-values.sssom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"curie_map": {
"COMENT": "https://example.com/entities/",
"ORGENT": "https://example.org/entities/"
},
"mapping_set_id": "https://example.org/sets/exo2c",
"mapping_set_title": "O2C set",
"mappings": [
{
"subject_id": "ORGENT:0001",
"subject_label": "alice",
"predicate_id": "skos:closeMatch",
"object_id": "COMENT:0011",
"object_label": "alpha",
"mapping_justification": "semapv:ManualMappingCuration",
"subject_type": "owl class",
"confidence": 0.7
}
]
}

0 comments on commit dde8ea2

Please sign in to comment.