Skip to content

Commit

Permalink
Add tests for the kgcl:mint command.
Browse files Browse the repository at this point in the history
Adds a text fixture for the `kgcl:mint` command. We test re-allocating
IDs on a dummy ontology that contains only one entity with a temporary
identifier, so that we don't run into reproducibility issue because we
can't control the order in which entities are iterated over.
  • Loading branch information
gouttegd committed Mar 27, 2024
1 parent 26df19a commit 7f1dde7
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.geneontology.owl.differ.Differ;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.obolibrary.robot.CommandManager;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

public class ApplyCommandTest {

Expand Down Expand Up @@ -120,65 +115,8 @@ void testCreateNewOntology() {
"create relation EX:0101 'object property 1'", "--kgcl", "create edge EX:0001 EX:0101 EX:0002");
}

/*
* Try running a KGCL-Apply command and check that the output ontology matches
* what we expect.
*
* inputFile: the input ontology (filename relative to
* {,../core}/src/test/resources). outputFile: expected output file (likewise);
* if null, ignore the output.
*/
private void runCommand(String inputFile, String outputFile, String... extra) {
File input = new File("/src/test/resources/" + inputFile);
if ( !input.exists() ) {
input = new File("../core/src/test/resources/" + inputFile);
}

File expectedOutput = null;
File actualOutput = null;
if ( outputFile != null ) {
expectedOutput = new File("src/test/resources/" + outputFile);
if ( !expectedOutput.exists() ) {
expectedOutput = new File("../core/src/test/resources/" + outputFile);
}
actualOutput = new File("src/test/resources/output-" + outputFile);
} else {
actualOutput = new File("dont-care.ofn");
}

String[] args = new String[1 + 2 + 2 + extra.length];
args[0] = "kgcl-apply";
args[1] = "--input";
args[2] = input.getPath();
args[3] = "--output";
args[4] = actualOutput.getPath();
for ( int i = 0; i < extra.length; i++ ) {
args[i + 5] = extra[i];
}

CommandManager robot = new CommandManager();
robot.addCommand("kgcl-apply", new ApplyCommand());
robot.main(args);

if ( outputFile != null ) {
compareOntologies(expectedOutput, actualOutput);
} else if ( actualOutput.exists() ) {
actualOutput.delete();
}
}

private void compareOntologies(File expected, File actual) {
try {
OWLOntology expectedOntology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(expected);
OWLOntology actualOntology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(actual);
Differ.BasicDiff diff = Differ.diff(expectedOntology, actualOntology);
Assertions.assertTrue(diff.isEmpty());
if ( diff.isEmpty() ) {
actual.delete();
}
} catch ( OWLOntologyCreationException e ) {
Assertions.fail(e);
}
TestUtils.runCommand("apply", inputFile, outputFile, extra);
}

private void checkOutput(String expectedFile, String actualFile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* KGCL-Java - KGCL library for Java
* Copyright © 2024 Damien Goutte-Gattat
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Gnu General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.incenp.obofoundry.kgcl.robot;

import org.junit.jupiter.api.Test;

public class MintCommandTest {

private final static String idRangeFile = "../core/src/test/resources/idranges.owl";

@Test
void testReallocateWithExplicitRange() {
runCommand("temporary-ids.ofn", "reallocated-ids.ofn", "--minted-id-prefix", "https://example.org/",
"--pad-width", "4", "--min-id", "501");
}

@Test
void testReallocateWithIDRangePolicy() {
runCommand("temporary-ids.ofn", "reallocated-ids.ofn", "--id-range-file", idRangeFile, "--id-range-name",
"Bob");
}

@Test
void testKeepDeprecated() {
runCommand("temporary-ids.ofn", "reallocated-ids-keep-deprecated.ofn", "--id-range-file", idRangeFile,
"--id-range-name", "Bob", "--keep-deprecated");
}

@Test
void testMintedFrom() {
runCommand("temporary-ids.ofn", "reallocated-ids-minted-from.ofn", "--id-range-file", idRangeFile,
"--id-range-name", "Bob", "--minted-from-property", "https://example.org/properties#mintedFrom");
}

private void runCommand(String inputFile, String outputFile, String... extra) {
TestUtils.runCommand("mint", inputFile, outputFile, extra);
}
}
103 changes: 103 additions & 0 deletions robot/src/test/java/org/incenp/obofoundry/kgcl/robot/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* KGCL-Java - KGCL library for Java
* Copyright © 2024 Damien Goutte-Gattat
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Gnu General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.incenp.obofoundry.kgcl.robot;

import java.io.File;

import org.geneontology.owl.differ.Differ;
import org.junit.jupiter.api.Assertions;
import org.obolibrary.robot.CommandManager;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

/*
* Helper methods to test the ROBOT commands.
*/
public class TestUtils {

/*
* Tries running a command from the KGCL plugin and checks that the output
* ontology matches what we expect.
*
* @param command The command to run ("apply", "mint").
*
* @param inputFile The input ontology (as a filename relative to {@code
* {,../core}/src/test/resources}.
*
* @param outputFile The expected output file (likewise). If {@code null}, the
* output is ignored.
*
* @param extra Any extra arguments to pass to the command.
*/
public static void runCommand(String command, String inputFile, String outputFile, String... extra) {
File input = new File("src/test/resources/" + inputFile);
if ( !input.exists() ) {
input = new File("../core/src/test/resources/" + inputFile);
}

File expectedOutput = null;
File actualOutput = null;
if ( outputFile != null ) {
expectedOutput = new File("src/test/resources/" + outputFile);
if ( !expectedOutput.exists() ) {
expectedOutput = new File("../core/src/test/resources/" + outputFile);
}
actualOutput = new File("src/test/resources/output-" + outputFile);
} else {
actualOutput = new File("dont-care.ofn");
}

String[] args = new String[1 + 2 + 2 + extra.length];
args[0] = "kgcl-" + command;
args[1] = "--input";
args[2] = input.getPath();
args[3] = "--output";
args[4] = actualOutput.getPath();
for ( int i = 0; i < extra.length; i++ ) {
args[i + 5] = extra[i];
}

CommandManager robot = new CommandManager();
robot.addCommand("kgcl-apply", new ApplyCommand());
robot.addCommand("kgcl-mint", new MintCommand());
robot.main(args);

if ( outputFile != null ) {
compareOntologies(expectedOutput, actualOutput);
} else if ( actualOutput.exists() ) {
actualOutput.delete();
}
}

private static void compareOntologies(File expected, File actual) {
try {
OWLOntology expectedOntology = OWLManager.createOWLOntologyManager()
.loadOntologyFromOntologyDocument(expected);
OWLOntology actualOntology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(actual);
Differ.BasicDiff diff = Differ.diff(expectedOntology, actualOntology);
Assertions.assertTrue(diff.isEmpty());
if ( diff.isEmpty() ) {
actual.delete();
}
} catch ( OWLOntologyCreationException e ) {
Assertions.fail(e);
}
}
}
34 changes: 34 additions & 0 deletions robot/src/test/resources/reallocated-ids-keep-deprecated.ofn
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(
Declaration(Class(<http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394>))
Declaration(Class(<https://example.org/0001>))
Declaration(Class(<https://example.org/0501>))
Declaration(AnnotationProperty(<http://purl.obolibrary.org/obo/IAO_0100001>))


############################
# Classes
############################

# Class: <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394> (obsolete class 2)

AnnotationAssertion(<http://purl.obolibrary.org/obo/IAO_0100001> <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394> <https://example.org/0501>)
AnnotationAssertion(rdfs:label <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394> "obsolete class 2")
AnnotationAssertion(owl:deprecated <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394> "true"^^xsd:boolean)

# Class: <https://example.org/0001> (class 1)

AnnotationAssertion(rdfs:label <https://example.org/0001> "class 1")

# Class: <https://example.org/0501> (class 2)

AnnotationAssertion(rdfs:label <https://example.org/0501> "class 2")


)
28 changes: 28 additions & 0 deletions robot/src/test/resources/reallocated-ids-minted-from.ofn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(
Declaration(Class(<https://example.org/0001>))
Declaration(Class(<https://example.org/0501>))
Declaration(AnnotationProperty(<https://example.org/properties#mintedFrom>))


############################
# Classes
############################

# Class: <https://example.org/0001> (class 1)

AnnotationAssertion(rdfs:label <https://example.org/0001> "class 1")

# Class: <https://example.org/0501> (class 2)

AnnotationAssertion(rdfs:label <https://example.org/0501> "class 2")
AnnotationAssertion(<https://example.org/properties#mintedFrom> <https://example.org/0501> <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394>)


)
26 changes: 26 additions & 0 deletions robot/src/test/resources/reallocated-ids.ofn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(
Declaration(Class(<https://example.org/0001>))
Declaration(Class(<https://example.org/0501>))


############################
# Classes
############################

# Class: <https://example.org/0001> (class 1)

AnnotationAssertion(rdfs:label <https://example.org/0001> "class 1")

# Class: <https://example.org/0501> (class 2)

AnnotationAssertion(rdfs:label <https://example.org/0501> "class 2")


)
26 changes: 26 additions & 0 deletions robot/src/test/resources/temporary-ids.ofn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(
Declaration(Class(<https://example.org/0001>))
Declaration(Class(<http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394>))


############################
# Classes
############################

# Class <https://example.org/0001> (class 1)

AnnotationAssertion(rdfs:label <https://example.org/0001> "class 1")

# Class: <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394> (class 2)

AnnotationAssertion(rdfs:label <http://purl.obolibrary.org/temp#00819d24-4175-43d4-8405-3a1be211a394> "class 2")


)

0 comments on commit 7f1dde7

Please sign in to comment.