File: test_cmemcached.py

package info (click to toggle)
pylibmc 1.6.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576 kB
  • sloc: ansic: 2,398; python: 1,014; makefile: 95
file content (74 lines) | stat: -rw-r--r-- 2,196 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
"""Ported cmemcached tests"""

# NOTE Don't write new tests here.
# These are ported from cmemcached to ensure compatibility.

import pylibmc
from tests import PylibmcTestCase

a           = "a"
I_          = b"I "
Do          = b"Do"
I_Do        = b"I Do"
n12345      = "12345"
num12345    = "num12345"
str12345    = "str12345"
hello_world = "hello world"


class TestCmemcached(PylibmcTestCase):
    def testSetAndGet(self):
        self.mc.set(num12345, 12345)
        assert self.mc.get(num12345) == 12345
        self.mc.set(str12345, n12345)
        assert self.mc.get(str12345) == n12345

    def testDelete(self):
        self.mc.set(str12345, n12345)
        #delete return True on success, otherwise False
        assert self.mc.delete(str12345)
        assert self.mc.get(str12345) is None

        # This test only works with old memcacheds. This has become a "client
        # error" in memcached.
        try:
            assert not self.mc.delete(hello_world)
        except pylibmc.ClientError:
            pass

    def testGetMulti(self):
        self.mc.set("a", "valueA")
        self.mc.set("b", "valueB")
        self.mc.set("c", "valueC")
        result = self.mc.get_multi(["a", "b", "c", "", "hello world"])
        assert result == {'a': 'valueA', 'b': 'valueB', 'c': 'valueC'}

    def testBigGetMulti(self):
        count = 10 ** 3
        # Python 2: .encode() is a no-op on these byte strings since they
        # only contain bytes that can be implicitly decoded as ASCII.
        keys = ['key%d' % i for i in range(count)]
        pairs = zip(keys, ['value%d' % i for i in range(count)])

        d = {}
        for key, value in pairs:
            d[key] = value
            self.mc.set(key, value)
        result = self.mc.get_multi(keys)
        assert result == d

    def testFunnyDelete(self):
        s = ""
        assert not self.mc.delete(s)

    def testAppend(self):
        self.mc.delete(a)
        self.mc.set(a, I_)
        assert self.mc.append(a, Do)
        assert self.mc.get(a) == I_Do

    def testPrepend(self):
        self.mc.delete(a)
        self.mc.set(a, Do)
        assert self.mc.prepend(a, I_)
        assert self.mc.get(a) == I_Do