summaryrefslogtreecommitdiff
path: root/test/test_sparql/test_operators.py
blob: c20aace188c06149903e01718bd1e3f2edc6b0aa (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
35
import datetime

import pytest

import rdflib
from rdflib.plugins.sparql import operators, sparql


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)