Skip to content

Commit

Permalink
Merge pull request #54 from lsst/tickets/DM-15158
Browse files Browse the repository at this point in the history
Add tests of DataIdContainer and improve exception handling
  • Loading branch information
parejkoj committed Jul 19, 2018
2 parents 53a6ac1 + 79dca0a commit 5ecaacd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
6 changes: 6 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2016-2018 Association of Universities for Research in Astronomy
2015-2018 University of Washington
2015-2018 The Trustees of Princeton University
2017-2018 The Board of Trustees of the Leland Stanford Junior University, through SLAC National Accelerator Laboratory
2016 University of Illinois Champaign-Urbana
2011-2015 LSST Corporation
5 changes: 3 additions & 2 deletions python/lsst/pipe/base/argumentParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ def castDataIds(self, butler):
raise RuntimeError("Must call setDatasetType first")
try:
idKeyTypeDict = butler.getKeys(datasetType=self.datasetType, level=self.level)
except KeyError:
raise KeyError("Cannot get keys for datasetType %s at level %s" % (self.datasetType, self.level))
except KeyError as e:
msg = "Cannot get keys for datasetType %s at level %s: %s" % (self.datasetType, self.level)
raise KeyError(msg) from e

for dataDict in self.idList:
for key, strVal in dataDict.items():
Expand Down
66 changes: 66 additions & 0 deletions tests/test_dataIdContainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This file is part of pipe_base.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""Tests of the DataIdContainer class."""

import unittest
import unittest.mock

import lsst.daf.persistence
import lsst.utils.tests
import lsst.pipe.base as pipeBase


class DataIdContainerTestCase(lsst.utils.tests.TestCase):
def setUp(self):
self.container = pipeBase.DataIdContainer()
self.butler = unittest.mock.MagicMock(spec=lsst.daf.persistence.Butler)

def test_castDataIdsNoneDatasetType(self):
"""Raise RuntimeError if we haven't set container.datasetType."""
with self.assertRaises(RuntimeError):
self.container.castDataIds(self.butler)

def test_makeDataRefListNoneDatasetType(self):
"""Raise RuntimeError if we haven't set container.datasetType."""
with self.assertRaises(RuntimeError):
self.container.makeDataRefList(self.butler)

def test_castDataIdsRaiseKeyError(self):
"""Test that castDataIds re-raises a butler.getKeys() exception."""
self.container.setDatasetType("nonsense!")
self.butler.getKeys.side_effect = KeyError
with self.assertRaises(KeyError) as cm:
self.container.castDataIds(self.butler)
self.assertIsInstance(cm.exception.__cause__, KeyError)


class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
pass


def setup_module(module):
lsst.utils.tests.init()


if __name__ == "__main__":
lsst.utils.tests.init()
unittest.main()

0 comments on commit 5ecaacd

Please sign in to comment.