Skip to content

Commit

Permalink
Fixed removal of empty prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gehrke committed Nov 11, 2024
1 parent e2c2936 commit b4f92ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/ontology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl PyIndexedOntology {
) -> PyResult<Option<String>> {
let iri: String = self.iri(py, iri, iri_is_absolute)?.into();
let mapping = self.mapping.borrow_mut(py);

let res = mapping.0.shrink_iri(iri.as_str());

if let Ok(curie) = res {
Expand Down Expand Up @@ -1072,7 +1073,7 @@ impl PyIndexedOntology {
amo.insert(component.clone());
}

let mapping = self.mapping.borrow_mut(py);
let mapping = self.mapping.borrow(py);

let result = match serialization {
ResourceType::OFN => {
Expand Down
9 changes: 9 additions & 0 deletions src/prefix_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ impl PrefixMapping {
/// Remove a prefix from the mapping.
pub fn remove_prefix(&mut self, prefix: &str) {
self.0.remove_prefix(prefix);

if prefix == "" {
let mut new_mapping = curie::PrefixMapping::default();
for (p, v) in self.0.mappings() {
new_mapping.add_prefix(p, v).expect("Cannot happen since self.0 contains only valid prefix mappings");
}

self.0 = new_mapping;
}
}

/// expand_curie(self, curie: str) -> str
Expand Down
21 changes: 11 additions & 10 deletions test/test_id.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

import pyhornedowl
from test_base import simple_ontology


Expand All @@ -15,29 +16,29 @@ def test_id_from_iri_empty(self):
self.assertNotEqual(expected, actual)

def test_id_from_absolute(self):
o = simple_ontology()
o.prefix_mapping.add_prefix("ex", "https://example.com/")
o = pyhornedowl.PyIndexedOntology()
o.prefix_mapping.add_prefix("EX", "https://example.com/")

expected = ":A"
expected = "EX:A"
actual = o.get_id_for_iri("https://example.com/A")

self.assertEqual(expected, actual)

def test_id_from_curie_empty_prefix(self):
o = simple_ontology()
o.prefix_mapping.add_prefix("ex", "https://example.com/")
o = pyhornedowl.PyIndexedOntology()
o.prefix_mapping.add_prefix("", "https://example.com/")

expected = ":A"
expected = "A"
actual = o.get_id_for_iri(":A")

self.assertEqual(expected, actual)

def test_id_from_curie_defined_prefix(self):
o = simple_ontology()
o.prefix_mapping.add_prefix("ex", "https://example.com/")
o = pyhornedowl.PyIndexedOntology()
o.prefix_mapping.add_prefix("EX", "https://example.com/")

expected = ":A"
actual = o.get_id_for_iri("ex:A")
expected = "EX:A"
actual = o.get_id_for_iri("EX:A")

self.assertEqual(expected, actual)

Expand Down

0 comments on commit b4f92ec

Please sign in to comment.