From 9e9cffab33cf1a43879ba0d1d9671cec2447e5ba Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Tue, 17 May 2022 19:11:31 +0300 Subject: [PATCH] Started a test case for inventory, so far using a filesystem backend --- src/tests/test_inventory.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/tests/test_inventory.py diff --git a/src/tests/test_inventory.py b/src/tests/test_inventory.py new file mode 100644 index 00000000..9ecb576f --- /dev/null +++ b/src/tests/test_inventory.py @@ -0,0 +1,44 @@ +"""Tests for inventory""" + +import os +import shutil +import struct +import tempfile +import time +import unittest + +from pybitmessage.addresses import calculateInventoryHash + +from .partial import TestPartialRun + + +class TestFilesystemInventory(TestPartialRun): + """A test case for the inventory using filesystem backend""" + + @classmethod + def setUpClass(cls): + cls.home = os.environ['BITMESSAGE_HOME'] = tempfile.mkdtemp() + super(TestFilesystemInventory, cls).setUpClass() + + from inventory import create_inventory_instance + cls.inventory = create_inventory_instance('filesystem') + + def test_consistency(self): + """Ensure the inventory is of proper class""" + if os.path.isfile(os.path.join(self.home, 'messages.dat')): + # this will likely never happen + self.fail("Failed to configure filesystem inventory!") + + def test_appending(self): + """Add a sample message to the inventory""" + TTL = 24 * 60 * 60 + embedded_time = int(time.time() + TTL) + msg = struct.pack('>Q', embedded_time) + os.urandom(166) + invhash = calculateInventoryHash(msg) + self.inventory[invhash] = (2, 1, msg, embedded_time, b'') + + @classmethod + def tearDownClass(cls): + super(TestFilesystemInventory, cls).tearDownClass() + cls.inventory.flush() + shutil.rmtree(os.path.join(cls.home, cls.inventory.topDir))