From a3060be40f11fe12e0ebf4f06bcdcb9c450c8433 Mon Sep 17 00:00:00 2001 From: vennyy3 Date: Wed, 1 Jul 2026 11:46:36 +0200 Subject: [PATCH] tests for user defined functions --- lib/query_processing/src/errors.rs | 2 +- py_maplib/tests/test_udfs.py | 49 +++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/query_processing/src/errors.rs b/lib/query_processing/src/errors.rs index 9b090c19..463c93b0 100644 --- a/lib/query_processing/src/errors.rs +++ b/lib/query_processing/src/errors.rs @@ -31,7 +31,7 @@ pub enum QueryProcessingError { ExpectedIntegerArgument(Function), #[error("Error parsing literal {0}")] ParseLiteralError(RepresentationError), - #[error("UDF function error {0}")] + #[error("UDF function error: {0}")] UDFError(String), #[error( "Custom function not found: {0}, it is possible to define a function using m.add_udf()" diff --git a/py_maplib/tests/test_udfs.py b/py_maplib/tests/test_udfs.py index 24212168..babfee1f 100644 --- a/py_maplib/tests/test_udfs.py +++ b/py_maplib/tests/test_udfs.py @@ -1,5 +1,7 @@ import polars as pl -from maplib import Model, xsd +import pytest +from polars import DataFrame +from maplib import Model, xsd, RDFType, MaplibException def f(df: pl.DataFrame) -> pl.Series: df = df.with_columns( @@ -13,6 +15,12 @@ def g(df: pl.DataFrame) -> pl.DataFrame: ) return df +def h(df: DataFrame) -> pl.DataFrame: + df = df.with_columns( + (pl.col("0") + pl.lit("_suf")).alias("out") + ) + return df + def test_udf_query(): m = Model() m.add_udf("urn:maplib:plusarg", f, xsd.integer) @@ -49,8 +57,47 @@ def test_udf_query_strings(): print(result) assert result.get_column("found").sort().to_list() == [None, False, False, True] +def test_udf_query_iris(): + m = Model() + m.add_udf("urn:maplib:addsuf", h, RDFType.IRI, [RDFType.IRI]) + m.reads(""" + . + . + . + """, "turtle") + result = m.query(""" + SELECT * { + ?a ?s1 . + BIND((?s1) AS ?suffed) + } + """) + assert result.get_column("suffed").sort().to_list() == ['', '', ''] + def test_list_udfs(): m = Model() assert m.list_udfs() == [] m.add_udf("urn:maplib:myudf", f, xsd.integer) assert m.list_udfs() == ["urn:maplib:myudf"] + +def test_udf_error_message(): + m = Model() + with pytest.raises(Exception, match="is not callable"): + m.add_udf("urn:maplib:test", "test", xsd.boolean, [xsd.string]) + +def test_udf_wrong_output_type(): + m = Model() + m.add_udf("urn:maplib:findabc", g, xsd.integer, [xsd.string]) + m.reads(""" + "1" . + "ab2" . + "abc" . + 1 . + """, "turtle") + with pytest.raises(MaplibException) as e: + result = m.query(""" + SELECT * { + ?a ?s1 . + BIND((?s1) AS ?found) + } + """) + assert "did not have the expected type" in e \ No newline at end of file