test_utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import six
  2. import pytest
  3. from library.pymemcache.test.utils import MockMemcacheClient
  4. @pytest.mark.unit()
  5. def test_get_set():
  6. client = MockMemcacheClient()
  7. assert client.get(b"hello") is None
  8. client.set(b"hello", 12)
  9. assert client.get(b"hello") == 12
  10. @pytest.mark.unit()
  11. def test_get_set_unicide_key():
  12. client = MockMemcacheClient()
  13. assert client.get(u"hello") is None
  14. client.set(b"hello", 12)
  15. assert client.get(u"hello") == 12
  16. @pytest.mark.unit()
  17. def test_get_set_non_ascii_value():
  18. client = MockMemcacheClient()
  19. assert client.get(b"hello") is None
  20. # This is the value of msgpack.packb('non_ascii')
  21. non_ascii_str = b'\xa9non_ascii'
  22. client.set(b"hello", non_ascii_str)
  23. assert client.get(b"hello") == non_ascii_str
  24. @pytest.mark.unit()
  25. def test_get_many_set_many():
  26. client = MockMemcacheClient()
  27. client.set(b"h", 1)
  28. result = client.get_many([b"h", b"e", b"l", b"o"])
  29. assert result == {b"h": 1}
  30. # Convert keys into bytes
  31. d = dict((k.encode('ascii'), v)
  32. for k, v in six.iteritems(dict(h=1, e=2, z=3)))
  33. client.set_many(d)
  34. assert client.get_many([b"h", b"e", b"z", b"o"]) == d
  35. @pytest.mark.unit()
  36. def test_get_many_set_many_non_ascii_values():
  37. client = MockMemcacheClient()
  38. # These are the values of calling msgpack.packb() on '1', '2', and '3'
  39. non_ascii_1 = b'\xa11'
  40. non_ascii_2 = b'\xa12'
  41. non_ascii_3 = b'\xa13'
  42. client.set(b"h", non_ascii_1)
  43. result = client.get_many([b"h", b"e", b"l", b"o"])
  44. assert result == {b"h": non_ascii_1}
  45. # Convert keys into bytes
  46. d = dict((k.encode('ascii'), v)
  47. for k, v in six.iteritems(
  48. dict(h=non_ascii_1, e=non_ascii_2, z=non_ascii_3)
  49. ))
  50. client.set_many(d)
  51. assert client.get_many([b"h", b"e", b"z", b"o"]) == d
  52. @pytest.mark.unit()
  53. def test_add():
  54. client = MockMemcacheClient()
  55. client.add(b"k", 2)
  56. assert client.get(b"k") == 2
  57. client.add(b"k", 25)
  58. assert client.get(b"k") == 2
  59. @pytest.mark.unit()
  60. def test_delete():
  61. client = MockMemcacheClient()
  62. client.add(b"k", 2)
  63. assert client.get(b"k") == 2
  64. client.delete(b"k")
  65. assert client.get(b"k") is None
  66. @pytest.mark.unit()
  67. def test_incr_decr():
  68. client = MockMemcacheClient()
  69. client.add(b"k", 2)
  70. client.incr(b"k", 4)
  71. assert client.get(b"k") == 6
  72. client.decr(b"k", 2)
  73. assert client.get(b"k") == 4
  74. @pytest.mark.unit()
  75. def test_prepand_append():
  76. client = MockMemcacheClient()
  77. client.set(b"k", '1')
  78. client.append(b"k", 'a')
  79. client.prepend(b"k", 'p')
  80. assert client.get(b"k") == b'p1a'