Coverage for tests/test_composites.py: 21%

46 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-10-02 08:00 +0000

1# This file is part of daf_butler. 

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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

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

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

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

18# (at your option) any later version. 

19# 

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

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

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

23# GNU General Public License for more details. 

24# 

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

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

27 

28import os 

29import unittest 

30 

31from lsst.daf.butler import CompositesConfig, CompositesMap, DatasetType, DimensionUniverse, StorageClass 

32 

33TESTDIR = os.path.dirname(__file__) 

34 

35 

36class TestCompositesConfig(unittest.TestCase): 

37 """Test composites configuration.""" 

38 

39 @classmethod 

40 def setUpClass(cls): 

41 cls.configFile = os.path.join(TESTDIR, "config", "basic", "composites.yaml") 

42 

43 def testBadConfig(self): 

44 """Config with bad values in it""" 

45 with self.assertRaises(ValueError): 

46 CompositesConfig(os.path.join(TESTDIR, "config", "basic", "composites-bad.yaml")) 

47 

48 def testConfig(self): 

49 c = CompositesConfig(self.configFile) 

50 self.assertIn("default", c) 

51 # Check merging has worked 

52 rootKey = "disassembled" 

53 self.assertIn(f".{rootKey}.dummyTrue", c) 

54 self.assertIn(f".{rootKey}.StructuredComposite", c) 

55 

56 # Check that all entries are booleans (this is meant to be enforced 

57 # internally) 

58 for k in c[rootKey]: 

59 self.assertIsInstance(c[f".{rootKey}.{k}"], bool, f"Testing {rootKey}.{k}") 

60 

61 def testMap(self): 

62 universe = DimensionUniverse() 

63 c = CompositesMap(self.configFile, universe=universe) 

64 

65 # Check that a str is not supported 

66 with self.assertRaises(ValueError): 

67 c.shouldBeDisassembled("fred") 

68 

69 # These will fail (not a composite) 

70 sc = StorageClass("StructuredDataJson") 

71 d = DatasetType("dummyTrue", universe.empty, sc) 

72 self.assertFalse(sc.isComposite()) 

73 self.assertFalse(d.isComposite()) 

74 self.assertFalse(c.shouldBeDisassembled(d), f"Test with DatasetType: {d}") 

75 self.assertFalse(c.shouldBeDisassembled(sc), f"Test with StorageClass: {sc}") 

76 

77 # Repeat but this time use a composite storage class 

78 sccomp = StorageClass("Dummy") 

79 sc = StorageClass("StructuredDataJson", components={"dummy": sccomp, "dummy2": sccomp}) 

80 d = DatasetType("dummyTrue", universe.empty, sc) 

81 self.assertTrue(sc.isComposite()) 

82 self.assertTrue(d.isComposite()) 

83 self.assertTrue(c.shouldBeDisassembled(d), f"Test with DatasetType: {d}") 

84 self.assertFalse(c.shouldBeDisassembled(sc), f"Test with StorageClass: {sc}") 

85 

86 # Override with False 

87 d = DatasetType("dummyFalse", universe.empty, sc) 

88 self.assertFalse(c.shouldBeDisassembled(d), f"Test with DatasetType: {d}") 

89 

90 # DatasetType that has no explicit entry 

91 d = DatasetType("dummyFred", universe.empty, sc) 

92 self.assertFalse(c.shouldBeDisassembled(d), f"Test with DatasetType: {d}") 

93 

94 # StorageClass that will be disassembled 

95 sc = StorageClass("StructuredComposite", components={"dummy": sccomp, "dummy2": sccomp}) 

96 d = DatasetType("dummyFred", universe.empty, sc) 

97 self.assertTrue(c.shouldBeDisassembled(d), f"Test with DatasetType: {d}") 

98 

99 # Check that we are not allowed a single component in a composite 

100 with self.assertRaises(ValueError): 

101 StorageClass("TestSC", components={"dummy": sccomp}) 

102 

103 

104if __name__ == "__main__": 

105 unittest.main()