def test_from_string(self): from_unicode = BytesToken.from_string( six.text_type('0123456789abcdef')) from_bin = BytesToken.from_string(six.b('0123456789abcdef')) self.assertEqual(from_unicode, from_bin) self.assertIsInstance(from_unicode.value, six.binary_type) self.assertIsInstance(from_bin.value, six.binary_type)
def test_bytes_tokens(self): bytes_token = BytesToken(unhexlify(six.b('01'))) self.assertEqual(bytes_token.value, six.b('\x01')) self.assertEqual(str(bytes_token), "<BytesToken: %s>" % bytes_token.value) self.assertEqual(bytes_token.hash_fn('123'), '123') self.assertEqual(bytes_token.hash_fn(123), 123) self.assertEqual(bytes_token.hash_fn(str(cassandra.metadata.MAX_LONG)), str(cassandra.metadata.MAX_LONG))
def test_bytes_tokens(self): bytes_token = BytesToken(str(cassandra.metadata.MIN_LONG - 1)) self.assertEqual(bytes_token.hash_fn('123'), '123') self.assertEqual(bytes_token.hash_fn(123), 123) self.assertEqual(bytes_token.hash_fn(str(cassandra.metadata.MAX_LONG)), str(cassandra.metadata.MAX_LONG)) self.assertEqual(str(bytes_token), "<BytesToken: -9223372036854775809>") try: bytes_token = BytesToken(cassandra.metadata.MIN_LONG - 1) self.fail('Tokens for ByteOrderedPartitioner should be only strings') except TypeError: pass
def test_token_values(self): """ Spot check token classes and values """ # spot check murmur3 murmur3_token = Murmur3Token(cassandra.metadata.MIN_LONG - 1) self.assertEqual(murmur3_token.hash_fn('123'), -7468325962851647638) self.assertEqual(murmur3_token.hash_fn(str(cassandra.metadata.MAX_LONG)), 7162290910810015547) self.assertEqual(str(murmur3_token), '<Murmur3Token: -9223372036854775809L>') md5_token = MD5Token(cassandra.metadata.MIN_LONG - 1) self.assertEqual(md5_token.hash_fn('123'), 42767516990368493138776584305024125808L) self.assertEqual(md5_token.hash_fn(str(cassandra.metadata.MAX_LONG)), 28528976619278518853815276204542453639L) self.assertEqual(str(md5_token), '<MD5Token: -9223372036854775809L>') bytes_token = BytesToken(str(cassandra.metadata.MIN_LONG - 1)) self.assertEqual(bytes_token.hash_fn('123'), '123') self.assertEqual(bytes_token.hash_fn(123), 123) self.assertEqual(bytes_token.hash_fn(str(cassandra.metadata.MAX_LONG)), str(cassandra.metadata.MAX_LONG)) self.assertEqual(str(bytes_token), "<BytesToken: '-9223372036854775809'>") try: bytes_token = BytesToken(cassandra.metadata.MIN_LONG - 1) self.fail('Tokens for ByteOrderedPartitioner should be only strings') except TypeError: pass
def test_comparison_unicode(self): value = six.b('\'_-()"\xc2\xac') t0 = BytesToken(value) t1 = BytesToken.from_string('00') self.assertGreater(t0, t1) self.assertFalse(t0 < t1)
def test_comparison(self): tok = BytesToken.from_string(six.text_type('0123456789abcdef')) token_high_order = uint16_unpack(tok.value[0:2]) self.assertLess(BytesToken(uint16_pack(token_high_order - 1)), tok) self.assertGreater(BytesToken(uint16_pack(token_high_order + 1)), tok)
def test_from_string(self): from_unicode = BytesToken.from_string(six.text_type('0123456789abcdef')) from_bin = BytesToken.from_string(six.b('0123456789abcdef')) self.assertEqual(from_unicode, from_bin) self.assertIsInstance(from_unicode.value, six.binary_type) self.assertIsInstance(from_bin.value, six.binary_type)