Skip to content

Commit

Permalink
Correctly test unicode/byte values in both python2 and python3
Browse files Browse the repository at this point in the history
Add mock for testing
  • Loading branch information
alastair committed Mar 26, 2018
1 parent 56bcef9 commit b0cd9ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
26 changes: 20 additions & 6 deletions brainzutils/test/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,35 @@ class CacheKeyTestCase(unittest.TestCase):

@mock.patch('brainzutils.cache.redis.StrictRedis', autospec=True)
def test_set_key(self, mock_redis):
"""Test setting a bytes value"""
cache.init(host='host', port=2, namespace=self.namespace)
cache.set('key', 'value')
cache.set('key', u'value'.encode('utf-8'))

expected_key = 'NS_TEST:a62f2225bf70bfaccbc7f1ef2a397836717377de'
# Keys are encoded into bytes always
expected_key = 'NS_TEST:a62f2225bf70bfaccbc7f1ef2a397836717377de'.encode('utf-8')
# msgpack encoded value
expected_value = '\xc4\x05value'
expected_value = b'\xc4\x05value'
mock_redis.return_value.mset.assert_called_with({expected_key: expected_value})
mock_redis.return_value.pexpire.assert_not_called()

@mock.patch('brainzutils.cache.redis.StrictRedis', autospec=True)
def test_set_key_unicode(self, mock_redis):
"""Test setting a unicode value"""
cache.init(host='host', port=2, namespace=self.namespace)
cache.set('key', u'value')

expected_key = 'NS_TEST:a62f2225bf70bfaccbc7f1ef2a397836717377de'.encode('utf-8')
# msgpack encoded value
expected_value = b'\xa5value'
mock_redis.return_value.mset.assert_called_with({expected_key: expected_value})
mock_redis.return_value.pexpire.assert_not_called()

@mock.patch('brainzutils.cache.redis.StrictRedis', autospec=True)
def test_key_expire(self, mock_redis):
cache.init(host='host', port=2, namespace=self.namespace)
cache.set('key', 'value', time=30)
expected_key = 'NS_TEST:a62f2225bf70bfaccbc7f1ef2a397836717377de'
cache.set('key', u'value'.encode('utf-8'), time=30)
expected_key = 'NS_TEST:a62f2225bf70bfaccbc7f1ef2a397836717377de'.encode('utf-8')
# msgpack encoded value
expected_value = '\xc4\x05value'
expected_value = b'\xc4\x05value'
mock_redis.return_value.mset.assert_called_with({expected_key: expected_value})
mock_redis.return_value.pexpire.assert_called_with(expected_key, 30000)
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest==3.1.2
pytest-cov==2.5.1
pylint==1.7.2
mock==2.0.0

0 comments on commit b0cd9ae

Please sign in to comment.