File: test_optional.py

package info (click to toggle)
rdflib 7.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,852 kB
  • sloc: python: 59,555; sh: 153; makefile: 83; ruby: 74; xml: 45
file content (37 lines) | stat: -rw-r--r-- 1,099 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
from rdflib import Graph, Literal, URIRef, Variable


def test_binding_with_optional_clause() -> None:
    """
    Optional clauses should bind variables if feasible.

    See https://github.com/RDFLib/rdflib/issues/2957
    """
    g = Graph().parse(
        data="""
        prefix ex:  <https://www.example.org/>
        ex:document ex:subject "Nice cars" .
        ex:someCar   ex:type "Car" .
        """
    )
    result = g.query(
        """prefix ex:  <https://www.example.org/>
        select ?subject ?car
        where  {
                $this ex:subject ?subject.
                optional
                {
                 # an offending subselect clause
                 select ?car
                 where {
                           ?car ex:type "Car".
                       }
                  }
            }"""
    )
    assert len(result.bindings) == 1
    (first,) = result.bindings
    assert first.get(Variable("car")) == URIRef("https://www.example.org/someCar")
    assert first.get(Variable("subject")) == Literal(
        "Nice cars"
    ), "optional clause didnt bind"