Commits

codernity committed df7b43c

Fixed issue #5. Improved test scenario to cover it.

Comments (0)

Files changed (2)

CodernityDB/database.py

         else:
             return ind_obj
 
+    def __check_if_index_unique(self, name, num):
+        indexes = os.listdir(os.path.join(self.path, '_indexes'))
+        if any((x for x in indexes if x[2:-3] == name and x[:2] != str(num))):
+            raise IndexConflict("Already exists")
+
     def __write_index(self, new_index, number=0, edit=False, ind_kwargs=None):
         #print new_index
         if ind_kwargs is None:
                 raise PreconditionsException(
                     "Id index must be the first added")
             ind_path = "%.2d%s" % (number, name)
+            if not edit:
+                self.__check_if_index_unique(name, number)
 
             with io.FileIO(os.path.join(p, ind_path + '.py'), 'w') as f:
                 f.write(new_index)
             if number == 0 and not edit and not ind.name == 'id':
                 raise PreconditionsException(
                     "Id index must be the first added")
+            if not edit:
+                self.__check_if_index_unique(ind.name, number)
             self._add_single_index(p, number, ind)
             ind_path = "%.2d%s" % (number, ind.name)
             ind_obj = self._read_index_single(p, ind_path + '.py', ind_kwargs)
 from CodernityDB.database import DatabaseException, RevConflict, DatabasePathException, DatabaseConflict, PreconditionsException
 
 from CodernityDB.hash_index import HashIndex, UniqueHashIndex
-from CodernityDB.index import IndexException, TryReindexException, IndexNotFoundException, IndexPreconditionsException
+from CodernityDB.index import IndexException, TryReindexException, IndexNotFoundException, IndexPreconditionsException, IndexConflict
 
 from CodernityDB.tree_index import TreeBasedIndex
 
         db = self._db(p)
         db.create()
         db2 = self._db(p)
-        with pytest.raises(DatabaseConflict):
+        with pytest.raises((DatabaseConflict, IndexConflict)):
             db2.create()
+        db2.open()
         db.destroy()
         db2 = self._db(p)
         db.create()
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.