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

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

# 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/>. 

 

"""Tests for Butler. 

""" 

 

import os 

import unittest 

import tempfile 

import shutil 

import pickle 

 

import lsst.utils.tests 

 

from lsst.daf.butler import Butler, Config 

from lsst.daf.butler import StorageClassFactory 

from lsst.daf.butler import DatasetType, DatasetRef 

from examplePythonTypes import MetricsExample 

 

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

 

 

def makeExampleMetrics(): 

return MetricsExample({"AM1": 5.2, "AM2": 30.6}, 

{"a": [1, 2, 3], 

"b": {"blue": 5, "red": "green"}}, 

[563, 234, 456.7, 752, 8, 9, 27] 

) 

 

 

class TransactionTestError(Exception): 

"""Specific error for testing transactions, to prevent misdiagnosing 

that might otherwise occur when a standard exception is used. 

""" 

pass 

 

 

class ButlerTests: 

"""Tests for Butler. 

""" 

useTempRoot = True 

 

@staticmethod 

def addDatasetType(datasetTypeName, dataUnits, storageClass, registry): 

"""Create a DatasetType and register it 

""" 

datasetType = DatasetType(datasetTypeName, dataUnits, storageClass) 

registry.registerDatasetType(datasetType) 

return datasetType 

 

@classmethod 

def setUpClass(cls): 

cls.storageClassFactory = StorageClassFactory() 

cls.storageClassFactory.addFromConfig(cls.configFile) 

 

def assertGetComponents(self, butler, datasetRef, components, reference): 

datasetTypeName = datasetRef.datasetType.name 

dataId = datasetRef.dataId 

for component in components: 

compTypeName = DatasetType.nameWithComponent(datasetTypeName, component) 

result = butler.get(compTypeName, dataId) 

self.assertEqual(result, getattr(reference, component)) 

 

def setUp(self): 

"""Create a new butler root for each test.""" 

if self.useTempRoot: 

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

Butler.makeRepo(self.root, config=Config(self.configFile)) 

self.tmpConfigFile = os.path.join(self.root, "butler.yaml") 

else: 

self.root = None 

self.tmpConfigFile = self.configFile 

 

def tearDown(self): 

if self.root is not None and os.path.exists(self.root): 

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

 

def testConstructor(self): 

"""Independent test of constructor. 

""" 

butler = Butler(self.tmpConfigFile) 

self.assertIsInstance(butler, Butler) 

 

def testBasicPutGet(self): 

storageClass = self.storageClassFactory.getStorageClass("StructuredDataNoComponents") 

self.runPutGetTest(storageClass, "test_metric") 

 

def testCompositePutGetConcrete(self): 

storageClass = self.storageClassFactory.getStorageClass("StructuredData") 

self.runPutGetTest(storageClass, "test_metric") 

 

def testCompositePutGetVirtual(self): 

storageClass = self.storageClassFactory.getStorageClass("StructuredComposite") 

self.runPutGetTest(storageClass, "test_metric_comp") 

 

def runPutGetTest(self, storageClass, datasetTypeName): 

butler = Butler(self.tmpConfigFile) 

# Create and register a DatasetType 

dataUnits = ("Camera", "Visit") 

 

# We can not delete datasets so for now create two so we can do 

# two puts. 

self.addDatasetType(datasetTypeName, dataUnits, storageClass, butler.registry) 

 

datasetTypeName2 = datasetTypeName + "2" 

self.addDatasetType(datasetTypeName2, dataUnits, storageClass, butler.registry) 

 

# Add a third type to test putting with a DataSetType 

datasetTypeName3 = datasetTypeName + "3" 

self.addDatasetType(datasetTypeName3, dataUnits, storageClass, butler.registry) 

 

# Add needed DataUnits 

butler.registry.addDataUnitEntry("Camera", {"camera": "DummyCamComp"}) 

butler.registry.addDataUnitEntry("PhysicalFilter", {"camera": "DummyCamComp", 

"physical_filter": "d-r"}) 

butler.registry.addDataUnitEntry("Visit", {"camera": "DummyCamComp", "visit": 423, 

"physical_filter": "d-r"}) 

 

# Create and store a dataset 

metric = makeExampleMetrics() 

dataId = {"camera": "DummyCamComp", "visit": 423} 

 

# Create a DatasetRef for put 

datasetType2 = butler.registry.getDatasetType(datasetTypeName2) 

ref2 = DatasetRef(datasetType2, dataId, id=None) 

 

datasetType3 = butler.registry.getDatasetType(datasetTypeName3) 

 

# Put with a preexisting id should fail 

with self.assertRaises(ValueError): 

butler.put(metric, DatasetRef(datasetType2, dataId, id=100)) 

 

# Put the dataset once as a DatasetRef, once as a dataId, and once with a DataSetType 

for args in ((ref2,), (datasetTypeName, dataId), (datasetType3, dataId)): 

ref = butler.put(metric, *args) 

self.assertIsInstance(ref, DatasetRef) 

 

# Test getDirect 

metricOut = butler.getDirect(ref) 

self.assertEqual(metric, metricOut) 

# Test get 

metricOut = butler.get(ref.datasetType.name, dataId) 

self.assertEqual(metric, metricOut) 

# Test get with a datasetRef 

metricOut = butler.get(ref) 

self.assertEqual(metric, metricOut) 

 

# Check we can get components 

if storageClass.isComposite(): 

self.assertGetComponents(butler, ref, 

("summary", "data", "output"), metric) 

 

# Get with parameters 

stop = 4 

sliced = butler.get(ref2, parameters={"slice": slice(stop)}) 

self.assertNotEqual(metric, sliced) 

self.assertEqual(metric.summary, sliced.summary) 

self.assertEqual(metric.output, sliced.output) 

self.assertEqual(metric.data[:stop], sliced.data) 

 

# Combining a DatasetRef with a dataId should fail 

with self.assertRaises(ValueError): 

butler.get(ref, dataId) 

# Getting with an explicit ref should fail if the id doesn't match 

with self.assertRaises(ValueError): 

butler.get(DatasetRef(ref.datasetType, ref.dataId, id=101)) 

 

# Getting a dataset with unknown parameters should fail 

with self.assertRaises(KeyError): 

butler.get(ref, parameters={"unsupported": True}) 

 

def testPickle(self): 

"""Test pickle support. 

""" 

butler = Butler(self.tmpConfigFile) 

butlerOut = pickle.loads(pickle.dumps(butler)) 

self.assertIsInstance(butlerOut, Butler) 

self.assertEqual(butlerOut.config, butler.config) 

 

def testTransaction(self): 

butler = Butler(self.tmpConfigFile) 

datasetTypeName = "test_metric" 

dataUnits = ("Camera", "Visit") 

dataUnitEntries = (("Camera", {"camera": "DummyCam"}), 

("PhysicalFilter", {"camera": "DummyCam", "physical_filter": "d-r"}), 

("Visit", {"camera": "DummyCam", "visit": 42, "physical_filter": "d-r"})) 

storageClass = self.storageClassFactory.getStorageClass("StructuredData") 

metric = makeExampleMetrics() 

dataId = {"camera": "DummyCam", "visit": 42} 

with self.assertRaises(TransactionTestError): 

with butler.transaction(): 

# Create and register a DatasetType 

datasetType = self.addDatasetType(datasetTypeName, dataUnits, storageClass, butler.registry) 

# Add needed DataUnits 

for name, value in dataUnitEntries: 

butler.registry.addDataUnitEntry(name, value) 

# Store a dataset 

ref = butler.put(metric, datasetTypeName, dataId) 

self.assertIsInstance(ref, DatasetRef) 

# Test getDirect 

metricOut = butler.getDirect(ref) 

self.assertEqual(metric, metricOut) 

# Test get 

metricOut = butler.get(datasetTypeName, dataId) 

self.assertEqual(metric, metricOut) 

# Check we can get components 

self.assertGetComponents(butler, ref, 

("summary", "data", "output"), metric) 

raise TransactionTestError("This should roll back the entire transaction") 

 

with self.assertRaises(KeyError): 

butler.registry.getDatasetType(datasetTypeName) 

for name, value in dataUnitEntries: 

self.assertIsNone(butler.registry.findDataUnitEntry(name, value)) 

# Should raise KeyError for missing DatasetType 

with self.assertRaises(KeyError): 

butler.get(datasetTypeName, dataId) 

# Also check explicitly if Dataset entry is missing 

self.assertIsNone(butler.registry.find(butler.collection, datasetType, dataId)) 

# Direct retrieval should not find the file in the Datastore 

with self.assertRaises(FileNotFoundError): 

butler.getDirect(ref) 

 

def testMakeRepo(self): 

"""Test that we can write butler configuration to a new repository via 

the Butler.makeRepo interface and then instantiate a butler from the 

repo root. 

""" 

# Do not run the test if we know this datastore configuration does 

# not support a file system root 

if self.fullConfigKey is None: 

return 

 

# Remove the file created in setUp 

os.unlink(self.tmpConfigFile) 

 

Butler.makeRepo(self.root, config=Config(self.configFile)) 

limited = Config(self.configFile) 

butler1 = Butler(self.root, collection="ingest") 

Butler.makeRepo(self.root, standalone=True, createRegistry=False, 

config=Config(self.configFile)) 

full = Config(self.tmpConfigFile) 

butler2 = Butler(self.root, collection="ingest") 

# Butlers should have the same configuration regardless of whether 

# defaults were expanded. 

self.assertEqual(butler1.config, butler2.config) 

# Config files loaded directly should not be the same. 

self.assertNotEqual(limited, full) 

# Make sure "limited" doesn't have a few keys we know it should be 

# inheriting from defaults. 

self.assertIn(self.fullConfigKey, full) 

self.assertNotIn(self.fullConfigKey, limited) 

 

def testStringification(self): 

butler = Butler(self.configFile) 

if self.datastoreStr is not None: 

self.assertIn(self.datastoreStr, str(butler)) 

if self.registryStr is not None: 

self.assertIn(self.registryStr, str(butler)) 

 

 

class PosixDatastoreButlerTestCase(ButlerTests, lsst.utils.tests.TestCase): 

"""PosixDatastore specialization of a butler""" 

configFile = os.path.join(TESTDIR, "config/basic/butler.yaml") 

fullConfigKey = ".datastore.formatters" 

 

datastoreStr = "datastore='./butler_test_repository" 

registryStr = "registry='sqlite:///:memory:'" 

 

 

class InMemoryDatastoreButlerTestCase(ButlerTests, lsst.utils.tests.TestCase): 

"""InMemoryDatastore specialization of a butler""" 

configFile = os.path.join(TESTDIR, "config/basic/butler-inmemory.yaml") 

fullConfigKey = None 

useTempRoot = False 

datastoreStr = "datastore='InMemory'" 

registryStr = "registry='sqlite:///:memory:'" 

 

 

class ChainedDatastoreButlerTestCase(ButlerTests, lsst.utils.tests.TestCase): 

"""PosixDatastore specialization""" 

configFile = os.path.join(TESTDIR, "config/basic/butler-chained.yaml") 

fullConfigKey = ".datastore.datastores.1.formatters" 

datastoreStr = "datastore='InMemory, ./butler_test_repository, ./butler_test_repository2'" 

registryStr = "registry='sqlite:///:memory:'" 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()