2021-06-01 11:37:32 +02:00
|
|
|
# standard imports
|
|
|
|
import unittest
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from chainqueue.fs.dir import HexDir
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class HexDirTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.dir = tempfile.mkdtemp()
|
2021-06-01 11:46:17 +02:00
|
|
|
self.hexdir = HexDir(os.path.join(self.dir, 'q'), 4, 3, 2)
|
2021-06-01 11:37:32 +02:00
|
|
|
logg.debug('setup hexdir root {}'.format(self.dir))
|
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.dir)
|
|
|
|
logg.debug('cleaned hexdir root {}'.format(self.dir))
|
|
|
|
|
|
|
|
|
|
|
|
def test_path(self):
|
|
|
|
content = b'cdef'
|
|
|
|
prefix = b'ab'
|
|
|
|
label = b'\xde\xad\xbe\xef'
|
|
|
|
self.hexdir.add(label, content, prefix=prefix)
|
2021-06-01 11:46:17 +02:00
|
|
|
file_path = os.path.join(self.dir, 'q', 'DE', 'AD', 'BE', label.hex().upper())
|
2021-06-01 11:37:32 +02:00
|
|
|
|
|
|
|
f = open(file_path, 'rb')
|
|
|
|
r = f.read()
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(content, r)
|
|
|
|
|
|
|
|
f = open(self.hexdir.master_file, 'rb')
|
|
|
|
r = f.read()
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(prefix + label, r)
|
|
|
|
|
|
|
|
|
|
|
|
def test_size(self):
|
|
|
|
content = b'cdef'
|
|
|
|
prefix = b'ab'
|
|
|
|
label = b'\xde\xad\xbe\xef'
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.hexdir.add(label, content, prefix=b'a')
|
|
|
|
|
|
|
|
|
2021-06-01 12:43:12 +02:00
|
|
|
def test_index(self):
|
|
|
|
self.hexdir.add(b'\xde\xad\xbe\xef', b'foo', b'ab')
|
|
|
|
self.hexdir.add(b'\xbe\xef\xfe\xed', b'bar', b'cd')
|
|
|
|
c = self.hexdir.add(b'\x01\x02\x03\x04', b'baz', b'ef')
|
|
|
|
self.assertEqual(c, 2)
|
|
|
|
|
|
|
|
|
2021-06-01 11:37:32 +02:00
|
|
|
def test_edit(self):
|
|
|
|
self.hexdir.add(b'\xde\xad\xbe\xef', b'foo', b'ab')
|
|
|
|
self.hexdir.add(b'\xbe\xef\xfe\xed', b'bar', b'cd')
|
|
|
|
self.hexdir.add(b'\x01\x02\x03\x04', b'baz', b'ef')
|
|
|
|
self.hexdir.set_prefix(1, b'ff')
|
|
|
|
|
|
|
|
f = open(self.hexdir.master_file, 'rb')
|
|
|
|
f.seek(6)
|
|
|
|
r = f.read(2)
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(b'ff', r)
|
|
|
|
|
|
|
|
|
2021-06-01 13:26:09 +02:00
|
|
|
def test_get(self):
|
|
|
|
self.hexdir.add(b'\xde\xad\xbe\xef', b'foo', b'ab')
|
|
|
|
self.hexdir.add(b'\xbe\xef\xfe\xed', b'bar', b'cd')
|
|
|
|
self.hexdir.add(b'\x01\x02\x03\x04', b'baz', b'ef')
|
|
|
|
(prefix, key) = self.hexdir.get(1)
|
|
|
|
self.assertEqual(b'\xbe\xef\xfe\xed', key)
|
|
|
|
self.assertEqual(b'cd', prefix)
|
|
|
|
|
|
|
|
|
2021-06-01 11:37:32 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|