Coverage for tests/test_butlerProxy.py: 37%

51 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-19 04:55 -0700

1# 

2# LSST Data Management System 

3# Copyright 2008, 2009, 2010 LSST Corporation. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <http://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import os 

24import shutil 

25import unittest 

26import tempfile 

27import lsst.utils.tests 

28 

29import lsst.daf.persistence as dafPersist 

30import lsst.daf.base as dafBase 

31 

32from testLib import isValidDateTime, TypeWithProxy, TypeWithoutProxy 

33 

34# Define the root of the tests relative to this file 

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

36 

37 

38class ButlerProxyTestCase(unittest.TestCase): 

39 """A test case for the data butler finding a Mapper in a root""" 

40 

41 inputDir = os.path.join(ROOT, 'root') 

42 

43 def setUp(self): 

44 self.outputDir = tempfile.mkdtemp(dir=ROOT, prefix='ButlerProxyTestCase-') 

45 self.butler = dafPersist.Butler(self.inputDir, 

46 outPath=os.path.join(self.outputDir, "proxyOut")) 

47 

48 def tearDown(self): 

49 del self.butler 

50 if os.path.exists(self.outputDir): 

51 shutil.rmtree(self.outputDir) 

52 

53 def testCheckProxy(self): 

54 """Attempt to cycle a DateTime object through the butler 

55 """ 

56 dt = dafBase.DateTime.now() 

57 self.butler.put(dt, "dt", ccd=1) 

58 

59 # The next two types should not be castable to a DateTime object 

60 # when the proxy is passed to a function 

61 p1 = TypeWithoutProxy() 

62 self.butler.put(p1, "p1", ccd=1) 

63 

64 p2 = TypeWithProxy() 

65 self.butler.put(p2, "p2", ccd=1) 

66 

67 # First try with immediate read, this should obviously work 

68 dt = self.butler.get("dt", ccd=1, immediate=True) 

69 self.assertIsInstance(dt, dafBase.DateTime) 

70 self.assertTrue(isValidDateTime(dt)) 

71 

72 # Now try again with lazy read 

73 dt = self.butler.get("dt", ccd=1, immediate=False) 

74 self.assertIsInstance(dt, dafPersist.readProxy.ReadProxy) 

75 self.assertTrue(isValidDateTime(dt)) 

76 

77 # Now try with a type for which a proxy is not registered 

78 p1 = self.butler.get("p1", ccd=1, immediate=True) 

79 self.assertIsInstance(p1, TypeWithoutProxy) 

80 with self.assertRaises(TypeError): 

81 isValidDateTime(p1) 

82 

83 # Now try with a type for which a proxy was registered but 

84 # that cannot convert to a DateTime 

85 p2 = self.butler.get("p2", ccd=1, immediate=True) 

86 self.assertIsInstance(p2, TypeWithProxy) 

87 with self.assertRaises(TypeError): 

88 isValidDateTime(p2) 

89 

90 # Finally try with an invalid DateTime 

91 dt = dafBase.DateTime() 

92 self.butler.put(dt, "dt", ccd=1) 

93 

94 dt = self.butler.get("dt", ccd=1, immediate=False) 

95 self.assertIsInstance(dt, dafPersist.readProxy.ReadProxy) 

96 self.assertFalse(isValidDateTime(dt)) 

97 

98 

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

100 pass 

101 

102 

103def setup_module(module): 

104 lsst.utils.tests.init() 

105 

106 

107if __name__ == "__main__": 107 ↛ 108line 107 didn't jump to line 108, because the condition on line 107 was never true

108 lsst.utils.tests.init() 

109 unittest.main()