-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add surgery to remove rid from UUIDIndex.
- Loading branch information
Showing
4 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
ftw/catalogdoctor/tests/test_surgery_remove_from_uuidindex.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from ftw.builder import Builder | ||
from ftw.builder import create | ||
from ftw.catalogdoctor.surgery import RemoveFromUUIDIndex | ||
from ftw.catalogdoctor.tests import FunctionalTestCase | ||
from ftw.catalogdoctor.utils import find_keys_pointing_to_rid | ||
|
||
|
||
class TestRemoveFromUUIDIndex(FunctionalTestCase): | ||
|
||
def setUp(self): | ||
super(TestRemoveFromUUIDIndex, self).setUp() | ||
|
||
self.grant('Contributor') | ||
self.folder = create(Builder('folder').titled(u'Foo')) | ||
|
||
def test_remove_healthy_object(self): | ||
rid = self.get_rid(self.folder) | ||
index = self.catalog.indexes['UID'] | ||
|
||
entries_pointing_to_rid = find_keys_pointing_to_rid(index._index, rid) | ||
self.assertEqual(1, len(entries_pointing_to_rid)) | ||
self.assertIn(rid, index._unindex) | ||
self.assertEqual(1, len(index)) | ||
|
||
surgery = RemoveFromUUIDIndex(index, rid) | ||
surgery.perform() | ||
|
||
entries_pointing_to_rid = find_keys_pointing_to_rid(index._index, rid) | ||
self.assertEqual(0, len(entries_pointing_to_rid)) | ||
self.assertNotIn(rid, index._unindex) | ||
self.assertEqual(0, len(index)) | ||
|
||
def test_remove_from_reverse_index_only(self): | ||
rid = self.get_rid(self.folder) | ||
index = self.catalog.indexes['UID'] | ||
|
||
entries_pointing_to_rid = find_keys_pointing_to_rid(index._index, rid) | ||
del index._index[entries_pointing_to_rid[0]] | ||
index._length.change(-1) | ||
|
||
self.assertIn(rid, index._unindex) | ||
self.assertEqual(1, len(index._unindex)) | ||
|
||
surgery = RemoveFromUUIDIndex(index, rid) | ||
surgery.perform() | ||
|
||
entries_pointing_to_rid = find_keys_pointing_to_rid(index._index, rid) | ||
self.assertEqual(0, len(entries_pointing_to_rid)) | ||
self.assertNotIn(rid, index._unindex) | ||
self.assertEqual(0, len(index)) | ||
|
||
def test_remove_from_forward_index_only(self): | ||
rid = self.get_rid(self.folder) | ||
index = self.catalog.indexes['UID'] | ||
|
||
del index._unindex[rid] | ||
|
||
entries_pointing_to_rid = find_keys_pointing_to_rid(index._index, rid) | ||
self.assertEqual(1, len(entries_pointing_to_rid)) | ||
self.assertEqual(1, len(index._index)) | ||
|
||
surgery = RemoveFromUUIDIndex(index, rid) | ||
surgery.perform() | ||
|
||
entries_pointing_to_rid = find_keys_pointing_to_rid(index._index, rid) | ||
self.assertEqual(0, len(entries_pointing_to_rid)) | ||
self.assertNotIn(rid, index._unindex) | ||
self.assertEqual(0, len(index)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
def find_keys_pointing_to_rid(dictish, rid): | ||
"""Return all entries in dictish item pointing to rid.""" | ||
"""Return all entries in dictish item pointing to rid. | ||
The values in dictish can be either the rid as a single value or a | ||
collection containing the rid. | ||
""" | ||
return [ | ||
key for key, rids_for_key in dictish.items() | ||
if rid in rids_for_key | ||
key for key, rids_or_rid in dictish.items() | ||
if contains_or_equals_rid(rid, rids_or_rid) | ||
] | ||
|
||
|
||
def contains_or_equals_rid(rid, rids_or_rid): | ||
"""Return whether rids_or_rid contains or equals a rid.""" | ||
|
||
try: | ||
return rid in rids_or_rid | ||
except TypeError: | ||
return rid == rids_or_rid |