Coverage for tests/test_policy.py: 38%

41 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-01-25 04:32 +0000

1# This file is part of obs_base. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 <https://www.gnu.org/licenses/>. 

21 

22import os 

23import shutil 

24import tempfile 

25import unittest 

26 

27import lsst.daf.persistence as dafPersist 

28import lsst.daf.persistence.test as dpTest 

29import lsst.utils.tests 

30import yaml 

31 

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

33 

34 

35class TestPolicyInRepo(unittest.TestCase): 

36 def setUp(self): 

37 self.testData = tempfile.mkdtemp(dir=ROOT, prefix="TestPolicyInRepo-") 

38 

39 def tearDown(self): 

40 if os.path.exists(self.testData): 

41 shutil.rmtree(self.testData) 

42 

43 def test(self): 

44 """Verify that when specifying a repo policy that the policy gets 

45 written & loaded correctly. 

46 """ 

47 

48 objA = dpTest.TestObject("abc") 

49 dpTest.TestObject("def") 

50 

51 firstRepoPath = os.path.join(self.testData, "repo1") 

52 os.path.join(self.testData, "repo2") 

53 

54 policy = dafPersist.Policy( 

55 { 

56 "camera": "lsst.afw.cameraGeom.Camera", 

57 "datasets": { 

58 "basicObject1": { 

59 "python": "lsst.daf.persistence.test.TestObject", 

60 "template": "basic/id%(id)s.pickle", 

61 "storage": "PickleStorage", 

62 }, 

63 }, 

64 } 

65 ) 

66 

67 firstRepoPath = os.path.join(self.testData, "repo1") 

68 repoArgs = dafPersist.RepositoryArgs( 

69 root=firstRepoPath, mapper="lsst.obs.base.test.CompositeMapper", policy=policy 

70 ) 

71 butler = dafPersist.Butler(outputs=repoArgs) 

72 

73 with open(os.path.join(firstRepoPath, "repositoryCfg.yaml")) as f: 

74 cfg = yaml.load(f, Loader=yaml.UnsafeLoader) 

75 self.assertEqual(cfg.policy, policy) 

76 butler.put(objA, "basicObject1", {"id": 1}) 

77 

78 del butler 

79 del repoArgs 

80 

81 # Test that a newly-initialized butler can find the policy in the 

82 # repositoryCfg. 

83 repoArgs = dafPersist.RepositoryArgs(root=firstRepoPath) 

84 butler = dafPersist.Butler(inputs=repoArgs) 

85 reloadedObjA = butler.get("basicObject1", {"id": 1}) 

86 self.assertEqual(reloadedObjA, objA) 

87 

88 

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

90 pass 

91 

92 

93def setup_module(module): 

94 lsst.utils.tests.init() 

95 

96 

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

98 lsst.utils.tests.init() 

99 unittest.main()