diff options
| author | Victor Stinner <vstinner@redhat.com> | 2015-07-27 18:08:10 +0200 |
|---|---|---|
| committer | Victor Stinner <vstinner@redhat.com> | 2015-07-27 18:08:10 +0200 |
| commit | 3fde65eeae0019ec0971442886331fb7ffbcdffb (patch) | |
| tree | d9e9bb91d2c73af79597d9ed6a3066a923e5149f /tests | |
| parent | d9c602ffb15c2619788d159a2fe0aeb1de282f85 (diff) | |
| download | python-memcached-3fde65eeae0019ec0971442886331fb7ffbcdffb.tar.gz | |
More Python 3 fixes
* Port set_multi() to Python 3
* Port delete_multi() to Python 3
* Fix _get_server() on Python 3 when the connection to the first server
fails: encode to ASCII before calling serverHashFunction.
* Fix expect(): don't decode line on Python 3, return the raw line
* Add more unit tests
* tox now also runs unit tests
* Explicit the encoding when calling str.encode(): use 'utf-8'
* test_memcache: close sockets in tearDown()
* test_get_unknown_value(): delete the key to ensure that it doesn't exist
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_memcache.py | 133 | ||||
| -rw-r--r-- | tests/test_setmulti.py | 6 |
2 files changed, 61 insertions, 78 deletions
diff --git a/tests/test_memcache.py b/tests/test_memcache.py index e5c833a..54609d8 100644 --- a/tests/test_memcache.py +++ b/tests/test_memcache.py @@ -1,10 +1,11 @@ from __future__ import print_function -from unittest import TestCase +import time +import unittest import six -from memcache import Client, SERVER_MAX_KEY_LENGTH +from memcache import Client, SERVER_MAX_KEY_LENGTH, SERVER_MAX_VALUE_LENGTH try: _str_cls = basestring @@ -32,12 +33,14 @@ class FooStruct(object): return 0 -class TestMemcache(TestCase): +class TestMemcache(unittest.TestCase): def setUp(self): # TODO: unix socket server stuff servers = ["127.0.0.1:11211"] self.mc = Client(servers, debug=1) - pass + + def tearDown(self): + self.mc.disconnect_all() def check_setget(self, key, val, noreply=False): self.mc.set(key, val, noreply=noreply) @@ -64,6 +67,8 @@ class TestMemcache(TestCase): {"gm_an_integer": 42, "gm_a_string": "some random string"}) def test_get_unknown_value(self): + self.mc.delete("unknown_value") + self.assertEqual(self.mc.get("unknown_value"), None) def test_setget_foostruct(self): @@ -120,79 +125,55 @@ class TestMemcache(TestCase): self.mc.set('a' * SERVER_MAX_KEY_LENGTH, 1) self.mc.set('a' * SERVER_MAX_KEY_LENGTH, 1, noreply=True) + def test_unicode_key(self): + s = six.u('\u4f1a') + maxlen = SERVER_MAX_KEY_LENGTH // len(s.encode('utf-8')) + key = s * maxlen -if __name__ == "__main__": - # failures = 0 - # print("Testing docstrings...") - # _doctest() - # print("Running tests:") - # print() - # serverList = [["127.0.0.1:11211"]] - # if '--do-unix' in sys.argv: - # serverList.append([os.path.join(os.getcwd(), 'memcached.socket')]) + self.mc.set(key, 5) + value = self.mc.get(key) + self.assertEqual(value, 5) - # for servers in serverList: - # mc = Client(servers, debug=1) - if False: + def test_ignore_too_large_value(self): + # NOTE: "MemCached: while expecting[...]" is normal... + key = 'keyhere' + + value = 'a' * (SERVER_MAX_VALUE_LENGTH // 2) + self.assertTrue(self.mc.set(key, value)) + self.assertEqual(self.mc.get(key), value) + + value = 'a' * SERVER_MAX_VALUE_LENGTH + self.assertFalse(self.mc.set(key, value)) + # This test fails if the -I option is used on the memcached server + self.assertIsNone(self.mc.get(key)) + + def test_get_set_multi_key_prefix(self): + """Testing set_multi() with no memcacheds running.""" + + prefix = 'pfx_' + values = {'key1': 'a', 'key2': 'b'} + errors = self.mc.set_multi(values, key_prefix=prefix) + self.assertEqual(errors, []) + + keys = list(values) + self.assertEqual(self.mc.get_multi(keys, key_prefix=prefix), + values) + + def test_set_multi_dead_servers(self): + """Testing set_multi() with no memcacheds running.""" + + self.mc.disconnect_all() + for server in self.mc.servers: + server.mark_dead('test') + errors = self.mc.set_multi({'key1': 'a', 'key2': 'b'}) + self.assertEqual(sorted(errors), ['key1', 'key2']) + + def test_disconnect_all_delete_multi(self): + """Testing delete_multi() with no memcacheds running.""" + self.mc.disconnect_all() + ret = self.mc.delete_multi({'keyhere': 'a', 'keythere': 'b'}) + self.assertEqual(ret, 1) - print("Testing sending a unicode-string key...", end=" ") - try: - x = mc.set(six.u('keyhere'), 1) - except Client.MemcachedStringEncodingError as msg: - print("OK", end=" ") - else: - print("FAIL", end=" ") - failures += 1 - try: - x = mc.set((six.u('a')*SERVER_MAX_KEY_LENGTH).encode('utf-8'), 1) - except Client.MemcachedKeyError: - print("FAIL", end=" ") - failures += 1 - else: - print("OK", end=" ") - s = pickle.loads('V\\u4f1a\np0\n.') - try: - x = mc.set((s * SERVER_MAX_KEY_LENGTH).encode('utf-8'), 1) - except Client.MemcachedKeyLengthError: - print("OK") - else: - print("FAIL") - failures += 1 - - print("Testing using a value larger than the memcached value limit...") - print('NOTE: "MemCached: while expecting[...]" is normal...') - x = mc.set('keyhere', 'a'*SERVER_MAX_VALUE_LENGTH) - if mc.get('keyhere') is None: - print("OK", end=" ") - else: - print("FAIL", end=" ") - failures += 1 - x = mc.set('keyhere', 'a'*SERVER_MAX_VALUE_LENGTH + 'aaa') - if mc.get('keyhere') is None: - print("OK") - else: - print("FAIL") - failures += 1 - - print("Testing set_multi() with no memcacheds running", end=" ") - mc.disconnect_all() - errors = mc.set_multi({'keyhere': 'a', 'keythere': 'b'}) - if errors != []: - print("FAIL") - failures += 1 - else: - print("OK") - - print("Testing delete_multi() with no memcacheds running", end=" ") - mc.disconnect_all() - ret = mc.delete_multi({'keyhere': 'a', 'keythere': 'b'}) - if ret != 1: - print("FAIL") - failures += 1 - else: - print("OK") - if failures > 0: - print('*** THERE WERE FAILED TESTS') - sys.exit(1) - sys.exit(0) +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_setmulti.py b/tests/test_setmulti.py index 2f7108a..39d43c9 100644 --- a/tests/test_setmulti.py +++ b/tests/test_setmulti.py @@ -53,18 +53,20 @@ class test_Memcached_Set_Multi(unittest.TestCase): self.old_socket = socket.socket socket.socket = FakeSocket + self.mc = memcache.Client(['memcached'], debug=True) + def tearDown(self): socket.socket = self.old_socket def test_Socket_Disconnect(self): - client = memcache.Client(['memcached'], debug=True) mapping = {'foo': 'FOO', 'bar': 'BAR'} - bad_keys = client.set_multi(mapping) + bad_keys = self.mc.set_multi(mapping) self.assertEqual(sorted(bad_keys), ['bar', 'foo']) if DEBUG: print('set_multi({0!r}) -> {1!r}'.format(mapping, bad_keys)) + if __name__ == '__main__': unittest.main() |
