Skip to content

Commit

Permalink
Use TestCase.id in makeTestCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfindeisen committed Apr 12, 2021
1 parent 0c8a5d4 commit 9c710f1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions doc/lsst.daf.butler/use-in-tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ Individual tests can be partly isolated using the `lsst.daf.butler.tests.makeTes
Once a test collection is created, datasets can be read or written as usual:

.. code-block:: py
:emphasize-lines: 1, 4
:emphasize-lines: 2, 5
butler = butlerTests.makeTestCollection(repo)
# Assuming self is a unittest.TestCase object
butler = butlerTests.makeTestCollection(repo, uniqueId=self.id())
exposure = makeTestExposure() # user-defined code
butler.put(exposure, "calexp", dataId)
Expand Down
13 changes: 9 additions & 4 deletions python/lsst/daf/butler/tests/_testRepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,17 @@ def makeTestRepo(root: str,
return butler


def makeTestCollection(repo: Butler) -> Butler:
def makeTestCollection(repo: Butler, uniqueId: Optional[str] = None) -> Butler:
"""Create a read/write Butler to a fresh collection.
Parameters
----------
repo : `lsst.daf.butler.Butler`
A previously existing Butler to a repository, such as that returned by
`~lsst.daf.butler.Butler.makeRepo` or `makeTestRepo`.
uniqueId : `str`, optional
A collection ID guaranteed by external code to be unique across all
calls to ``makeTestCollection`` for the same repository.
Returns
-------
Expand All @@ -137,9 +140,11 @@ def makeTestCollection(repo: Butler) -> Butler:
isolated test area, and not for repositories intended for real data
processing or analysis.
"""
# Create a "random" collection name
# Speed matters more than cryptographic guarantees
collection = "test" + str(random.randrange(1_000_000_000))
if not uniqueId:
# Create a "random" collection name
# Speed matters more than cryptographic guarantees
uniqueId = str(random.randrange(1_000_000_000))
collection = "test" + uniqueId
return Butler(butler=repo, run=collection)


Expand Down
3 changes: 2 additions & 1 deletion tests/test_testRepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def tearDownClass(cls):
removeTestTempDir(cls.root)

def setUp(self):
self.butler = makeTestCollection(self.creatorButler)
# TestCase.id is unique for each test
self.butler = makeTestCollection(self.creatorButler, uniqueId=self.id())

def testButlerValid(self):
self.butler.validateConfiguration()
Expand Down

0 comments on commit 9c710f1

Please sign in to comment.