File: test_discovery.py

package info (click to toggle)
python-curies 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 804 kB
  • sloc: python: 3,617; makefile: 14
file content (82 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Test discovering a prefix map from a list of URIs."""

import unittest
from typing import ClassVar

import rdflib

from curies import Converter, Record
from curies.discovery import discover, discover_from_rdf
from tests.constants import SLOW


class TestDiscovery(unittest.TestCase):
    """Test discovery of URI prefixes."""

    converter: ClassVar[Converter]

    @classmethod
    def setUpClass(cls) -> None:
        """Set up the test case with a dummy converter."""
        cls.converter = Converter(
            [
                Record(prefix="GO", uri_prefix="http://purl.obolibrary.org/obo/GO_"),
                Record(prefix="rdfs", uri_prefix=str(rdflib.RDFS._NS)),
            ]
        )

    def test_simple(self):
        """Test a simple case of discovering URI prefixes."""
        uris = [f"http://ran.dom/{i:03}" for i in range(30)]
        uris.append("http://purl.obolibrary.org/obo/GO_0001234")

        converter = discover(uris, cutoff=3, converter=self.converter)
        self.assertEqual([Record(prefix="ns1", uri_prefix="http://ran.dom/")], converter.records)
        self.assertEqual("ns1:001", converter.compress("http://ran.dom/001"))
        self.assertIsNone(
            converter.compress("http://purl.obolibrary.org/obo/GO_0001234"),
            msg="discovered converter should not inherit reference converter's definitions",
        )

        converter = discover(uris, cutoff=50, converter=self.converter)
        self.assertEqual([], converter.records)
        self.assertIsNone(
            converter.compress("http://ran.dom/001"),
            msg="cutoff was high, so discovered converter should not detect `http://ran.dom/`",
        )

    def test_rdflib(self):
        """Test discovery in RDFlib."""
        graph = rdflib.Graph()
        for i in range(30):
            graph.add(
                (
                    rdflib.URIRef(f"http://ran.dom/{i:03}"),
                    rdflib.RDFS.subClassOf,
                    rdflib.URIRef(f"http://ran.dom/{i + 1:03}"),
                )
            )
            graph.add(
                (
                    rdflib.URIRef(f"http://ran.dom/{i:03}"),
                    rdflib.RDFS.label,
                    rdflib.Literal(f"Node {i}"),
                )
            )

        converter = discover_from_rdf(graph, converter=self.converter)
        self.assertEqual([Record(prefix="ns1", uri_prefix="http://ran.dom/")], converter.records)
        self.assertEqual("ns1:001", converter.compress("http://ran.dom/001"))
        self.assertIsNone(
            converter.compress("http://purl.obolibrary.org/obo/GO_0001234"),
            msg="discovered converter should not inherit reference converter's definitions",
        )

    @SLOW
    def test_remote(self):
        """Test parsing AEON."""
        converter = discover_from_rdf(
            graph="https://raw.githubusercontent.com/tibonto/aeon/main/aeon.owl",
            format="xml",
        )
        self.assertIn("http://purl.obolibrary.org/obo/AEON_", converter.reverse_prefix_map)