forked from LBRYCommunity/lbry-sdk
add lbrynet.dht.protocol.distance unit tests
This commit is contained in:
parent
44f92a271f
commit
664f91bfab
2 changed files with 16 additions and 1 deletions
|
@ -10,11 +10,13 @@ class Distance:
|
|||
|
||||
def __init__(self, key: bytes):
|
||||
if len(key) != constants.hash_length:
|
||||
raise ValueError("invalid key length: %i" % len(key))
|
||||
raise ValueError(f"invalid key length: {len(key)}")
|
||||
self.key = key
|
||||
self.val_key_one = int.from_bytes(key, 'big')
|
||||
|
||||
def __call__(self, key_two: bytes) -> int:
|
||||
if len(key_two) != constants.hash_length:
|
||||
raise ValueError(f"invalid length of key to compare: {len(key_two)}")
|
||||
val_key_two = int.from_bytes(key_two, 'big')
|
||||
return self.val_key_one ^ val_key_two
|
||||
|
||||
|
|
13
tests/unit/dht/protocol/test_distance.py
Normal file
13
tests/unit/dht/protocol/test_distance.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
import unittest
|
||||
from lbrynet.dht.protocol.distance import Distance
|
||||
|
||||
|
||||
class DistanceTests(unittest.TestCase):
|
||||
def test_invalid_key_length(self):
|
||||
self.assertRaises(ValueError, Distance, b'1' * 47)
|
||||
self.assertRaises(ValueError, Distance, b'1' * 49)
|
||||
self.assertRaises(ValueError, Distance, b'')
|
||||
|
||||
self.assertRaises(ValueError, Distance(b'0' * 48), b'1' * 47)
|
||||
self.assertRaises(ValueError, Distance(b'0' * 48), b'1' * 49)
|
||||
self.assertRaises(ValueError, Distance(b'0' * 48), b'')
|
Loading…
Reference in a new issue