Peer tests and minor peer features/bug fixes.
This commit is contained in:
parent
330ed8363a
commit
db6b846251
|
@ -9,3 +9,11 @@ virtualenv -p python3.6 venv
|
|||
. venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
After installing our requirements (see above), you can:
|
||||
```sh
|
||||
cd tests
|
||||
pytest
|
||||
```
|
|
@ -13,6 +13,9 @@ class PeerData(IPeerData):
|
|||
def add_protocols(self, protocols):
|
||||
self.protocols.extend(protocols)
|
||||
|
||||
def set_protocols(self, protocols):
|
||||
self.protocols = protocols
|
||||
|
||||
def add_addrs(self, addrs):
|
||||
self.addrs.extend(addrs)
|
||||
|
||||
|
|
|
@ -2,48 +2,62 @@ from abc import ABC, abstractmethod
|
|||
|
||||
class IPeerData(ABC):
|
||||
|
||||
"""
|
||||
:return: all protocols associated with given peer
|
||||
"""
|
||||
@abstractmethod
|
||||
def get_protocols(self):
|
||||
"""
|
||||
:return: all protocols associated with given peer
|
||||
"""
|
||||
pass
|
||||
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
@abstractmethod
|
||||
def add_protocols(self, protocols):
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
pass
|
||||
|
||||
"""
|
||||
:param addrs: multiaddresses to add
|
||||
"""
|
||||
@abstractmethod
|
||||
def set_protocols(self, protocols):
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_addrs(self, addrs):
|
||||
"""
|
||||
:param addrs: multiaddresses to add
|
||||
"""
|
||||
pass
|
||||
|
||||
"""
|
||||
:return: all multiaddresses
|
||||
"""
|
||||
@abstractmethod
|
||||
def get_addrs(self):
|
||||
"""
|
||||
:return: all multiaddresses
|
||||
"""
|
||||
pass
|
||||
|
||||
"""
|
||||
Clear all addresses
|
||||
"""
|
||||
@abstractmethod
|
||||
def clear_addrs(self):
|
||||
"""
|
||||
Clear all addresses
|
||||
"""
|
||||
pass
|
||||
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:param val: val to associate with key
|
||||
:raise Exception: unsuccesful put
|
||||
"""
|
||||
@abstractmethod
|
||||
def put_metadata(self, key, val):
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:param val: val to associate with key
|
||||
:raise Exception: unsuccesful put
|
||||
"""
|
||||
pass
|
||||
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:return: val for key
|
||||
:raise Exception: key not found
|
||||
"""
|
||||
@abstractmethod
|
||||
def get_metadata(self, key):
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:return: val for key
|
||||
:raise Exception: key not found
|
||||
"""
|
||||
pass
|
||||
|
|
|
@ -38,6 +38,10 @@ class PeerStore(IPeerStore):
|
|||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.add_protocols(protocols)
|
||||
|
||||
def set_protocols(self, peer_id, protocols):
|
||||
peer = self.__create_or_get_peer(peer_id)
|
||||
peer.set_protocols(protocols)
|
||||
|
||||
def peers(self):
|
||||
return self.peer_map.keys()
|
||||
|
||||
|
@ -55,7 +59,7 @@ class PeerStore(IPeerStore):
|
|||
peer.put_metadata(key, val)
|
||||
|
||||
def add_addr(self, peer_id, addr, ttl):
|
||||
self.add_addrs(self, peer_id, [addr])
|
||||
self.add_addrs(peer_id, [addr], ttl)
|
||||
|
||||
def add_addrs(self, peer_id, addrs, ttl):
|
||||
# Ignore ttl for now
|
||||
|
|
|
@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
|
|||
from .addrbook_interface import IAddrBook
|
||||
from .peermetadata_interface import IPeerMetadata
|
||||
|
||||
class IPeerStore(ABC, IAddrBook, IPeerMetadata):
|
||||
class IPeerStore(IAddrBook, IPeerMetadata):
|
||||
|
||||
def __init__(self):
|
||||
IPeerMetadata.__init__(self)
|
||||
|
|
0
tests/peer/__init__.py
Normal file
0
tests/peer/__init__.py
Normal file
59
tests/peer/test_addrbook.py
Normal file
59
tests/peer/test_addrbook.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
import pytest
|
||||
|
||||
from peer.peerstore import PeerStoreError
|
||||
from peer.peerstore import PeerStore
|
||||
|
||||
# Testing methods from IAddrBook base class.
|
||||
|
||||
|
||||
def test_addrs_empty():
|
||||
with pytest.raises(PeerStoreError):
|
||||
store = PeerStore()
|
||||
val = store.addrs("peer")
|
||||
assert not val
|
||||
|
||||
|
||||
def test_add_addr_single():
|
||||
store = PeerStore()
|
||||
store.add_addr("peer1", "/foo", 10)
|
||||
store.add_addr("peer1", "/bar", 10)
|
||||
store.add_addr("peer2", "/baz", 10)
|
||||
|
||||
assert store.addrs("peer1") == ["/foo", "/bar"]
|
||||
assert store.addrs("peer2") == ["/baz"]
|
||||
|
||||
|
||||
def test_add_addrs_multiple():
|
||||
store = PeerStore()
|
||||
store.add_addrs("peer1", ["/foo1", "/bar1"], 10)
|
||||
store.add_addrs("peer2", ["/foo2"], 10)
|
||||
|
||||
assert store.addrs("peer1") == ["/foo1", "/bar1"]
|
||||
assert store.addrs("peer2") == ["/foo2"]
|
||||
|
||||
|
||||
def test_clear_addrs():
|
||||
store = PeerStore()
|
||||
store.add_addrs("peer1", ["/foo1", "/bar1"], 10)
|
||||
store.add_addrs("peer2", ["/foo2"], 10)
|
||||
store.clear_addrs("peer1")
|
||||
|
||||
assert store.addrs("peer1") == []
|
||||
assert store.addrs("peer2") == ["/foo2"]
|
||||
|
||||
store.add_addrs("peer1", ["/foo1", "/bar1"], 10)
|
||||
|
||||
assert store.addrs("peer1") == ["/foo1", "/bar1"]
|
||||
|
||||
|
||||
def test_peers_with_addrs():
|
||||
store = PeerStore()
|
||||
store.add_addrs("peer1", [], 10)
|
||||
store.add_addrs("peer2", ["/foo"], 10)
|
||||
store.add_addrs("peer3", ["/bar"], 10)
|
||||
|
||||
assert set(store.peers_with_addrs()) == set(["peer2", "peer3"])
|
||||
|
||||
store.clear_addrs("peer2")
|
||||
|
||||
assert set(store.peers_with_addrs()) == set(["peer3"])
|
44
tests/peer/test_peermetadata.py
Normal file
44
tests/peer/test_peermetadata.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
import pytest
|
||||
|
||||
from peer.peerstore import PeerStoreError
|
||||
from peer.peerstore import PeerStore
|
||||
|
||||
# Testing methods from IPeerMetadata base class.
|
||||
|
||||
|
||||
def test_get_empty():
|
||||
with pytest.raises(PeerStoreError):
|
||||
store = PeerStore()
|
||||
val = store.get("peer", "key")
|
||||
assert not val
|
||||
|
||||
|
||||
def test_put_get_simple():
|
||||
store = PeerStore()
|
||||
store.put("peer", "key", "val")
|
||||
assert store.get("peer", "key") == "val"
|
||||
|
||||
|
||||
def test_put_get_update():
|
||||
store = PeerStore()
|
||||
store.put("peer", "key1", "val1")
|
||||
store.put("peer", "key2", "val2")
|
||||
store.put("peer", "key2", "new val2")
|
||||
|
||||
assert store.get("peer", "key1") == "val1"
|
||||
assert store.get("peer", "key2") == "new val2"
|
||||
|
||||
|
||||
def test_put_get_two_peers():
|
||||
store = PeerStore()
|
||||
store.put("peer1", "key1", "val1")
|
||||
store.put("peer2", "key1", "val1 prime")
|
||||
|
||||
assert store.get("peer1", "key1") == "val1"
|
||||
assert store.get("peer2", "key1") == "val1 prime"
|
||||
|
||||
# Try update
|
||||
store.put("peer2", "key1", "new val1")
|
||||
|
||||
assert store.get("peer1", "key1") == "val1"
|
||||
assert store.get("peer2", "key1") == "new val1"
|
61
tests/peer/test_peerstore.py
Normal file
61
tests/peer/test_peerstore.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
import pytest
|
||||
|
||||
from peer.peerstore import PeerStoreError
|
||||
from peer.peerstore import PeerStore
|
||||
|
||||
# Testing methods from IPeerStore base class.
|
||||
|
||||
|
||||
def test_peer_info_empty():
|
||||
store = PeerStore()
|
||||
info = store.peer_info("peer")
|
||||
|
||||
assert not info
|
||||
|
||||
|
||||
def test_peer_info_basic():
|
||||
store = PeerStore()
|
||||
store.add_addr("peer", "/foo", 10)
|
||||
info = store.peer_info("peer")
|
||||
|
||||
assert info.peer_id == "peer"
|
||||
assert info.addrs == ["/foo"]
|
||||
|
||||
|
||||
def test_add_get_protocols_basic():
|
||||
store = PeerStore()
|
||||
store.add_protocols("peer1", ["p1", "p2"])
|
||||
store.add_protocols("peer2", ["p3"])
|
||||
|
||||
assert set(store.get_protocols("peer1")) == set(["p1", "p2"])
|
||||
assert set(store.get_protocols("peer2")) == set(["p3"])
|
||||
|
||||
|
||||
def test_add_get_protocols_extend():
|
||||
store = PeerStore()
|
||||
store.add_protocols("peer1", ["p1", "p2"])
|
||||
store.add_protocols("peer1", ["p3"])
|
||||
|
||||
assert set(store.get_protocols("peer1")) == set(["p1", "p2", "p3"])
|
||||
|
||||
|
||||
def test_set_protocols():
|
||||
store = PeerStore()
|
||||
store.add_protocols("peer1", ["p1", "p2"])
|
||||
store.add_protocols("peer2", ["p3"])
|
||||
|
||||
store.set_protocols("peer1", ["p4"])
|
||||
store.set_protocols("peer2", [])
|
||||
|
||||
assert set(store.get_protocols("peer1")) == set(["p4"])
|
||||
assert set(store.get_protocols("peer2")) == set([])
|
||||
|
||||
|
||||
# Test with methods from other Peer interfaces.
|
||||
def test_peers():
|
||||
store = PeerStore()
|
||||
store.add_protocols("peer1", [])
|
||||
store.put("peer2", "key", "val")
|
||||
store.add_addr("peer3", "/foo", 10)
|
||||
|
||||
assert set(store.peers()) == set(["peer1", "peer2", "peer3"])
|
|
@ -3,7 +3,7 @@ import pytest
|
|||
@pytest.mark.parametrize("test_input,expected", [
|
||||
("3+5", 8),
|
||||
("2+4", 6),
|
||||
("6*9", 42),
|
||||
("6*9", 54),
|
||||
])
|
||||
def test_eval(test_input, expected):
|
||||
assert eval(test_input) == expected
|
||||
|
|
Loading…
Reference in New Issue
Block a user