Skip to content

Commit

Permalink
Modernize and clean tests
Browse files Browse the repository at this point in the history
All the tests in obs_subaru have been cleaned to make them runnable
under the py.test framework. The tests have also been purged of all
deprecated usage of assert_, assertEquals, etc... The decorator to skip
tests in testRepository has been replaced with the unittest provided
skipIf decorator.
  • Loading branch information
AstroVPK committed Aug 25, 2016
1 parent cb2ff32 commit f68454c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ _build.*
.sconsign.dblite
config.log
tests/.tests
tests/.cache/*
ups/*.cfgc
python/lsst/obs/subaru/version.py
python/lsst/obs/hsc/hscLib.py
Expand Down
29 changes: 11 additions & 18 deletions tests/testCamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# LSST Data Management System
#
# Copyright 2008-2015 AURA/LSST.
# Copyright 2008-2016 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
Expand All @@ -21,16 +21,15 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#

import unittest
from collections import namedtuple

import lsst.utils.tests as utilsTests
import lsst.utils.tests
from lsst.afw.image import Filter
from lsst.obs.hsc import HscMapper


class CameraTestCase(utilsTests.TestCase):
class CameraTestCase(lsst.utils.tests.TestCase):

def setUp(self):
self.mapper = HscMapper(root=".")
Expand Down Expand Up @@ -65,25 +64,19 @@ def testFilters(self):
FilterName(alias="HSC-Y", canonical="y"),
FilterName(alias="NONE", canonical="UNRECOGNISED")
)

for filterName in filterNames:
self.assertTrue(filterName.alias in self.mapper.filters)
self.assertIn(filterName.alias, self.mapper.filters)
self.assertEqual(Filter(filterName.alias).getCanonicalName(), filterName.canonical)


def suite():
"""Returns a suite containing all the test cases in this module."""

utilsTests.init()

suites = []
suites += unittest.makeSuite(CameraTestCase)
suites += unittest.makeSuite(utilsTests.MemoryTestCase)
return unittest.TestSuite(suites)
class TestMemory(lsst.utils.tests.MemoryTestCase):
pass


def run(shouldExit=False):
"""Run the tests"""
utilsTests.run(suite(), shouldExit)
def setup_module(module):
lsst.utils.tests.init()

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
13 changes: 7 additions & 6 deletions tests/testColorterms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function
#
# LSST Data Management System
# Copyright 2008, 2009, 2010 LSST Corporation.
#
# Copyright 2008-2016 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
Expand All @@ -19,8 +19,9 @@
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
# see <https://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import absolute_import, division, print_function

import os
import numbers
Expand All @@ -42,7 +43,7 @@ def setUp(self):
self.photoCalConf = photoCal.PhotoCalConfig()
self.photoCalConf.colorterms.load(colortermsFile)

def testHSCColorterms(self):
def testHscColorterms(self):
"""Test that the colorterm libraries are formatted correctly"""
refBands = ["g", "r", "i", "z", "y"]
hscBands = ["g", "r", "i", "z", "y"]
Expand All @@ -54,7 +55,7 @@ def testHSCColorterms(self):
self.assertIsInstance(ct.c1, numbers.Number)
self.assertIsInstance(ct.c2, numbers.Number)

def testSDSSColorterms(self):
def testSdssColorterms(self):
"""Test that the colorterm libraries are formatted correctly"""
sdssBands = ["g", "r", "i", "z", "y"]
hscBands = ["g", "r", "i", "i2", "z", "y", "N816", "N921"]
Expand All @@ -66,7 +67,7 @@ def testSDSSColorterms(self):
self.assertIsInstance(ct.c1, numbers.Number)
self.assertIsInstance(ct.c2, numbers.Number)

def testPS1Colorterms(self):
def testPs1Colorterms(self):
"""Test that the colorterm libraries are formatted correctly"""
ps1Bands = ["g", "r", "i", "z", "y"]
hscBands = ["g", "r", "i", "i2", "z", "y", "N816", "N921"]
Expand Down
37 changes: 33 additions & 4 deletions tests/testDistortion.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
#!/usr/bin/env python

#
# LSST Data Management System
#
# Copyright 2008-2016 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# 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 LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
import unittest
import lsst.utils.tests as utilsTests

import lsst.utils.tests
from lsst.obs.hsc import HscMapper
from lsst.afw.cameraGeom import PIXELS, FOCAL_PLANE, PUPIL, TAN_PIXELS
from lsst.afw.geom import Point2D
Expand All @@ -15,7 +36,7 @@ class HscDistortionTestCase(unittest.TestCase):
test that it produces the same results.
"""

@utilsTests.debugger(Exception)
@lsst.utils.tests.debugger(Exception)
def test(self):
verification = getVerificationData()
camera = HscMapper(root=".").camera
Expand Down Expand Up @@ -1233,5 +1254,13 @@ def getVerificationData():

################################################################################

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


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

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
61 changes: 22 additions & 39 deletions tests/testRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# LSST Data Management System
#
# Copyright 2008-2016 AURA/LSST.
# Copyright 2008-2016 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
Expand All @@ -21,33 +21,24 @@
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#

import os
import unittest
from glob import glob
from shutil import rmtree
from subprocess import check_call
from tempfile import mkdtemp
import warnings

import lsst.afw.geom as afwGeom
import lsst.daf.persistence as dafPersist
import lsst.pex.exceptions as pexExcept
import lsst.utils.tests as utilsTests
from lsst.utils import getPackageDir

TEST_DATA_PACKAGE = "testdata_subaru"


def requirePackage(packageName):
"""
Decorator to skip unit tests unless the package ``packageName`` is set up.
"""
try:
getPackageDir(packageName)
except pexExcept.NotFoundError as e:
return unittest.skip("Required package %s not available [%s]." % (packageName, str(e)))
return lambda func: func
import lsst.utils

testData = "testdata_subaru"
try:
testDataPackage = lsst.utils.getPackageDir(testData)
except lsst.pex.exceptions.NotFoundError as err:
testDataPackage = None

def createDataRepository(mapperName, inputPath):
"""
Expand All @@ -60,16 +51,16 @@ def createDataRepository(mapperName, inputPath):
repoPath = mkdtemp()
with open(os.path.join(repoPath, "_mapper"), "w") as _mapper:
_mapper.write(mapperName)
ingest_cmd = os.path.join(getPackageDir("obs_subaru"), "bin", "hscIngestImages.py")
ingest_cmd = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "bin", "hscIngestImages.py")
check_call([ingest_cmd, repoPath] + glob(os.path.join(inputPath, "*.fits.gz")))
return repoPath


class IngestRawTestCase(utilsTests.TestCase):
class IngestRawTestCase(lsst.utils.tests.TestCase):
"""Ensure that we can ingest raw data to a repository."""
@requirePackage(TEST_DATA_PACKAGE)
@unittest.skipIf(testDataPackage is None, "%s not setup"%(testData))
def setUp(self):
rawPath = os.path.join(getPackageDir(TEST_DATA_PACKAGE), "hsc", "raw")
rawPath = os.path.join(testDataPackage, "hsc", "raw")
self.repoPath = createDataRepository("lsst.obs.hsc.HscMapper", rawPath)

def tearDown(self):
Expand All @@ -83,12 +74,12 @@ def testMapperName(self):
self.assertEqual(name, "obs_subaru")


class GetDataTestCase(utilsTests.TestCase):
class GetDataTestCase(lsst.utils.tests.TestCase):
"""Demonstrate retrieval of data from a repository."""
@requirePackage(TEST_DATA_PACKAGE)
@unittest.skipIf(testDataPackage is None, "%s not setup"%(testData))
def setUp(self):
rawPath = os.path.join(getPackageDir(TEST_DATA_PACKAGE), "hsc", "raw")
calibPath = os.path.join(getPackageDir(TEST_DATA_PACKAGE), "hsc", "calib")
rawPath = os.path.join(testDataPackage, "hsc", "raw")
calibPath = os.path.join(testDataPackage, "hsc", "calib")
self.repoPath = createDataRepository("lsst.obs.hsc.HscMapper", rawPath)
self.butler = dafPersist.Butler(root=self.repoPath, calibRoot=calibPath)

Expand Down Expand Up @@ -136,21 +127,13 @@ def testFlat(self):
self.assertEqual(flat.getDetector().getId(), self.ccdNum)


def suite():
"""Returns a suite containing all the test cases in this module."""

utilsTests.init()

suites = []
suites += unittest.makeSuite(IngestRawTestCase)
suites += unittest.makeSuite(GetDataTestCase)
suites += unittest.makeSuite(utilsTests.MemoryTestCase)
return unittest.TestSuite(suites)
class TestMemory(lsst.utils.tests.MemoryTestCase):
pass


def run(shouldExit=False):
"""Run the tests"""
utilsTests.run(suite(), shouldExit)
def setup_module(module):
lsst.utils.tests.init()

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

0 comments on commit f68454c

Please sign in to comment.