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
 
EitherGraph = Graph | SyncedGraph
 

	
 
_ObjType = TypeVar('_ObjType')
 
@@ -72,16 +72,17 @@ def typedValue(objType: Type[_ObjType], 
 

	
 
    ConvFrom: Type[Node] = type(obj)
 
    ConvTo = 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}')
 
    # if objType is float and isinstance(conv, decimal.Decimal):
 
    #     conv = float(conv)
 
    return cast(objType, conv)
 
\ No newline at end of file
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
 

	
 
g = cast(
 
    Graph,
 
    MockSyncedGraph('''
 
    @prefix : <http://light9.bigasterisk.com/> .
 
    :subj
 
        :uri :c;
 
        :bnode [];
 
        # see https://w3c.github.io/N3/spec/#literals for syntaxes.
 
        :int 0;
 
        :float1 0.0;
 
        :float2 1.0e1;
 
        :float3 0.5;
 
        :color "#ffffff"^^:hexColor;
 
@@ -45,12 +46,20 @@ class TestTypeIncludes:
 

	
 
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
 
        assert typedValue(float, g, subj, L9['float2']) == 10
 
        assert typedValue(float, g, subj, L9['float3']) == 0.5
 

	
0 comments (0 inline, 0 general)