Commits

codernity committed bc64a0d

Changed a bit textwrap behaviour with indented index codes. Also added tests to cover that situation.

Comments (0)

Files changed (2)

CodernityDB/database.py

 import os
 import io
 from inspect import getsource
-from textwrap import dedent
 from indexcreator import Parser
 
 # for custom indexes
         It will use :py:meth:`inspect.getsource` to get class source.
         Then it will build real index file, save it in ``_indexes`` directory.
         """
-        code = dedent(getsource(index.__class__))
+        code = getsource(index.__class__)
+        if not code.startswith('c'):  # fix for indented index codes
+            import textwrap
+            code = textwrap.dedent(code)
         index._order = i
         cls_code = getattr(index, 'classes_code', [])
         classes_code = ""
         return key.rjust(16, '_').lower()
 
 
+
 class DB_Tests:
 
     def setup_method(self, method):
         db.delete(doc)
         with pytest.raises(RecordNotFound):
             db.get('words', "Codern")
+
+    def test_add_indented_index(self, tmpdir):
+        class IndentedMd5Index(HashIndex):
+
+            def __init__(self, *args, **kwargs):
+                #kwargs['entry_line_format'] = '<32s32sIIcI'
+                kwargs['key_format'] = '16s'
+                kwargs['hash_lim'] = 4 * 1024
+                super(IndentedMd5Index, self).__init__(*args, **kwargs)
+
+            def make_key_value(self, data):
+                return md5(data['name']).digest(), None
+
+            def make_key(self, key):
+                return md5(key).digest()
+
+        db = self._db(os.path.join(str(tmpdir), 'db'))
+        db.create()
+        db.add_index(IndentedMd5Index(db.path, 'ind'))
+
+        db.insert(dict(name='a'))
+        assert db.get('ind', 'a', with_doc=True)['doc']['name'] == 'a'