File: test_sparql_construct_bindings.py

package info (click to toggle)
rdflib 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 38,248 kB
  • sloc: python: 39,216; sh: 153; makefile: 110
file content (38 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from rdflib import Graph, URIRef, Literal, BNode
from rdflib.plugins.sparql import prepareQuery
from rdflib.compare import isomorphic

import unittest
from .testutils import eq_

class TestConstructInitBindings(unittest.TestCase):
    def test_construct_init_bindings(self):
        """
        This is issue https://github.com/RDFLib/rdflib/issues/1001
        """

        g1 = Graph()

        q_str = """
        PREFIX : <urn:ns1:>
        CONSTRUCT {
          ?uri :prop1 ?val1;
               :prop2 ?c .
        }
        WHERE {
          bind(uri(concat("urn:ns1:", ?a)) as ?uri)
          bind(?b as ?val1)
        }
        """
        q_prepared = prepareQuery(q_str)

        expected = [
            (URIRef("urn:ns1:A"), URIRef("urn:ns1:prop1"), Literal("B")),
            (URIRef("urn:ns1:A"), URIRef("urn:ns1:prop2"), Literal("C")),
        ]
        results = g1.query(
            q_prepared,
            initBindings={"a": Literal("A"), "b": Literal("B"), "c": Literal("C")},
        )

        eq_(sorted(results, key=lambda x: str(x[1])), expected)