test_inventory.py
1 """Tests for inventory""" 2 3 import os 4 import shutil 5 import struct 6 import tempfile 7 import time 8 import unittest 9 10 import six 11 12 from pybitmessage import highlevelcrypto 13 from pybitmessage.storage import storage 14 15 from .partial import TestPartialRun 16 17 18 class TestFilesystemInventory(TestPartialRun): 19 """A test case for the inventory using filesystem backend""" 20 21 @classmethod 22 def setUpClass(cls): 23 cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.mkdtemp() 24 super(TestFilesystemInventory, cls).setUpClass() 25 26 from inventory import create_inventory_instance 27 cls.inventory = create_inventory_instance('filesystem') 28 29 def test_consistency(self): 30 """Ensure the inventory is of proper class""" 31 if os.path.isfile(os.path.join(self.home, 'messages.dat')): 32 # this will likely never happen 33 self.fail("Failed to configure filesystem inventory!") 34 35 def test_appending(self): 36 """Add a sample message to the inventory""" 37 TTL = 24 * 60 * 60 38 embedded_time = int(time.time() + TTL) 39 msg = struct.pack('>Q', embedded_time) + os.urandom(166) 40 invhash = highlevelcrypto.calculateInventoryHash(msg) 41 self.inventory[invhash] = (2, 1, msg, embedded_time, b'') 42 43 @classmethod 44 def tearDownClass(cls): 45 super(TestFilesystemInventory, cls).tearDownClass() 46 cls.inventory.flush() 47 shutil.rmtree(os.path.join(cls.home, cls.inventory.topDir)) 48 49 50 class TestStorageAbstract(unittest.TestCase): 51 """A test case for refactoring of the storage abstract classes""" 52 53 def test_inventory_storage(self): 54 """Check inherited abstract methods""" 55 with six.assertRaisesRegex( 56 self, TypeError, "^Can't instantiate abstract class.*" 57 "methods __contains__, __delitem__, __getitem__, __iter__," 58 " __len__, __setitem__" 59 ): # pylint: disable=abstract-class-instantiated 60 storage.InventoryStorage()