Skip to content

Commit

Permalink
Test surgery lookup by doctor is correct.
Browse files Browse the repository at this point in the history
Also improve testing and add another assertion here and there.
  • Loading branch information
deiferni committed Jul 8, 2019
1 parent ff140bd commit 9f2195b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
8 changes: 6 additions & 2 deletions ftw/catalogdoctor/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ def grant(self, *roles):
setRoles(self.portal, TEST_USER_ID, list(roles))
transaction.commit()

def get_catalog_indexdata(self, obj):
def get_catalog_indexdata(self, obj, omit_empty=False):
"""Return the catalog index data for an object as dict.
"""
self.maybe_process_indexing_queue()
return self.portal_catalog.getIndexDataForRID(self.get_rid(obj))
index_data = self.portal_catalog.getIndexDataForRID(self.get_rid(obj))
if omit_empty:
index_data = dict((key, value) for key, value in index_data.items()
if value)
return index_data

def get_catalog_metadata(self, obj):
"""Return the catalog metadata for an object as dict.
Expand Down
58 changes: 40 additions & 18 deletions ftw/catalogdoctor/tests/test_surgery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ftw.builder import Builder
from ftw.builder import create
from ftw.catalogdoctor.exceptions import CantPerformSurgery
from ftw.catalogdoctor.surgery import CatalogDoctor
from ftw.catalogdoctor.surgery import ReindexMissingUUID
from ftw.catalogdoctor.surgery import RemoveExtraRid
from ftw.catalogdoctor.surgery import RemoveOrphanedRid
Expand Down Expand Up @@ -28,6 +29,7 @@ def test_surgery_remove_extra_rid_with_partial_uuid(self):

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]

self.assertEqual(
Expand All @@ -40,8 +42,9 @@ def test_surgery_remove_extra_rid_with_partial_uuid(self):
),
result.get_symptoms(unhealthy_rid.rid))

surgery = RemoveExtraRid(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveExtraRid, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())
Expand All @@ -51,6 +54,7 @@ def test_surgery_remove_extra_rid_without_partial_uuid(self):

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]

self.assertEqual(
Expand All @@ -61,8 +65,9 @@ def test_surgery_remove_extra_rid_without_partial_uuid(self):
),
unhealthy_rid.catalog_symptoms)

surgery = RemoveExtraRid(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveExtraRid, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())
Expand All @@ -75,6 +80,7 @@ def test_surgery_remove_orphaned_rid_not_in_indexes(self):

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]

self.assertEqual(
Expand All @@ -85,8 +91,9 @@ def test_surgery_remove_orphaned_rid_not_in_indexes(self):
),
result.get_symptoms(unhealthy_rid.rid))

surgery = RemoveOrphanedRid(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveOrphanedRid, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())
Expand All @@ -96,6 +103,7 @@ def test_surgery_remove_orphaned_rid_in_indexes(self):

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]

self.assertEqual(
Expand All @@ -108,8 +116,9 @@ def test_surgery_remove_orphaned_rid_in_indexes(self):
),
result.get_symptoms(unhealthy_rid.rid))

surgery = RemoveOrphanedRid(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveOrphanedRid, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())
Expand All @@ -119,6 +128,7 @@ def test_surgery_reindex_missing_uuid(self):

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]

self.assertEqual(
Expand All @@ -128,8 +138,9 @@ def test_surgery_reindex_missing_uuid(self):
),
result.get_symptoms(unhealthy_rid.rid))

surgery = ReindexMissingUUID(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(ReindexMissingUUID, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())
Expand All @@ -146,6 +157,7 @@ def test_surgery_remove_object_moved_into_parent_and_found_via_acquisition_abort

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]
self.assertEqual(rid, unhealthy_rid.rid)
self.assertEqual(
Expand All @@ -155,9 +167,10 @@ def test_surgery_remove_object_moved_into_parent_and_found_via_acquisition_abort
),
result.get_symptoms(unhealthy_rid.rid))

surgery = RemoveRidOrReindexObject(self.catalog, unhealthy_rid)
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveRidOrReindexObject, doctor.get_surgery())
with self.assertRaises(CantPerformSurgery):
surgery.perform()
doctor.perform_surgery()

def test_surgery_remove_object_moved_into_parent_and_found_via_acquisition(self):
grandchild = create(Builder('folder')
Expand All @@ -179,6 +192,7 @@ def test_surgery_remove_object_moved_into_parent_and_found_via_acquisition(self)

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]
self.assertEqual(rid, unhealthy_rid.rid)
self.assertEqual(
Expand All @@ -188,17 +202,21 @@ def test_surgery_remove_object_moved_into_parent_and_found_via_acquisition(self)
),
result.get_symptoms(unhealthy_rid.rid))

surgery = RemoveRidOrReindexObject(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveRidOrReindexObject, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())

def test_surgery_add_dropped_object_to_indices(self):
self.drop_object_from_catalog_indexes(self.parent)
self.assertEqual(
{}, self.get_catalog_indexdata(self.parent, omit_empty=True))

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]
self.assertEqual(
(
Expand All @@ -207,8 +225,9 @@ def test_surgery_add_dropped_object_to_indices(self):
),
result.get_symptoms(unhealthy_rid.rid))

surgery = RemoveRidOrReindexObject(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveRidOrReindexObject, doctor.get_surgery())
doctor.perform_surgery()

result = self.run_healthcheck()
self.assertTrue(result.is_healthy())
Expand Down Expand Up @@ -252,16 +271,19 @@ def test_surgery_remove_untraversable_object_from_catalog(self):

result = self.run_healthcheck()
self.assertFalse(result.is_healthy())
self.assertEqual(1, len(result.get_unhealthy_rids()))
unhealthy_rid = result.get_unhealthy_rids()[0]
self.assertEqual(rid, unhealthy_rid.rid)
self.assertEqual(
(
'in_catalog_not_in_uuid_index',
'in_catalog_not_in_uuid_unindex',
),
unhealthy_rid.catalog_symptoms)

surgery = RemoveRidOrReindexObject(self.catalog, unhealthy_rid)
surgery.perform()
doctor = CatalogDoctor(self.catalog, unhealthy_rid)
self.assertIs(RemoveRidOrReindexObject, doctor.get_surgery())
doctor.perform_surgery()

self.assertEqual(1, len(self.catalog))
self.assertNotIn(rid, self.catalog.paths)
Expand Down

0 comments on commit 9f2195b

Please sign in to comment.