diff --git a/README.md b/README.md index 7be5db0..e0b7087 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,12 @@ py-libp2p requires Python 3.6 and the best way to guarantee a clean Python 3.6 e 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 ``` \ No newline at end of file diff --git a/peer/peerdata.py b/peer/peerdata.py index 9a700d9..eec06cd 100644 --- a/peer/peerdata.py +++ b/peer/peerdata.py @@ -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) diff --git a/peer/peerdata_interface.py b/peer/peerdata_interface.py index dd4432d..b8e4e12 100644 --- a/peer/peerdata_interface.py +++ b/peer/peerdata_interface.py @@ -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 diff --git a/peer/peerstore.py b/peer/peerstore.py index 8db6a33..0745667 100644 --- a/peer/peerstore.py +++ b/peer/peerstore.py @@ -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 diff --git a/peer/peerstore_interface.py b/peer/peerstore_interface.py index 1f8d21c..0773621 100644 --- a/peer/peerstore_interface.py +++ b/peer/peerstore_interface.py @@ -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) diff --git a/tests/__init.py__ b/tests/__init__.py similarity index 100% rename from tests/__init.py__ rename to tests/__init__.py diff --git a/tests/peer/__init__.py b/tests/peer/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/peer/test_addrbook.py b/tests/peer/test_addrbook.py new file mode 100644 index 0000000..c67b74f --- /dev/null +++ b/tests/peer/test_addrbook.py @@ -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"]) diff --git a/tests/peer/test_peermetadata.py b/tests/peer/test_peermetadata.py new file mode 100644 index 0000000..afc9c46 --- /dev/null +++ b/tests/peer/test_peermetadata.py @@ -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" diff --git a/tests/peer/test_peerstore.py b/tests/peer/test_peerstore.py new file mode 100644 index 0000000..d0200ea --- /dev/null +++ b/tests/peer/test_peerstore.py @@ -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"]) diff --git a/tests/test_example.py b/tests/test_example.py index d61d9b6..41d6cb6 100644 --- a/tests/test_example.py +++ b/tests/test_example.py @@ -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