leveldir

Multi-level directory structure data stores in python3
git clone git://git.defalsify.org/python-leveldir.git
Log | Files | Refs | LICENSE

test_hexdir.py (2972B)


      1 # standard imports
      2 import unittest
      3 import tempfile
      4 import shutil
      5 import logging
      6 import os
      7 
      8 # local imports
      9 from leveldir.hex import HexDir
     10 
     11 logging.basicConfig(level=logging.DEBUG)
     12 logg = logging.getLogger()
     13 
     14 
     15 class HexDirTest(unittest.TestCase):
     16 
     17     def setUp(self):
     18         self.dir = tempfile.mkdtemp() 
     19         self.hexdir = HexDir(os.path.join(self.dir, 'q'), 4, 3, 2)
     20         logg.debug('setup hexdir root {}'.format(self.dir))
     21       
     22 
     23     def tearDown(self):
     24         shutil.rmtree(self.dir)
     25         logg.debug('cleaned hexdir root {}'.format(self.dir))
     26 
     27 
     28     def test_path(self):
     29         content = b'cdef'
     30         prefix = b'ab'
     31         label = b'\xde\xad\xbe\xef'
     32         (c, entry_path) = self.hexdir.add(label, content, prefix=prefix)
     33         
     34         file_path = os.path.join(self.dir, 'q', 'DE', 'AD', 'BE', label.hex().upper())
     35         self.assertEqual(file_path, entry_path)
     36         
     37         f = open(file_path, 'rb')
     38         r = f.read()
     39         f.close()
     40         self.assertEqual(content, r)
     41 
     42         f = open(self.hexdir.master_file, 'rb')
     43         r = f.read()
     44         f.close()
     45         self.assertEqual(prefix + label, r)
     46 
     47 
     48     def test_size(self):
     49         content = b'cdef'
     50         prefix = b'ab'
     51         label = b'\xde\xad\xbe\xef'
     52         with self.assertRaises(ValueError):
     53             self.hexdir.add(label, content, prefix=b'a')
     54 
     55 
     56     def test_index(self):
     57         self.hexdir.add(b'\xde\xad\xbe\xef', b'foo', b'ab')
     58         self.hexdir.add(b'\xbe\xef\xfe\xed', b'bar', b'cd')
     59         (c, entry_path) = self.hexdir.add(b'\x01\x02\x03\x04', b'baz', b'ef')
     60         self.assertEqual(c, 2)
     61 
     62 
     63     def test_edit(self):
     64         self.hexdir.add(b'\xde\xad\xbe\xef', b'foo', b'ab')
     65         self.hexdir.add(b'\xbe\xef\xfe\xed', b'bar', b'cd')
     66         self.hexdir.add(b'\x01\x02\x03\x04', b'baz', b'ef')
     67         self.hexdir.set_prefix(1, b'ff')
     68 
     69         f = open(self.hexdir.master_file, 'rb')
     70         f.seek(6)
     71         r = f.read(2)
     72         f.close()
     73         self.assertEqual(b'ff', r)
     74 
     75 
     76     def test_get(self):
     77         self.hexdir.add(b'\xde\xad\xbe\xef', b'foo', b'ab')
     78         self.hexdir.add(b'\xbe\xef\xfe\xed', b'bar', b'cd')
     79         self.hexdir.add(b'\x01\x02\x03\x04', b'baz', b'ef')
     80         (prefix, key) = self.hexdir.get(1)
     81         self.assertEqual(b'\xbe\xef\xfe\xed', key)
     82         self.assertEqual(b'cd', prefix)
     83 
     84 
     85 class HexDirTestFormatter(unittest.TestCase):
     86 
     87     def setUp(self):
     88         def lower_formatter(hx):
     89             return hx.lower()
     90         self.dir = tempfile.mkdtemp() 
     91         self.hexdir = HexDir(os.path.join(self.dir, 'q'), 4, 3, 2, formatter=lower_formatter)
     92         logg.debug('setup hexdir root {}'.format(self.dir))
     93 
     94 
     95     def test_format(self):
     96         self.hexdir.add(b'\xaa\xbb\xcc\xdd', b'foo', b'ab')
     97         (prefix, key) = self.hexdir.get(1)
     98         checkdir_path = os.path.join(self.hexdir.path, 'aa', 'bb', 'cc', 'aabbccdd')
     99         os.stat(checkdir_path)
    100         
    101 
    102 if __name__ == '__main__':
    103     unittest.main()