summaryrefslogtreecommitdiff
path: root/test/test_sparql_operators.py
blob: 2c122eb4ba17a62b09a1f19a6ebbb53f509172ca (plain)
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
import datetime

import rdflib
from rdflib.plugins.sparql import operators
from rdflib.plugins.sparql import sparql
import pytest

def test_date_cast():
    now = datetime.datetime.now()
    today = now.date()

    literal = rdflib.Literal(now)
    result = operators.date(literal)
    assert isinstance(result, datetime.date)
    assert result == today

    literal = rdflib.Literal(today)
    result = operators.date(literal)
    assert isinstance(result, datetime.date)
    assert result == today


def test_datetime_cast():
    now = datetime.datetime.now()
    literal = rdflib.Literal(now)
    result = operators.datetime(literal)
    assert isinstance(result, datetime.datetime)
    assert result == now


def test_datetime_cast_type_error():
    literal = rdflib.Literal("2020-01-02")
    with pytest.raises(sparql.SPARQLError):
        operators.date(literal)