Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmem_plugin_base/dataintegration/parameter/graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Knowledge Graph Parameter Type."""

import re
from typing import Any

from cmem.cmempy.dp.proxy.graph import get_graphs_list
Expand All @@ -8,6 +9,8 @@
from cmem_plugin_base.dataintegration.types import Autocompletion, StringParameterType
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access

IRI_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*:.+$")


class GraphParameterType(StringParameterType):
"""Knowledge Graph parameter type."""
Expand All @@ -34,6 +37,8 @@ def __init__(
- if None -> defaults to di:Dataset, void:Dataset and shui:QueryCatalog
:param allow_only_autocompleted_values: allow entering new graph URLs
"""
self.name = "scheme:string"
self._validate_graph()
self.show_di_graphs = show_di_graphs
self.show_system_graphs = show_system_graphs
self.show_graphs_without_class = show_graphs_without_class
Expand Down Expand Up @@ -91,3 +96,9 @@ def autocomplete(
continue
result.sort(key=lambda x: x.label) # type: ignore[return-value, arg-type]
return list(set(result))

def _validate_graph(self) -> None:
"""Verify that graph name is valid aka it has at least a scheme and something after it"""
is_valid = bool(IRI_PATTERN.match(self.name))
if not is_valid:
raise ValueError(f"Could not validate graph IRI '{self.name}'")
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ keywords = [
homepage = "https://github.com/eccenca/cmem-plugin-base"

[tool.poetry.dependencies]
python = "^3.13"
cmem-cmempy = ">=25.2.0"
python = ">=3.13, ^3"
cmem-cmempy = "^25.4.0"
pydantic = "^2.12.2"
python-ulid = "^3.1.0"


[tool.poetry.group.dev.dependencies]
deptry = "^0.24.0"
genbadge = {extras = ["coverage"], version = "^1.1.3"}
Expand Down
28 changes: 28 additions & 0 deletions tests/parameter_types/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""graph parameter type tests"""

import pytest

from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
from cmem_plugin_base.testing import TestPluginContext
from tests.utils import needs_cmem
Expand Down Expand Up @@ -28,3 +30,29 @@ def test_graph_parameter_type_completion() -> None:
)
== 0
)


def test_graph_validation() -> None:
"""Test graph parameter string validation"""
parameter = GraphParameterType(show_system_graphs=True)

parameter.name = "urn:ISBN:3-8273-7019-1"
parameter._validate_graph() # noqa: SLF001

parameter.name = "http://test/data"
parameter._validate_graph() # noqa: SLF001

parameter.name = "https://test/data"
parameter._validate_graph() # noqa: SLF001

parameter.name = "test :test"
with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"):
parameter._validate_graph() # noqa: SLF001

parameter.name = ":ttt"
with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"):
parameter._validate_graph() # noqa: SLF001

parameter.name = ""
with pytest.raises(ValueError, match=f"Could not validate graph IRI '{parameter.name}'"):
parameter._validate_graph() # noqa: SLF001