Changeset - d1946cb32121
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 8 months ago 2024-05-28 22:34:21
drewp@bigasterisk.com
bool support
2 files changed with 6 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/light9/typedgraph.py
Show inline comments
 
@@ -35,24 +35,25 @@ def _typeIncludes(t1: Type, t2: Type) ->
 
        return any(_typeIncludes(t, t2) for t in ts)
 

	
 
    return False
 

	
 

	
 
def _convLiteral(objType: Type[_ObjType], x: Literal) -> _ObjType:
 
    if _typeIncludes(objType, Literal):
 
        return cast(objType, x)
 

	
 
    for outType, dtypes in [
 
        (float, (XSD['integer'], XSD['double'], XSD['decimal'])),
 
        (int, (XSD['integer'],)),
 
        (bool, (XSD['boolean'],)), # todo tests
 
        (str, ()),
 
    ]:
 
        for t in _expandUnion(objType):
 
            if _typeIncludes(t, outType) and (not dtypes or x.datatype in dtypes):
 
                # e.g. user wants float and we have xsd:double
 
                return cast(objType, outType(x.toPython()))
 
    raise ConversionError
 

	
 

	
 
def typedValue(objType: Type[_ObjType], graph: EitherGraph, subj: Node, pred: URIRef) -> _ObjType:
 
    """graph.value(subj, pred) with a given return type.
 
    If objType is not an rdflib.Node, we toPython() the value.
src/light9/typedgraph_test.py
Show inline comments
 
@@ -10,24 +10,26 @@ from light9.typedgraph import Conversion
 
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;
 
        :boolt true;
 
        :boolf false;
 
        :color "#ffffff"^^:hexColor;
 
        :definitelyAString "hello" .
 
'''))
 

	
 
subj = L9['subj']
 

	
 

	
 
class TestTypeIncludes:
 

	
 
    def test_includesItself(self):
 
        assert _typeIncludes(str, str)
 

	
 
@@ -80,24 +82,27 @@ class TestTypedValueReturnsBasicTypes:
 
        tv = typedValue(str, g, subj, L9['color'])
 
        assert tv == '#ffffff'
 

	
 
    def test_getsLiteral(self):
 
        tv = typedValue(Literal, g, subj, L9['float2'])
 
        assert type(tv) == Literal
 
        assert tv.datatype == XSD['double']
 

	
 
        tv = typedValue(Literal, g, subj, L9['color'])
 
        assert type(tv) == Literal
 
        assert tv.datatype == L9['hexColor']
 

	
 
    def test_getsBoolean(self):
 
        assert typedValue(bool, g, subj, L9['boolt'])
 
        assert not typedValue(bool, g, subj, L9['boolf'])
 

	
 
class TestTypedValueDoesntDoInappropriateUriStringConversions:
 

	
 
    def test_noUriToString(self):
 
        with pytest.raises(ConversionError):
 
            typedValue(str, g, subj, L9['uri'])
 

	
 
    def test_noUriToLiteral(self):
 
        with pytest.raises(ConversionError):
 
            typedValue(Literal, g, subj, L9['uri'])
 

	
 
    def test_noStringToUri(self):
0 comments (0 inline, 0 general)