summaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: 62e552ff5a796064f2f505678a948a9abc5fea51 (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
49
50
51
52
53
import sys
from datetime import datetime
from unittest.mock import Mock

import pytest
from apscheduler.serializers.cbor import CBORSerializer
from apscheduler.serializers.json import JSONSerializer
from apscheduler.serializers.pickle import PickleSerializer

if sys.version_info >= (3, 9):
    from zoneinfo import ZoneInfo
else:
    from backports.zoneinfo import ZoneInfo


@pytest.fixture(scope='session')
def timezone():
    return ZoneInfo('Europe/Berlin')


@pytest.fixture
def freeze_time(monkeypatch, timezone):
    class TimeFreezer:
        def __init__(self, initial):
            self.current = initial
            self.increment = None

        def get(self, tzinfo=None):
            now = self.current.astimezone(tzinfo) if tzinfo else self.current.replace(tzinfo=None)
            if self.increment:
                self.current += self.increment
            return now

        def set(self, new_time):
            self.current = new_time

        def next(self,):
            return self.current + self.increment

        def set_increment(self, delta):
            self.increment = delta

    freezer = TimeFreezer(timezone.localize(datetime(2011, 4, 3, 18, 40)))
    fake_datetime = Mock(datetime, now=freezer.get)
    monkeypatch.setattr('apscheduler.triggers.interval.datetime', fake_datetime)
    monkeypatch.setattr('apscheduler.triggers.date.datetime', fake_datetime)
    return freezer


@pytest.fixture(params=[None, PickleSerializer, CBORSerializer, JSONSerializer],
                ids=['none', 'pickle', 'cbor', 'json'])
def serializer(request):
    return request.param() if request.param else None