Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-41846: Add ability to reset storage class factory #910

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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