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-11607: Use mkdtemp in tests #61

Merged
merged 1 commit into from
Aug 19, 2017
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
101 changes: 50 additions & 51 deletions tests/testCameraMapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import collections
import os
import unittest
import tempfile

import numpy as np

Expand All @@ -34,10 +35,9 @@
import lsst.afw.table as afwTable
import lsst.daf.persistence as dafPersist
import lsst.obs.base
from lsst.utils import getPackageDir
import shutil

testDir = os.path.relpath(os.path.join(getPackageDir('obs_base'), 'tests'))
ROOT = os.path.abspath(os.path.dirname(__file__))


def setup_module(module):
Expand All @@ -48,17 +48,17 @@ class BaseMapper(lsst.obs.base.CameraMapper):
packageName = 'base'

def __init__(self):
policy = dafPersist.Policy(os.path.join(testDir, "BaseMapper.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=testDir, root=testDir)
policy = dafPersist.Policy(os.path.join(ROOT, "BaseMapper.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=ROOT, root=ROOT)
return


class MinMapper1(lsst.obs.base.CameraMapper):
packageName = 'larry'

def __init__(self, **kwargs):
policy = dafPersist.Policy(os.path.join(testDir, "MinMapper1.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=testDir, **kwargs)
policy = dafPersist.Policy(os.path.join(ROOT, "MinMapper1.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=ROOT, **kwargs)
return

def std_x(self, item, dataId):
Expand All @@ -76,8 +76,8 @@ class MinMapper2(lsst.obs.base.CameraMapper):
# CalibRoot in policy
# needCalibRegistry
def __init__(self, **kwargs):
policy = dafPersist.Policy(os.path.join(testDir, "MinMapper2.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=testDir,
policy = dafPersist.Policy(os.path.join(ROOT, "MinMapper2.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=ROOT,
registry="cfhtls.sqlite3", **kwargs)
return

Expand All @@ -100,16 +100,16 @@ def getCameraName(cls):
class MinMapper3(lsst.obs.base.CameraMapper):

def __init__(self, **kwargs):
policy = dafPersist.Policy(os.path.join(testDir, "MinMapper1.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=testDir, root=testDir)
policy = dafPersist.Policy(os.path.join(ROOT, "MinMapper1.paf"))
lsst.obs.base.CameraMapper.__init__(self, policy=policy, repositoryDir=ROOT, root=ROOT)
return


class Mapper1TestCase(unittest.TestCase):
"""A test case for the mapper used by the data butler."""

def setUp(self):
self.mapper = MinMapper1(root=testDir)
self.mapper = MinMapper1(root=ROOT)

def tearDown(self):
del self.mapper
Expand All @@ -126,7 +126,7 @@ def testMap(self):
self.assertEqual(loc.getPythonType(), "lsst.afw.geom.BoxI")
self.assertEqual(loc.getCppType(), "BoxI")
self.assertEqual(loc.getStorageName(), "PickleStorage")
expectedRoot = testDir
expectedRoot = ROOT
expectedLocations = ["foo-1,1.pickle"]
self.assertEqual(loc.getStorage().root, expectedRoot)
self.assertEqual(loc.getLocations(), expectedLocations)
Expand Down Expand Up @@ -160,7 +160,7 @@ class Mapper2TestCase(unittest.TestCase):
"""A test case for the mapper used by the data butler."""

def testGetDatasetTypes(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
expectedTypes = BaseMapper().getDatasetTypes()
# Add the expected additional types to what the base class provides
expectedTypes.extend(["flat", "flat_md", "flat_filename", "flat_sub",
Expand All @@ -179,13 +179,13 @@ def testGetDatasetTypes(self):
set(expectedTypes))

def testMap(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
loc = mapper.map("raw", {"ccd": 13}, write=True)
self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureU")
self.assertEqual(loc.getCppType(), "ImageU")
self.assertEqual(loc.getStorageName(), "FitsStorage")
self.assertEqual(loc.getLocations(), ["foo-13.fits"])
self.assertEqual(loc.getStorage().root, testDir)
self.assertEqual(loc.getStorage().root, ROOT)
self.assertEqual(loc.getAdditionalData().toString(), "ccd = 13\n")

def testSubMap(self):
Expand All @@ -197,13 +197,13 @@ def testSubMap(self):
# new afw (post-#1556) interface
bbox = afwGeom.BoxI(afwGeom.Point2I(200, 100),
afwGeom.Extent2I(300, 400))
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
loc = mapper.map("raw_sub", {"ccd": 13, "bbox": bbox}, write=True)
self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureU")
self.assertEqual(loc.getCppType(), "ImageU")
self.assertEqual(loc.getStorageName(), "FitsStorage")
self.assertEqual(loc.getLocations(), ["foo-13.fits"])
self.assertEqual(loc.getStorage().root, testDir)
self.assertEqual(loc.getStorage().root, ROOT)
self.assertEqual(loc.getAdditionalData().toString(),
'ccd = 13\nheight = 400\nllcX = 200\nllcY = 100\nwidth = 300\n')

Expand All @@ -212,13 +212,13 @@ def testSubMap(self):
self.assertEqual(loc.getCppType(), "ImageU")
self.assertEqual(loc.getStorageName(), "FitsStorage")
self.assertEqual(loc.getLocations(), ["foo-13.fits"])
self.assertEqual(loc.getStorage().root, testDir)
self.assertEqual(loc.getStorage().root, ROOT)
self.assertEqual(loc.getAdditionalData().toString(),
'ccd = 13\nheight = 400\nimageOrigin = "PARENT"\n'
'llcX = 200\nllcY = 100\nwidth = 300\n')

def testCatalogExtras(self):
butler = dafPersist.Butler(root=testDir, mapper=MinMapper2)
butler = dafPersist.Butler(root=ROOT, mapper=MinMapper2)
schema = afwTable.Schema()
aa = schema.addField("a", type=np.int32, doc="a")
bb = schema.addField("b", type=np.float64, doc="b")
Expand All @@ -243,10 +243,10 @@ def testCatalogExtras(self):
print("Warning: could not remove file %r: %s" % (filename, exc))

def testImage(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
loc = mapper.map("some", dict(ccd=35))
expectedLocations = ["bar-35.fits"]
self.assertEqual(loc.getStorage().root, testDir)
self.assertEqual(loc.getStorage().root, ROOT)
self.assertEqual(loc.getLocations(), expectedLocations)

butler = dafPersist.ButlerFactory(mapper=mapper).create()
Expand All @@ -262,16 +262,16 @@ def testImage(self):
self.assertEqual(image.getWidth(), 300)

def testDetector(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
butler = dafPersist.ButlerFactory(mapper=mapper).create()
detector = butler.get("raw_detector", ccd=0)
self.assertEqual(detector.getName(), "ccd00")

def testGzImage(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
loc = mapper.map("someGz", dict(ccd=35))
expectedLocations = [os.path.join("gz", "bar-35.fits.gz")]
self.assertEqual(loc.getStorage().root, testDir)
self.assertEqual(loc.getStorage().root, ROOT)
self.assertEqual(loc.getLocations(), expectedLocations)

butler = dafPersist.ButlerFactory(mapper=mapper).create()
Expand All @@ -285,9 +285,9 @@ def testGzImage(self):
self.assertEqual(image.getWidth(), 300)

def testFzImage(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
loc = mapper.map("someFz", dict(ccd=35))
expectedRoot = testDir
expectedRoot = ROOT
expectedLocations = [os.path.join("fz", "bar-35.fits.fz")]
self.assertEqual(loc.getStorage().root, expectedRoot)
self.assertEqual(loc.getLocations(), expectedLocations)
Expand All @@ -303,7 +303,7 @@ def testFzImage(self):
self.assertEqual(image.getWidth(), 300)

def testButlerQueryMetadata(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
butler = dafPersist.ButlerFactory(mapper=mapper).create()
kwargs = {"ccd": 35, "filter": "r", "visit": 787731,
"taiObs": "2005-04-02T09:24:49.933440000"}
Expand All @@ -316,22 +316,22 @@ def testButlerQueryMetadata(self):
self.assertEqual(butler.queryMetadata("raw", "ccd", ccd=36, filter="r", visit=787731), [])

def testQueryMetadata(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
self.assertEqual(mapper.queryMetadata("raw", ["ccd"], None),
[(x,) for x in range(36) if x != 3])

def testStandardize(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
self.assertEqual(mapper.canStandardize("raw"), True)
self.assertEqual(mapper.canStandardize("notPresent"), False)

def testCalib(self):
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
loc = mapper.map("flat", {"visit": 787650, "ccd": 13}, write=True)
self.assertEqual(loc.getPythonType(), "lsst.afw.image.ExposureF")
self.assertEqual(loc.getCppType(), "ExposureF")
self.assertEqual(loc.getStorageName(), "FitsStorage")
expectedRoot = testDir
expectedRoot = ROOT
expectedLocations = ["flat-05Am03-fi.fits"]
self.assertEqual(loc.getStorage().root, expectedRoot)
self.assertEqual(loc.getLocations(), expectedLocations)
Expand All @@ -344,29 +344,29 @@ def testNames(self):

@unittest.expectedFailure
def testParentSearch(self):
mapper = MinMapper2(root=testDir)
paths = mapper.parentSearch(os.path.join(testDir, 'testParentSearch'),
os.path.join(testDir, os.path.join('testParentSearch', 'bar.fits')))
self.assertEqual(paths, [os.path.join(testDir, os.path.join('testParentSearch', 'bar.fits'))])
paths = mapper.parentSearch(os.path.join(testDir, 'testParentSearch'),
os.path.join(testDir,
mapper = MinMapper2(root=ROOT)
paths = mapper.parentSearch(os.path.join(ROOT, 'testParentSearch'),
os.path.join(ROOT, os.path.join('testParentSearch', 'bar.fits')))
self.assertEqual(paths, [os.path.join(ROOT, os.path.join('testParentSearch', 'bar.fits'))])
paths = mapper.parentSearch(os.path.join(ROOT, 'testParentSearch'),
os.path.join(ROOT,
os.path.join('testParentSearch', 'bar.fits[1]')))
self.assertEqual(paths, [os.path.join(testDir, os.path.join('testParentSearch', 'bar.fits[1]'))])
self.assertEqual(paths, [os.path.join(ROOT, os.path.join('testParentSearch', 'bar.fits[1]'))])

paths = mapper.parentSearch(os.path.join(testDir, 'testParentSearch'),
os.path.join(testDir, os.path.join('testParentSearch', 'baz.fits')))
self.assertEqual(paths, [os.path.join(testDir,
paths = mapper.parentSearch(os.path.join(ROOT, 'testParentSearch'),
os.path.join(ROOT, os.path.join('testParentSearch', 'baz.fits')))
self.assertEqual(paths, [os.path.join(ROOT,
os.path.join('testParentSearch', '_parent', 'baz.fits'))])
paths = mapper.parentSearch(os.path.join(testDir, 'testParentSearch'),
os.path.join(testDir,
paths = mapper.parentSearch(os.path.join(ROOT, 'testParentSearch'),
os.path.join(ROOT,
os.path.join('testParentSearch', 'baz.fits[1]')))
self.assertEqual(paths, [os.path.join(testDir,
self.assertEqual(paths, [os.path.join(ROOT,
os.path.join('testParentSearch', '_parent', 'baz.fits[1]'))])

def testSkymapLookups(self):
"""Test that metadata lookups don't try to get skymap data ID values from the registry.
"""
mapper = MinMapper2(root=testDir)
mapper = MinMapper2(root=ROOT)
butler = dafPersist.Butler(mapper=mapper)
with self.assertRaises(RuntimeError) as manager:
butler.dataRef("forced_src", visit=787650, ccd=13)
Expand All @@ -391,22 +391,21 @@ def testPackageName(self):
class ParentRegistryTestCase(unittest.TestCase):

def setUp(self):
self.testDir = os.path.join(testDir, "ParentRegistryTestCase")
self.tearDown()
self.repoARoot = os.path.join(self.testDir, 'a')
self.ROOT = tempfile.mkdtemp(dir=ROOT, prefix="ParentRegistryTestCase-")
self.repoARoot = os.path.join(self.ROOT, 'a')
args = dafPersist.RepositoryArgs(root=self.repoARoot, mapper=MinMapper1)
butler = dafPersist.Butler(outputs=args)
with open(os.path.join(self.repoARoot, 'registry.sqlite3'), 'w') as f:
f.write('123')

def tearDown(self):
if os.path.exists(self.testDir):
shutil.rmtree(self.testDir)
if os.path.exists(self.ROOT):
shutil.rmtree(self.ROOT)

def test(self):
"""Verify that when the child repo does not have a registry it is assigned the registry from the
parent."""
repoBRoot = os.path.join(self.testDir, 'b')
repoBRoot = os.path.join(self.ROOT, 'b')
butler = dafPersist.Butler(inputs=self.repoARoot, outputs=repoBRoot)
# This way of getting the registry from the mapping is obviously going way into private members and
# the python lambda implementation code. It is very brittle and should not be duplicated in user code
Expand Down
22 changes: 8 additions & 14 deletions tests/testComposite.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@
import os
import shutil
import unittest
import lsst.utils.tests
from lsst.utils import getPackageDir
import tempfile

import lsst.utils.tests
import lsst.daf.persistence as dafPersist
import lsst.daf.persistence.test as dpTest
import lsst.utils.tests
from lsst.utils import getPackageDir

ROOT = os.path.abspath(os.path.dirname(__file__))


class TestCompositeTestCase(unittest.TestCase):
"""A test case for composite object i/o."""

def setUp(self):
packageDir = getPackageDir('obs_base')
self.testData = os.path.join(packageDir, 'tests', 'composite')
self.tearDown()
self.testData = tempfile.mkdtemp(dir=ROOT, prefix='TestCompositeTestCase-')
self.firstRepoPath = os.path.join(self.testData, 'repo1')
self.objA = dpTest.TestObject("abc")
self.objB = dpTest.TestObject("def")
Expand Down Expand Up @@ -189,9 +187,7 @@ class TestGenericAssembler(unittest.TestCase):
"""A test case for the generic assembler feature of composite datasets."""

def setUp(self):
packageDir = getPackageDir('obs_base')
self.testData = os.path.join(packageDir, 'tests', 'genericAssembler')
self.tearDown()
self.testData = tempfile.mkdtemp(dir=ROOT, prefix='TestGenericAssembler-')
self.firstRepoPath = os.path.join(self.testData, 'repo1')
self.secondRepoPath = os.path.join(self.testData, 'repo2')
self.objA = dpTest.TestObject("abc")
Expand Down Expand Up @@ -379,8 +375,7 @@ class TestSubset(unittest.TestCase):
"""A test case for composite object subset keyword."""

def setUp(self):
packageDir = getPackageDir('obs_base')
self.testData = os.path.join(packageDir, 'tests', 'compositeSubset')
self.testData = tempfile.mkdtemp(dir=ROOT, prefix='TestSubset-')
self.firstRepoPath = os.path.join(self.testData, 'repo1')
self.objA1 = dpTest.TestObject("abc")
self.objA2 = dpTest.TestObject("ABC")
Expand Down Expand Up @@ -446,8 +441,7 @@ class TestInputOnly(unittest.TestCase):
"""A test case for composite input keyword."""

def setUp(self):
packageDir = getPackageDir('obs_base')
self.testData = os.path.join(packageDir, 'tests', 'composite')
self.testData = tempfile.mkdtemp(dir=ROOT, prefix='TestInputOnly-')
self.firstRepoPath = os.path.join(self.testData, 'repo1')
self.objA = dpTest.TestObject("abc")
self.objB = dpTest.TestObject("def")
Expand Down
17 changes: 8 additions & 9 deletions tests/testDM-329.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,28 @@


import unittest
import lsst.utils.tests
import os

import lsst.utils.tests
import lsst.afw.image
import lsst.daf.persistence as dafPersist
import lsst.obs.base
import lsst.pex.policy as pexPolicy
from lsst.utils import getPackageDir

import os

testDir = os.path.relpath(os.path.join(getPackageDir('obs_base'), 'tests'))
ROOT = os.path.abspath(os.path.dirname(__file__))


class MinMapper2(lsst.obs.base.CameraMapper):
packageName = 'larry'

def __init__(self):
policy = pexPolicy.Policy.createPolicy(os.path.join(testDir, 'MinMapper2.paf'))
policy = pexPolicy.Policy.createPolicy(os.path.join(ROOT, 'MinMapper2.paf'))
lsst.obs.base.CameraMapper.__init__(self,
policy=policy,
repositoryDir=testDir,
root=testDir,
registry=os.path.join(testDir, 'cfhtls.sqlite3'))
repositoryDir=ROOT,
root=ROOT,
registry=os.path.join(ROOT, 'cfhtls.sqlite3'))
return

def _transformId(self, dataId):
Expand All @@ -69,7 +68,7 @@ def testHdu(self):
for i in (1, 2, 3):
loc = mapper.map("other", dict(ccd=35, hdu=i))
expectedLocations = ["bar-35.fits[%d]" % (i,)]
self.assertEqual(loc.getStorage().root, testDir)
self.assertEqual(loc.getStorage().root, ROOT)
self.assertEqual(loc.getLocations(), expectedLocations)
image = butler.get("other", ccd=35, hdu=i, immediate=True)
self.assertIsInstance(image, lsst.afw.image.ImageF)
Expand Down