summaryrefslogtreecommitdiff
path: root/test/test_literal/test_duration.py
blob: c4e199c9ad035b5eb2183d8abd30c888dfe7f899 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
from datetime import timedelta

from isodate import Duration, parse_duration

from rdflib.namespace import XSD
from rdflib.term import Literal


class TestDuration:
    def test_to_python_timedelta(self):
        l = Literal("P4DT5H6M7S", datatype=XSD.dayTimeDuration)
        assert isinstance(l.toPython(), timedelta)
        assert l.toPython() == parse_duration("P4DT5H6M7S")

    def test_to_python_ym_duration(self):
        l = Literal("P1Y2M", datatype=XSD.yearMonthDuration)
        assert isinstance(l.toPython(), Duration)
        assert l.toPython() == parse_duration("P1Y2M")

    def test_to_python_ymdhms_duration(self):
        l = Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration)
        assert isinstance(l.toPython(), Duration)
        assert l.toPython() == parse_duration("P1Y2M4DT5H6M7S")

    def test_equalityself(self):
        x = Literal("P1Y2M3W4DT5H6M7S", datatype=XSD.duration)
        y = Literal("P1Y2M25DT5H6M7S", datatype=XSD.duration)
        assert x == y

    def test_duration_le(self):
        assert Literal("P4DT5H6M7S", datatype=XSD.duration) < Literal(
            "P8DT10H12M14S", datatype=XSD.duration
        )

    def test_duration_sum(self):
        assert Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration) + Literal(
            "P1Y2M4DT5H6M7S", datatype=XSD.duration
        ).toPython() == Literal("P2Y4M8DT10H12M14S", datatype=XSD.duration)

    def test_duration_sub_pos(self):
        assert Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration) - Literal(
            "P1Y2M3DT4H7M8S", datatype=XSD.duration
        ).toPython() == Literal("P1DT58M59S", datatype=XSD.duration)

    def test_duration_sub_neg(self):
        assert Literal("P1Y2M3DT4H7M8S", datatype=XSD.duration) - Literal(
            "P1Y2M4DT5H6M7S", datatype=XSD.duration
        ).toPython() == Literal("-P1DT58M59S", datatype=XSD.duration)