Skip to content

Commit

Permalink
Merge pull request #910 from lsst/tickets/DM-41846
Browse files Browse the repository at this point in the history
DM-41846: Add ability to reset storage class factory
  • Loading branch information
timj authored Nov 27, 2023
2 parents 593d116 + d2c27c3 commit 224f3da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion python/lsst/daf/butler/_storage_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def __str__(self) -> str:
StorageClasses
--------------
{sep.join(f"{s}: {self._storageClasses[s]}" for s in self._storageClasses)}
{sep.join(f"{s}: {self._storageClasses[s]!r}" for s in sorted(self._storageClasses))}
"""

def __contains__(self, storageClassOrName: StorageClass | str) -> bool:
Expand Down Expand Up @@ -989,3 +989,13 @@ def _unregisterStorageClass(self, storageClassName: str) -> None:
functionality and it is not expected to be required for normal usage.
"""
del self._storageClasses[storageClassName]

def reset(self) -> None:
"""Remove all storage class entries from factory and reset to
initial state.
This is useful for test code where a known start state is useful.
"""
self._storageClasses.clear()
# Seed with the default config.
self.addFromConfig(StorageClassConfig())
6 changes: 6 additions & 0 deletions tests/test_storageClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ def testRegistry(self):
# Check you can silently insert something that is already there
factory.registerStorageClass(newclass3)

# Reset the factory and check that default items are present
# but the new ones are not.
factory.reset()
self.assertNotIn(newclass3.name, factory)
self.assertIn("StructuredDataDict", factory)

def testFactoryFind(self):
# Finding a storage class can involve doing lots of slow imports so
# this is a separate test.
Expand Down

0 comments on commit 224f3da

Please sign in to comment.