def setUp(self): self.ipfs = IpfsApi()
class TestIPFS(unittest.TestCase): """These test cases need a running daemon. Use a VM or a IPFS test node. Use the `DEBUG` class attribute and the decorator to skip tests or run only some. #todo: implement mocks where possible """ DEBUG = True # set this flag to skip tests while debugging the unit @classmethod def setUpClass(cls): cls.KEY1 = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' cls.KEY2 = 'QmR9MzChjp1MdFWik7NjEjqKQMzVmBkdK3dz14A6B5Cupm' cls.NODE = {'Data': b'Hello World'} def setUp(self): self.ipfs = IpfsApi() #@unittest.skipIf(DEBUG, "debug") def test_should_return_dict_of_root(self): """Use the IPFS class' id() method to retrieve the node's root""" resp = self.ipfs.id() test_fields = ('Addresses', 'ProtocolVersion', 'ID', 'PublicKey', ) self.assertTrue( all(k in resp.keys() for k in test_fields) ) # print(repr(resp)) #@unittest.skipIf(DEBUG, "debug") def test_should_return_node_configuration(self): """Use the ConfigAPI to return node's configuration""" resp = self.ipfs.config.show() # check integrity of configuration dict keys # #todo: @mec-is add subkeys and checks test_dict = { 'Identity': [], 'API': [], 'Datastore': [], 'Tour': [], 'Addresses': [], 'Swarm': [], 'SupernodeRouting': [], 'Version': [], 'Bootstrap': [], 'Gateway': [], 'Mounts': [], 'Ipns': [], 'Discovery': [] } #for k, v in resp.items(): #print(k, '>>>', v) self.assertTrue( all(k in resp.keys() for k in test_dict.keys()) ) # #todo: @jgraef add in a comment here requirements for this test case # #todo: i.e. constraints on certain value in resp dictionary or any other # #todo: possible heck to control integrity and consistency of the data if not trivial # print(repr(resp)) #@unittest.skipIf(DEBUG, "debug") def test_should_return_version_information(self): """Use the IPFS class' version() to return information on version, number of repo and commits""" resp = self.ipfs.version() # check integrity of configuration dict keys test_dict = { 'Version': str, 'Repo': str, 'Commit': str } # for k, v in resp.items(): # print(k, '>>>', str(type(v))) self.assertTrue( all(k in resp.keys() for k in test_dict.keys()) ) self.assertTrue( all(str(type(resp[k])) == str(test_dict[k]) for k in test_dict.keys()) ) # print(repr(resp)) @unittest.skipIf(DEBUG, "debug") def test_resolve(self): resp = self.ipfs.resolve(self.KEY2 + "/static") self.assertEqual(resp, {'Path': '/ipfs/QmP5BvrMtqWGirZYyHgz77zhEzLiJbonZVdHPMJRM1xe8G'}) def tearDown(self): del self.ipfs @classmethod def tearDownClass(cls): del cls
from ipfs.api import IpfsApi # connect to the IPFS daemon ipfs = IpfsApi() # Read a file # Many commands work on byte streams instead of bytes objects f = ipfs.file.cat("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB") data = f.read() # Since it's bytes but we want to print it as text, we need to decode it text = data.decode() # And print it print(text) # Don't forget to close the stream f.close()
class TestIPFS(unittest.TestCase): """These test cases need a running daemon. Use a VM or a IPFS test node. Use the `DEBUG` class attribute and the decorator to skip tests or run only some. #todo: implement mocks where possible """ DEBUG = True # set this flag to skip tests while debugging the unit @classmethod def setUpClass(cls): cls.KEY1 = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' cls.KEY2 = 'QmR9MzChjp1MdFWik7NjEjqKQMzVmBkdK3dz14A6B5Cupm' cls.NODE = {'Data': b'Hello World'} def setUp(self): self.ipfs = IpfsApi() #@unittest.skipIf(DEBUG, "debug") def test_should_return_dict_of_root(self): """Use the IPFS class' id() method to retrieve the node's root""" resp = self.ipfs.id() test_fields = ( 'Addresses', 'ProtocolVersion', 'ID', 'PublicKey', ) self.assertTrue(all(k in resp.keys() for k in test_fields)) # print(repr(resp)) #@unittest.skipIf(DEBUG, "debug") def test_should_return_node_configuration(self): """Use the ConfigAPI to return node's configuration""" resp = self.ipfs.config.show() # check integrity of configuration dict keys # #todo: @mec-is add subkeys and checks test_dict = { 'Identity': [], 'API': [], 'Datastore': [], 'Tour': [], 'Addresses': [], 'Swarm': [], 'SupernodeRouting': [], 'Version': [], 'Bootstrap': [], 'Gateway': [], 'Mounts': [], 'Ipns': [], 'Discovery': [] } #for k, v in resp.items(): #print(k, '>>>', v) self.assertTrue(all(k in resp.keys() for k in test_dict.keys())) # #todo: @jgraef add in a comment here requirements for this test case # #todo: i.e. constraints on certain value in resp dictionary or any other # #todo: possible heck to control integrity and consistency of the data if not trivial # print(repr(resp)) #@unittest.skipIf(DEBUG, "debug") def test_should_return_version_information(self): """Use the IPFS class' version() to return information on version, number of repo and commits""" resp = self.ipfs.version() # check integrity of configuration dict keys test_dict = {'Version': str, 'Repo': str, 'Commit': str} # for k, v in resp.items(): # print(k, '>>>', str(type(v))) self.assertTrue(all(k in resp.keys() for k in test_dict.keys())) self.assertTrue( all( str(type(resp[k])) == str(test_dict[k]) for k in test_dict.keys())) # print(repr(resp)) @unittest.skipIf(DEBUG, "debug") def test_resolve(self): resp = self.ipfs.resolve(self.KEY2 + "/static") self.assertEqual( resp, {'Path': '/ipfs/QmP5BvrMtqWGirZYyHgz77zhEzLiJbonZVdHPMJRM1xe8G'}) def tearDown(self): del self.ipfs @classmethod def tearDownClass(cls): del cls
def setUp(self): self.fs = UnixFs(IpfsApi())
from ipfs.api import IpfsApi from ipfs.unixfs import UnixFs fs = UnixFs(IpfsApi()) with fs.open("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB", "r") as f: for line in f.readlines(): print(line, end="")
def setUp(self): self.ipfs = IpfsApi() # # Testing Variables ################################################# # # This object holds correct responses' content self.tester = mock.MagicMock() self.tester.links = { 'Hash': 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', 'Links': [] } self.tester.chunk = b'\x08\x01' # `test_object_get`: the 'Links' value may change based on local ipfs instance # it's collected here for documenting the HTTP interface's response self.tester.full_object = { 'Data': self.tester.chunk, 'Links': [{ 'Size': 4118930, 'Name': 'bundle.js', 'Hash': 'QmdoDatULjkor1eA1YhBAjmKkkDr7AGEiTrANh7uK17Hfn' }, { 'Size': 4761372, 'Name': 'bundle.js.map', 'Hash': 'QmUVYznSyVB32u6jjCjcXmZb7byv832PUC3tuGAJg6SUQz' }, { 'Size': 485, 'Name': 'index.html', 'Hash': 'QmNh5CNBdFkVsALDqzU6AvbFAJd8LpjBV7voojQq95nKDA' }, { 'Size': 2506050, 'Name': 'static', 'Hash': 'QmP5BvrMtqWGirZYyHgz77zhEzLiJbonZVdHPMJRM1xe8G' }, { 'Size': 181436, 'Name': 'style.css', 'Hash': 'QmecBJMFtTsn4RawUcqFGudevEWcDUym4b6FtemLtKhZy7' }] } self.tester.default_node = { 'Links': [{ 'Size': 1688, 'Name': 'about', 'Hash': 'QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V' }, { 'Size': 200, 'Name': 'contact', 'Hash': 'QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y' }, { 'Size': 322, 'Name': 'help', 'Hash': 'QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7' }, { 'Size': 1707, 'Name': 'quick-start', 'Hash': 'QmXifYTiYxz8Nxt3LmjaxtQNLYkjdh324L4r81nZSadoST' }, { 'Size': 1102, 'Name': 'readme', 'Hash': 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' }, { 'Size': 1027, 'Name': 'security-notes', 'Hash': 'QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ' }], 'Data': b'\x08\x01' } self.tester.links = { 'Links': [], 'Hash': 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' } self.tester.node_put = { 'Links': [], 'Hash': 'QmXy2pAWQ3Ef1PqZqi4Z9TJnpDh1trdkCqAvzBgKNNRrSR' } # `test_object_stat`: same as for 'Links' above self.tester.stat_full = { 'CumulativeSize': 4, 'LinksSize': 2, 'Hash': 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn', 'NumLinks': 0, 'DataSize': 2, 'BlockSize': 4 }