Coverage for tests/test_composites.py: 23%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 02:46 -0700

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 DatasetType, DimensionUniverse, StorageClass 

32from lsst.daf.butler.datastore.composites import CompositesConfig, CompositesMap 

33 

34TESTDIR = os.path.dirname(__file__) 

35 

36 

37class TestCompositesConfig(unittest.TestCase): 

38 """Test composites configuration.""" 

39 

40 @classmethod 

41 def setUpClass(cls): 

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

43 

44 def testBadConfig(self): 

45 """Config with bad values in it""" 

46 with self.assertRaises(ValueError): 

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

48 

49 def testConfig(self): 

50 c = CompositesConfig(self.configFile) 

51 self.assertIn("default", c) 

52 # Check merging has worked 

53 rootKey = "disassembled" 

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

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

56 

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

58 # internally) 

59 for k in c[rootKey]: 

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

61 

62 def testMap(self): 

63 universe = DimensionUniverse() 

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

65 

66 # Check that a str is not supported 

67 with self.assertRaises(ValueError): 

68 c.shouldBeDisassembled("fred") 

69 

70 # These will fail (not a composite) 

71 sc = StorageClass("StructuredDataJson") 

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

73 self.assertFalse(sc.isComposite()) 

74 self.assertFalse(d.isComposite()) 

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

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

77 

78 # Repeat but this time use a composite storage class 

79 sccomp = StorageClass("Dummy") 

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

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

82 self.assertTrue(sc.isComposite()) 

83 self.assertTrue(d.isComposite()) 

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

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

86 

87 # Override with False 

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

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

90 

91 # DatasetType that has no explicit entry 

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

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

94 

95 # StorageClass that will be disassembled 

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

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

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

99 

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

101 with self.assertRaises(ValueError): 

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

103 

104 

105if __name__ == "__main__": 

106 unittest.main()