From c92de9254fa31f5dbf8bdbb6c4f6af6ce26a111a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=BCschelberger?= Date: Sun, 1 Dec 2024 18:45:15 +0100 Subject: [PATCH] add warning --- data2rdf/models/graph.py | 6 ++++ .../test_json_custom_relations.py | 31 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/data2rdf/models/graph.py b/data2rdf/models/graph.py index 16b2012..31bfdcf 100644 --- a/data2rdf/models/graph.py +++ b/data2rdf/models/graph.py @@ -5,6 +5,7 @@ from data2rdf.qudt.utils import _get_query_match from data2rdf.utils import make_prefix +from data2rdf.warnings import ParserWarning from data2rdf.models.utils import ( # isort:skip apply_datatype, @@ -141,6 +142,11 @@ def validate_value( value = int(value) elif isinstance(value, str) and is_float(value): value = float(value) + elif isinstance(value, str): + warnings.warn( + f"Cannot type case value from str into float or int: {value}", + ParserWarning + ) return value @field_validator("unit", mode="after") diff --git a/tests/abox/json_custom_relations/test_json_custom_relations.py b/tests/abox/json_custom_relations/test_json_custom_relations.py index 79fc81b..4f57d09 100644 --- a/tests/abox/json_custom_relations/test_json_custom_relations.py +++ b/tests/abox/json_custom_relations/test_json_custom_relations.py @@ -448,17 +448,28 @@ def test_pipeline_json_custom_relations_quantity_graph() -> None: from rdflib import Graph from data2rdf import Data2RDF, Parser + from data2rdf.warnings import ParserWarning + + with pytest.warns(ParserWarning, match="Cannot") as warnings: + + pipeline = Data2RDF( + raw_data=DATA_SUBGRAPHS, + mapping=MAPPING_SUBGRAPHS, + parser=Parser.json, + config={ + "base_iri": BASE_IRI, + "separator": "#", + "suppress_file_description": True, + }, + ) + + warnings = [ + warning + for warning in warnings + if warning.category == ParserWarning + ] + assert len(warnings) == 1 - pipeline = Data2RDF( - raw_data=DATA_SUBGRAPHS, - mapping=MAPPING_SUBGRAPHS, - parser=Parser.json, - config={ - "base_iri": BASE_IRI, - "separator": "#", - "suppress_file_description": True, - }, - ) expected_graph = Graph() expected_graph.parse(data=EXPECTED_SUBGRAPHS)