File: test_cache.py

package info (click to toggle)
python-zeep 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,600 kB
  • sloc: python: 15,592; makefile: 13
file content (91 lines) | stat: -rw-r--r-- 3,043 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
import datetime

import freezegun
import pytest

from zeep import cache


def test_base_add():
    base = cache.Base()
    with pytest.raises(NotImplementedError):
        base.add("test", b"test")


def test_base_get():
    base = cache.Base()
    with pytest.raises(NotImplementedError):
        base.get("test")


class TestSqliteCache:
    def test_in_memory(self):
        with pytest.raises(ValueError):
            cache.SqliteCache(path=":memory:")

    def test_cache(self, tmpdir):
        c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
        c.add("http://tests.python-zeep.org/example.wsdl", b"content")

        result = c.get("http://tests.python-zeep.org/example.wsdl")
        assert result == b"content"

    def test_no_records(self, tmpdir):
        c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
        result = c.get("http://tests.python-zeep.org/example.wsdl")
        assert result is None

    def test_has_expired(self, tmpdir):
        c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
        c.add("http://tests.python-zeep.org/example.wsdl", b"content")

        freeze_dt = datetime.datetime.utcnow() + datetime.timedelta(seconds=7200)
        with freezegun.freeze_time(freeze_dt):
            result = c.get("http://tests.python-zeep.org/example.wsdl")
            assert result is None

    def test_has_not_expired(self, tmpdir):
        c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
        c.add("http://tests.python-zeep.org/example.wsdl", b"content")
        result = c.get("http://tests.python-zeep.org/example.wsdl")
        assert result == b"content"


def test_memory_cache_timeout(tmpdir):
    c = cache.InMemoryCache()
    c.add("http://tests.python-zeep.org/example.wsdl", b"content")
    result = c.get("http://tests.python-zeep.org/example.wsdl")
    assert result == b"content"

    freeze_dt = datetime.datetime.utcnow() + datetime.timedelta(seconds=7200)
    with freezegun.freeze_time(freeze_dt):
        result = c.get("http://tests.python-zeep.org/example.wsdl")
        assert result is None


def test_memory_cache_share_data(tmpdir):
    a = cache.InMemoryCache()
    b = cache.InMemoryCache()
    a.add("http://tests.python-zeep.org/example.wsdl", b"content")

    result = b.get("http://tests.python-zeep.org/example.wsdl")
    assert result == b"content"


class TestIsExpired:
    def test_timeout_none(self):
        assert cache._is_expired(100, None) is False

    def test_has_expired(self):
        timeout = 7200
        utcnow = datetime.datetime.utcnow()
        value = utcnow + datetime.timedelta(seconds=timeout)
        with freezegun.freeze_time(utcnow):
            assert cache._is_expired(value, timeout) is False

    def test_has_not_expired(self):
        timeout = 7200
        utcnow = datetime.datetime.utcnow()
        value = utcnow - datetime.timedelta(seconds=timeout)
        with freezegun.freeze_time(utcnow):
            assert cache._is_expired(value, timeout) is False