Skip to content

Commit 10c1247

Browse files
committed
more complete unit tests for BytesToken
PYTHON-559
1 parent 33ae06d commit 10c1247

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

tests/unit/test_metadata.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
except ImportError:
1818
import unittest # noqa
1919

20+
from binascii import unhexlify
2021
from mock import Mock
2122
import os
2223
import six
2324

2425
import cassandra
26+
from cassandra.marshal import uint16_unpack, uint16_pack
2527
from cassandra.metadata import (Murmur3Token, MD5Token,
2628
BytesToken, ReplicationStrategy,
2729
NetworkTopologyStrategy, SimpleStrategy,
@@ -309,11 +311,32 @@ def test_md5_tokens(self):
309311
class BytesTokensTest(unittest.TestCase):
310312

311313
def test_bytes_tokens(self):
312-
bytes_token = BytesToken(str(cassandra.metadata.MIN_LONG - 1))
314+
bytes_token = BytesToken(unhexlify('01'))
315+
self.assertEqual(bytes_token.value, six.b('\x01'))
316+
self.assertEqual(str(bytes_token), "<BytesToken: %s>" % bytes_token.value)
313317
self.assertEqual(bytes_token.hash_fn('123'), '123')
314318
self.assertEqual(bytes_token.hash_fn(123), 123)
315319
self.assertEqual(bytes_token.hash_fn(str(cassandra.metadata.MAX_LONG)), str(cassandra.metadata.MAX_LONG))
316-
self.assertEqual(str(bytes_token), "<BytesToken: -9223372036854775809>")
320+
321+
def test_from_string(self):
322+
from_unicode = BytesToken.from_string(six.text_type('0123456789abcdef'))
323+
from_bin = BytesToken.from_string(six.b('0123456789abcdef'))
324+
self.assertEqual(from_unicode, from_bin)
325+
self.assertIsInstance(from_unicode.value, six.binary_type)
326+
self.assertIsInstance(from_bin.value, six.binary_type)
327+
328+
def test_comparison(self):
329+
tok = BytesToken.from_string(six.text_type('0123456789abcdef'))
330+
token_high_order = uint16_unpack(tok.value[0:2])
331+
self.assertLess(BytesToken(uint16_pack(token_high_order - 1)), tok)
332+
self.assertGreater(BytesToken(uint16_pack(token_high_order + 1)), tok)
333+
334+
def test_comparison_unicode(self):
335+
value = six.b('\'_-()"\xc2\xac')
336+
t0 = BytesToken(value)
337+
t1 = BytesToken.from_string('00')
338+
self.assertGreater(t0, t1)
339+
self.assertFalse(t0 < t1)
317340

318341

319342
class KeyspaceMetadataTest(unittest.TestCase):

0 commit comments

Comments
 (0)