Skip to content

Commit

Permalink
Add getAllBrains method to the ZCatalog:
Browse files Browse the repository at this point in the history
Cherry picked from master.
  • Loading branch information
Michael Howitz committed Jan 29, 2019
1 parent 41da385 commit 7cb14dc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
7 changes: 5 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ Changelog
2.13.30 (unreleased)
--------------------

- Nothing changed yet.

- Add new `getAllBrains` method to the ZCatalog, returning a generator
of brains for all cataloged objects. You can use this if you relied
on `searchResults` returning all brains for empty queries before
version 4.0a2. This is a backport of this method in version 4.1 to ease the
migration.

2.13.29 (2017-10-13)
--------------------
Expand Down
6 changes: 6 additions & 0 deletions src/Products/ZCatalog/ZCatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ def getIndexDataForRID(self, rid):
# return the current index contents for the specific rid
return self._catalog.getIndexDataForRID(rid)

security.declareProtected(search_zcatalog, 'getAllBrains')
def getAllBrains(self):
# return a generator of brains for all cataloged objects
for rid in self._catalog.data:
yield self._catalog[rid]

security.declareProtected(search_zcatalog, 'schema')
def schema(self):
return self._catalog.schema.keys()
Expand Down
34 changes: 32 additions & 2 deletions src/Products/ZCatalog/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,39 @@ def setUp(self):
self._catalog.catalogObject(dummy(self.nums[x]), repr(x))
self._catalog = self._catalog.__of__(dummy('foo'))

# clear
def test_clear(self):
catalog = self._make_one()
self.assertTrue(len(catalog) > 0)
catalog.clear()
self.assertEqual(catalog._length(), 0)
self.assertEqual(len(catalog), 0)
self.assertEqual(len(catalog.data), 0)
self.assertEqual(len(catalog.paths), 0)
self.assertEqual(len(catalog.uids), 0)
for index_id in catalog.indexes:
index = catalog.getIndex(index_id)
self.assertEqual(index.numObjects(), 0)

def test_getitem(self):
def extra(catalog):
catalog.addColumn('att1')

catalog = self._make_one(extra=extra)
catalog_rids = set(catalog.data)
brain_class = catalog._v_result_class
brains = []
brain_rids = set()
for rid in catalog_rids:
brain = catalog[rid]
brains.append(brain)
brain_rids.add(brain.getRID())
self.assertIsInstance(brain, brain_class)
self.assertEqual(brain.att1, 'att1')

self.assertEqual(len(brains), len(catalog))
self.assertEqual(catalog_rids, brain_rids)

# updateBrains
# __getitem__
# __setstate__
# useBrains
# getIndex
Expand Down
10 changes: 10 additions & 0 deletions src/Products/ZCatalog/tests/test_zcatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ def testUpdateMetadata(self):

# getMetadataForRID
# getIndexDataForRID

def testGetAllBrains(self):
brain_class = self._catalog._catalog._v_result_class
brains = []
for brain in self._catalog.getAllBrains():
brains.append(brain)
self.assertIsInstance(brain, brain_class)
self.assertTrue(hasattr(brain, 'title'))
self.assertEqual(len(brains), len(self._catalog))

# schema
# indexes
# index_objects
Expand Down

0 comments on commit 7cb14dc

Please sign in to comment.