codernity avatar 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'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.