|
17 | 17 | except ImportError: |
18 | 18 | import unittest # noqa |
19 | 19 |
|
| 20 | +from binascii import unhexlify |
20 | 21 | from mock import Mock |
21 | 22 | import os |
22 | 23 | import six |
23 | 24 |
|
24 | 25 | import cassandra |
| 26 | +from cassandra.marshal import uint16_unpack, uint16_pack |
25 | 27 | from cassandra.metadata import (Murmur3Token, MD5Token, |
26 | 28 | BytesToken, ReplicationStrategy, |
27 | 29 | NetworkTopologyStrategy, SimpleStrategy, |
@@ -309,11 +311,32 @@ def test_md5_tokens(self): |
309 | 311 | class BytesTokensTest(unittest.TestCase): |
310 | 312 |
|
311 | 313 | 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) |
313 | 317 | self.assertEqual(bytes_token.hash_fn('123'), '123') |
314 | 318 | self.assertEqual(bytes_token.hash_fn(123), 123) |
315 | 319 | 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) |
317 | 340 |
|
318 | 341 |
|
319 | 342 | class KeyspaceMetadataTest(unittest.TestCase): |
|
0 commit comments