Changeset - 5edb163780e2
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 20 months ago 2023-05-23 18:44:54
drewp@bigasterisk.com
caller can ask for Node and BNode is allowed
2 files changed with 14 insertions and 4 deletions:
0 comments (0 inline, 0 general)
light9/typedgraph.py
Show inline comments
 
from typing import List, Type, TypeVar, cast, get_args
 

	
 
from rdfdb.syncedgraph.syncedgraph import SyncedGraph
 
from rdflib import XSD, Graph, Literal, URIRef
 
from rdflib import XSD, BNode, Graph, Literal, URIRef
 
from rdflib.term import Node
 

	
 
# todo: this ought to just require a suitable graph.value method
 
@@ -75,10 +75,11 @@ def typedValue(objType: Type[_ObjType], 
 
    try:
 
        if ConvFrom == URIRef and _typeIncludes(ConvTo, URIRef):
 
            conv = obj
 
        elif ConvFrom == BNode and issubclass(BNode, ConvTo):
 
            conv = obj
 
        elif ConvFrom == Literal:
 
            conv = _convLiteral(objType, cast(Literal, obj))
 
        else:
 
            # e.g. BNode is not handled yet
 
            raise ConversionError
 
    except ConversionError:
 
        raise ConversionError(f'graph contains {type(obj)}, caller requesting {objType}')
light9/typedgraph_test.py
Show inline comments
 
from typing import NewType, Optional, cast
 

	
 
import pytest
 
from rdflib import Graph, Literal, URIRef
 

	
 
from rdflib import BNode, Graph, Literal, URIRef
 
from rdflib.term import Node
 
from light9.mock_syncedgraph import MockSyncedGraph
 
from light9.namespaces import L9, XSD
 
from light9.typedgraph import ConversionError, _typeIncludes, typedValue
 
@@ -13,6 +13,7 @@ g = cast(
 
    @prefix : <http://light9.bigasterisk.com/> .
 
    :subj
 
        :uri :c;
 
        :bnode [];
 
        # see https://w3c.github.io/N3/spec/#literals for syntaxes.
 
        :int 0;
 
        :float1 0.0;
 
@@ -48,6 +49,14 @@ class TestTypedValueReturnsBasicTypes:
 
    def test_getsUri(self):
 
        assert typedValue(URIRef, g, subj, L9['uri']) == L9['c']
 

	
 
    def test_getsBNode(self):
 
        # this is unusual usage since users ought to always be able to replace BNode with URIRef
 
        assert typedValue(BNode, g, subj, L9['bnode']) == g.value(subj,  L9['bnode'])
 

	
 
    def test_getsBNodeAsNode(self):
 
        assert typedValue(Node, g, subj, L9['bnode']) == g.value(subj,  L9['bnode'])
 

	
 

	
 
    def test_getsNumerics(self):
 
        assert typedValue(float, g, subj, L9['int']) == 0
 
        assert typedValue(float, g, subj, L9['float1']) == 0
0 comments (0 inline, 0 general)