Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

 

"""Unit tests for `lsst.daf.butler.tests.testRepo`, a module for creating 

test repositories or butlers. 

""" 

 

import os 

import shutil 

import tempfile 

import unittest 

 

import lsst.daf.butler 

from lsst.daf.butler.tests import (makeTestRepo, makeTestCollection, addDatasetType, expandUniqueId, 

MetricsExample, registerMetricsExample) 

 

 

TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

 

 

class ButlerUtilsTestSuite(unittest.TestCase): 

@classmethod 

def setUpClass(cls): 

# Repository should be re-created for each test case, but 

# this has a prohibitive run-time cost at present 

cls.root = tempfile.mkdtemp(dir=TESTDIR) 

 

dataIds = { 

"instrument": ["notACam", "dummyCam"], 

"physical_filter": ["k2020", "l2019"], 

"visit": [101, 102], 

"detector": [5] 

} 

cls.creatorButler = makeTestRepo(cls.root, dataIds) 

 

registerMetricsExample(cls.creatorButler) 

addDatasetType(cls.creatorButler, "DataType1", {"instrument"}, "StructuredDataNoComponents") 

addDatasetType(cls.creatorButler, "DataType2", {"instrument", "visit"}, "StructuredData") 

 

@classmethod 

def tearDownClass(cls): 

# TODO: use addClassCleanup rather than tearDownClass in Python 3.8 

# to keep the addition and removal together and make it more robust 

shutil.rmtree(cls.root, ignore_errors=True) 

 

def setUp(self): 

self.butler = makeTestCollection(self.creatorButler) 

 

def testButlerValid(self): 

self.butler.validateConfiguration() 

 

def testButlerKwargs(self): 

# outfile has the most obvious effects of any Butler.makeRepo keyword 

temp = tempfile.mkdtemp(dir=TESTDIR) 

try: 

path = os.path.join(temp, 'oddConfig.py') 

makeTestRepo(self.root, {}, outfile=path) 

self.assertTrue(os.path.isfile(path)) 

finally: 

shutil.rmtree(temp, ignore_errors=True) 

 

def _checkButlerDimension(self, dimensions, query, expected): 

result = [id for id in self.butler.registry.queryDimensions( 

dimensions, 

where=query, 

expand=False)] 

self.assertEqual(len(result), 1) 

self.assertIn(dict(result[0]), expected) 

 

def testButlerDimensions(self): 

self. _checkButlerDimension({"instrument"}, 

"instrument='notACam'", 

[{"instrument": "notACam"}, {"instrument": "dummyCam"}]) 

self. _checkButlerDimension({"visit", "instrument"}, 

"visit=101", 

[{"instrument": "notACam", "visit": 101}, 

{"instrument": "dummyCam", "visit": 101}]) 

self. _checkButlerDimension({"visit", "instrument"}, 

"visit=102", 

[{"instrument": "notACam", "visit": 102}, 

{"instrument": "dummyCam", "visit": 102}]) 

self. _checkButlerDimension({"detector", "instrument"}, 

"detector=5", 

[{"instrument": "notACam", "detector": 5}, 

{"instrument": "dummyCam", "detector": 5}]) 

 

def testAddDatasetType(self): 

# 1 for StructuredDataNoComponents, 4 for StructuredData 

self.assertEqual(len(self.butler.registry.getAllDatasetTypes()), 5) 

 

# Testing the DatasetType objects is not practical, because all tests 

# need a DimensionUniverse. So just check that we have the dataset 

# types we expect. 

self.butler.registry.getDatasetType("DataType1") 

self.butler.registry.getDatasetType("DataType2") 

 

with self.assertRaises(ValueError): 

addDatasetType(self.butler, "DataType3", {"4thDimension"}, "NumpyArray") 

with self.assertRaises(ValueError): 

addDatasetType(self.butler, "DataType3", {"instrument"}, "UnstorableType") 

 

def testRegisterMetricsExample(self): 

id1 = {"instrument": "notACam"} 

id2 = expandUniqueId(self.butler, {"visit": 101}) 

data = MetricsExample(summary={"answer": 42, "question": "unknown"}) 

 

self.butler.put(data, "DataType1", id1) 

self.assertEqual(self.butler.get("DataType1", id1), data) 

 

self.butler.put(data, "DataType2", id2) 

self.assertEqual(self.butler.get("DataType2", id2), data) 

self.assertEqual(self.butler.get("DataType2.summary", id2), data.summary) 

 

def testRegisterMetricsExampleChained(self): 

"""Regression test for registerMetricsExample having no effect 

on ChainedDatastore. 

""" 

temp = tempfile.mkdtemp(dir=TESTDIR) 

try: 

config = lsst.daf.butler.Config() 

config["datastore", "cls"] = "lsst.daf.butler.datastores.chainedDatastore.ChainedDatastore" 

config["datastore", "datastores"] = [{ 

"cls": "lsst.daf.butler.datastores.posixDatastore.PosixDatastore", 

}] 

 

repo = lsst.daf.butler.Butler.makeRepo(temp, config=config) 

butler = lsst.daf.butler.Butler(repo, run="chainedExample") 

registerMetricsExample(butler) 

addDatasetType(butler, "DummyType", {}, "StructuredDataNoComponents") 

 

data = MetricsExample(summary={}) 

# Should not raise 

butler.put(data, "DummyType") 

finally: 

shutil.rmtree(temp, ignore_errors=True) 

 

def testUniqueButler(self): 

dataId = {"instrument": "notACam"} 

self.butler.put(MetricsExample({"answer": 42, "question": "unknown"}), "DataType1", dataId) 

self.assertTrue(self.butler.datasetExists("DataType1", dataId)) 

 

newButler = makeTestCollection(self.creatorButler) 

with self.assertRaises(LookupError): 

newButler.datasetExists("DataType1", dataId) 

 

def testExpandUniqueId(self): 

self.assertEqual(dict(expandUniqueId(self.butler, {"instrument": "notACam"})), 

{"instrument": "notACam"}) 

self.assertIn(dict(expandUniqueId(self.butler, {"visit": 101})), 

[{"instrument": "notACam", "visit": 101}, 

{"instrument": "dummyCam", "visit": 101}]) 

self.assertIn(dict(expandUniqueId(self.butler, {"detector": 5})), 

[{"instrument": "notACam", "detector": 5}, 

{"instrument": "dummyCam", "detector": 5}]) 

self.assertIn(dict(expandUniqueId(self.butler, {"physical_filter": "k2020"})), 

[{"instrument": "notACam", "physical_filter": "k2020"}, 

{"instrument": "notACam", "physical_filter": "k2020"}]) 

with self.assertRaises(ValueError): 

expandUniqueId(self.butler, {"tract": 42}) 

 

 

180 ↛ 181line 180 didn't jump to line 181, because the condition on line 180 was never trueif __name__ == "__main__": 

unittest.main()