93 lines
2.8 KiB
Python
93 lines
2.8 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 rdflib
|
|
|
|
from glowtables.table import Language, LiteralField, Table
|
|
|
|
|
|
def test_single_literal() -> 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.
|
|
}
|
|
"""
|
|
)
|
|
|
|
|
|
def test_two_literals() -> 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.
|
|
}
|
|
"""
|
|
)
|
|
|
|
|
|
def test_default_value() -> 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. }.
|
|
}
|
|
"""
|
|
)
|