summaryrefslogtreecommitdiff
path: root/tests/test_cache.py
blob: 78e9ee03913dde373291c719cceed2480d0f50fd (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import unittest
from slixmpp.test import SlixTest
from slixmpp.util import (
    MemoryCache, MemoryPerJidCache,
    FileSystemCache, FileSystemPerJidCache
)
from tempfile import TemporaryDirectory

class TestCacheClass(SlixTest):

    def testMemoryCache(self):
        cache = MemoryCache()

        cache.store("test", "test_value")
        self.assertEqual(cache.retrieve("test"), "test_value")
        self.assertEqual(cache.retrieve("test2"), None)

        cache.remove("test")
        self.assertEqual(cache.retrieve("test"), None)

    def testMemoryPerJidcache(self):
        cache = MemoryPerJidCache()

        cache.store_by_jid("test@example.com", "test", "test_value")
        self.assertEqual(
            cache.retrieve_by_jid("test@example.com", "test"),
            "test_value"
        )

        cache.remove_by_jid("test@example.com", "test")
        self.assertEqual(
            cache.retrieve_by_jid("test@example.com", "test"),
            None
        )

    def testFileSystemCache(self):
        def failing_decode(value):
            if value == "failme":
                raise Exception("you failed")
            return value
        with TemporaryDirectory() as tmpdir:
            cache = FileSystemCache(tmpdir, "test", decode=failing_decode)
            cache.store("test", "test_value")
            cache.store("test2", "failme")
            self.assertEqual(
                cache.retrieve("test"),
                "test_value"
            )
            cache.remove("test")
            self.assertEqual(
                cache.retrieve("test"),
                None
            )

            self.assertEqual(
                cache.retrieve("test2"),
                None
            )

    def testFileSystemPerJidCache(self):
        with TemporaryDirectory() as tmpdir:
            cache = FileSystemPerJidCache(tmpdir, "test")
            cache.store_by_jid("test@example.com", "test", "test_value")
            self.assertEqual(
                cache.retrieve_by_jid("test@example.com", "test"),
                "test_value"
            )
            cache.remove_by_jid("test@example.com", "test")
            self.assertEqual(
                cache.retrieve_by_jid("test@example.com", "test"),
                None
            )

suite = unittest.TestLoader().loadTestsFromTestCase(TestCacheClass)