133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
# This file is part of the Glowtables software
|
|
# Copyright (C) 2023 Valentin Lorentz
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it under the
|
|
# terms of the GNU Affero General Public License version 3, as published by the
|
|
# Free Software Foundation.
|
|
#
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License along with
|
|
# this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import textwrap
|
|
|
|
import pytest
|
|
import rdflib
|
|
|
|
from glowtables.table import Language, LiteralField, Table
|
|
|
|
from .conftest import RdflibSparqlBackend
|
|
|
|
|
|
def test_single_literal(rdflib_sparql: RdflibSparqlBackend) -> None:
|
|
name_field = LiteralField(
|
|
"display_name",
|
|
{Language("en"): "Name"},
|
|
rdflib.URIRef("http://example.org/display-name"),
|
|
)
|
|
table = Table(
|
|
fields=[name_field],
|
|
constraints="?subject <http://example.org/type> <http://example.org/CPU>.",
|
|
)
|
|
|
|
assert table.sparql() == textwrap.dedent(
|
|
"""
|
|
SELECT ?display_name
|
|
WHERE {
|
|
?subject <http://example.org/type> <http://example.org/CPU>.
|
|
?subject <http://example.org/display-name> ?display_name.
|
|
}
|
|
"""
|
|
)
|
|
|
|
rdflib_sparql.query(table.sparql())
|
|
|
|
|
|
def test_two_literals(rdflib_sparql: RdflibSparqlBackend) -> None:
|
|
name_field = LiteralField(
|
|
"display_name",
|
|
{Language("en"): "Name"},
|
|
rdflib.URIRef("http://example.org/display-name"),
|
|
)
|
|
frequency_field = LiteralField(
|
|
"frequency",
|
|
{Language("en"): "Clock frequency"},
|
|
rdflib.URIRef("http://example.org/clock-frequency"),
|
|
)
|
|
table = Table(
|
|
fields=[name_field, frequency_field],
|
|
constraints="?subject <http://example.org/type> <http://example.org/CPU>.",
|
|
)
|
|
|
|
assert table.sparql() == textwrap.dedent(
|
|
"""
|
|
SELECT ?display_name ?frequency
|
|
WHERE {
|
|
?subject <http://example.org/type> <http://example.org/CPU>.
|
|
?subject <http://example.org/display-name> ?display_name.
|
|
?subject <http://example.org/clock-frequency> ?frequency.
|
|
}
|
|
"""
|
|
)
|
|
|
|
rdflib_sparql.query(table.sparql())
|
|
|
|
|
|
def test_default_value(rdflib_sparql: RdflibSparqlBackend) -> None:
|
|
name_field = LiteralField(
|
|
"display_name",
|
|
{Language("en"): "Name"},
|
|
rdflib.URIRef("http://example.org/display-name"),
|
|
default=rdflib.Literal("Anonymous CPU"),
|
|
)
|
|
table = Table(
|
|
fields=[name_field],
|
|
constraints="?subject <http://example.org/type> <http://example.org/CPU>.",
|
|
)
|
|
|
|
assert table.sparql() == textwrap.dedent(
|
|
"""
|
|
SELECT ?display_name
|
|
WHERE {
|
|
?subject <http://example.org/type> <http://example.org/CPU>.
|
|
OPTIONAL { ?subject <http://example.org/display-name> ?display_name. }.
|
|
}
|
|
"""
|
|
)
|
|
|
|
rdflib_sparql.query(table.sparql())
|
|
|
|
|
|
def test_field_id_subject() -> None:
|
|
name_field = LiteralField(
|
|
"subject",
|
|
{Language("en"): "Name"},
|
|
rdflib.URIRef("http://example.org/display-name"),
|
|
)
|
|
with pytest.raises(ValueError, match="both subject and a field id"):
|
|
Table(
|
|
fields=[name_field],
|
|
constraints="",
|
|
)
|
|
|
|
|
|
def test_field_id_clash() -> None:
|
|
name_field = LiteralField(
|
|
"name",
|
|
{Language("en"): "Name"},
|
|
rdflib.URIRef("http://example.org/name"),
|
|
)
|
|
display_name_field = LiteralField(
|
|
"name",
|
|
{Language("en"): "Display Name"},
|
|
rdflib.URIRef("http://example.org/display-name"),
|
|
)
|
|
with pytest.raises(ValueError, match="has duplicate field ids: name"):
|
|
Table(
|
|
fields=[name_field, display_name_field],
|
|
constraints="",
|
|
)
|