~homeworkprod/byceps

ref: 9f63b62a2b4647a97ebd3b6356d937ffa1d88876 byceps/tests/util/datetime/test_datetimerange.py -rw-r--r-- 2.0 KiB
9f63b62a — Jochen Kupperschmidt Note that Python 3.6 is a supported Python version 4 years ago
                                                                                
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime

from nose2.tools import params

from byceps.util.datetime.range import create_adjacent_ranges, DateTimeRange


@params(
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 11, 59, 59),
        False,
    ),
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 12,  0,  0),
        True,
    ),
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 12,  0,  1),
        True,
    ),
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 17, 48, 23),
        True,
    ),
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 19, 29, 59),
        True,
    ),
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        False,
    ),
    (
        datetime(2014,  8, 15, 12,  0,  0),
        datetime(2014,  8, 15, 19, 30,  0),
        datetime(2014,  8, 15, 19, 30,  1),
        False,
    ),
)
def test_contains(starts_at, ends_at, tested_datetime, expected):
    date_time_range = DateTimeRange(start=starts_at, end=ends_at)

    actual = date_time_range.contains(tested_datetime)

    assert actual == expected


def test_create_adjacent_ranges():
    dt1 = datetime(2014,  8, 15, 12,  0,  0)
    dt2 = datetime(2014,  8, 15, 19, 30,  0)
    dt3 = datetime(2014,  8, 16,  7, 15,  0)
    dt4 = datetime(2014,  8, 18, 22, 45,  0)

    dts = [dt1, dt2, dt3, dt4]

    expected = [
        DateTimeRange(dt1, dt2),
        DateTimeRange(dt2, dt3),
        DateTimeRange(dt3, dt4),
    ]

    actual = create_adjacent_ranges(dts)

    assert list(actual) == expected