Coverage for tests/test_policyInRepo.py: 39%

42 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-05 18:08 -0800

1# This file is part of obs_test. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

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

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21# 

22 

23import os 

24import tempfile 

25import unittest 

26 

27import lsst.daf.persistence as dafPersist 

28# we only import lsst.obs.test.TestMapper from lsst.obs.test, but use the namespace to hide it from pytest 

29import lsst.obs.test 

30import lsst.utils.tests 

31from lsst.utils import getPackageDir 

32import shutil 

33 

34 

35ROOT = getPackageDir('obs_test') 

36 

37 

38class PolicyTestCase(unittest.TestCase): 

39 """Tests related to the use of the policy file in Butler/butlerUtils. 

40 """ 

41 def setUp(self): 

42 self.testDir = tempfile.mkdtemp(dir=os.path.join(ROOT, 'tests'), prefix=type(self).__name__+'-') 

43 self.repoARoot = os.path.join(self.testDir, 'a') 

44 

45 def tearDown(self): 

46 if os.path.exists(self.testDir): 

47 shutil.rmtree(self.testDir) 

48 

49 def testInRepoPolicyOverrides(self): 

50 """Test policy file overrides. 

51 

52 Notes 

53 ----- 

54 Verify that the template value specified in the policy file in 

55 the repository overrides the template value set in the policy file 

56 in the package. 

57 

58 Checks that child repositories do not get the policy from the 

59 parent (per specification). 

60 Checks that values not specified in the local _policy file are set 

61 with those of the package's _policy file. 

62 """ 

63 policyOverride = {'exposures': {'raw': {'template': "raw/v%(visit)d_f%(filter)s.fits.gz"}}} 

64 policyPath = os.path.join(ROOT, 'policy', 'testMapper.yaml') 

65 policy = dafPersist.Policy(policyPath) 

66 postISRCCDtemplate = policy.get('exposures.postISRCCD.template') 

67 

68 butler = dafPersist.Butler(outputs={'root': self.repoARoot, 

69 'mapper': lsst.obs.test.TestMapper, 

70 'policy': policyOverride}) 

71 

72 # check that the declared policy got used in the mapper 

73 mapper = butler._repos.outputs()[0].repo._mapper 

74 self.assertEqual(mapper.mappings['raw'].template, "raw/v%(visit)d_f%(filter)s.fits.gz") 

75 # Run a simple test case to verify that although the package's policy was overloaded with some 

76 # values, other values specified in the policy file in the package are loaded. 

77 self.assertEqual(postISRCCDtemplate, mapper.mappings['postISRCCD'].template) 

78 del butler 

79 del mapper 

80 

81 repoBRoot = os.path.join(self.testDir, 'b') 

82 butler = dafPersist.Butler(inputs=self.repoARoot, outputs=repoBRoot) 

83 # check that the reloaded policy got used in the mapper for repo A 

84 mapper = butler._repos.inputs()[0].repo._mapper 

85 self.assertEqual(mapper.mappings['raw'].template, "raw/v%(visit)d_f%(filter)s.fits.gz") 

86 # again, test that another value is loaded from package policy file is loaded correctly. 

87 self.assertEqual(postISRCCDtemplate, mapper.mappings['postISRCCD'].template) 

88 # also check that repo B does not get the in-repo policy from A 

89 mapper = butler._repos.outputs()[0].repo._mapper 

90 self.assertNotEqual(mapper.mappings['raw'].template, "raw/v%(visit)d_f%(filter)s.fits.gz") 

91 # and again, test that another value is loaded from package policy file is loaded correctly. 

92 self.assertEqual(postISRCCDtemplate, mapper.mappings['postISRCCD'].template) 

93 

94 

95class MemoryTester(lsst.utils.tests.MemoryTestCase): 

96 pass 

97 

98 

99def setup_module(module): 

100 lsst.utils.tests.init() 

101 

102 

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

104 lsst.utils.tests.init() 

105 unittest.main()