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

# -*- coding: UTF-8 -*- 

 

# 

# LSST Data Management System 

# Copyright 2016 LSST Corporation. 

# 

# This product includes software developed by the 

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

# 

# 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import lsst.daf.persistence as dp 

import lsst.utils.tests 

import os 

import shutil 

import unittest 

import urllib.parse 

import tempfile 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class MapperTest(dp.Mapper): 

pass 

 

 

# Define the root of the tests relative to this file 

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

 

 

class DefaultMapper(unittest.TestCase): 

"""Tests for finding the default mapper for a repository given different inputs. 

 

Butler should allow class objects, class instances , and importable strings to be passed in, and treat 

them as equivalent. 

 

Butler will find a default mapper only if all the inputs to the butler use the same mapper. If there are 

inputs with different mappers then the butler will not assume a default mapper and _getDefaultMapper 

will return None.""" 

 

def setUp(self): 

self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="DefaultMapper-") 

 

def tearDown(self): 

if os.path.exists(self.testDir): 

shutil.rmtree(self.testDir) 

 

def _setup(self, mapper1, mapper2): 

repo1Root = os.path.join(self.testDir, 'repo1') 

repo2Root = os.path.join(self.testDir, 'repo2') 

butler = dp.Butler(outputs=(dp.RepositoryArgs(root=repo1Root, mapper=mapper1))) 

del butler 

butler = dp.Butler(outputs=(dp.RepositoryArgs(root=repo2Root, mapper=mapper2))) 

del butler 

butler = dp.Butler(inputs=(repo1Root, repo2Root)) 

return butler 

 

def testClassObjAndMatchingString(self): 

"""Pass a class object and a string that evaluates to the same object, and verify a default mapper 

can be found.""" 

butler = self._setup(dp.Mapper, 'lsst.daf.persistence.Mapper') 

self.assertEqual(butler._getDefaultMapper(), lsst.daf.persistence.Mapper) 

 

def testClassObjAndNotMatchingString(self): 

"""Pass a class object and a non-matching string, and verify a default mapper can not be found.""" 

butler = self._setup(MapperTest, 'lsst.daf.persistence.Mapper') 

self.assertIsNone(butler._getDefaultMapper()) 

 

def testInstanceAndMatchingString(self): 

"""Pass a class instance and a string that evaluates to the same object, and verify a default mapper 

can be found.""" 

butler = self._setup(dp.Mapper, 'lsst.daf.persistence.Mapper') 

self.assertEqual(butler._getDefaultMapper(), lsst.daf.persistence.Mapper) 

 

def testInstanceAndNotMatchingString(self): 

"""Pass a class instance and a non-matching string, and verify a default mapper can not be found.""" 

butler = self._setup(MapperTest, 'lsst.daf.persistence.Mapper') 

self.assertIsNone(butler._getDefaultMapper()) 

 

def testClassObjAndMatchingInstance(self): 

"""Pass a class object and a class instance of the same type, and verify a default mapper can be 

found.""" 

butler = self._setup(dp.Mapper, dp.Mapper) 

self.assertEqual(butler._getDefaultMapper(), lsst.daf.persistence.Mapper) 

 

def testClassObjAndNotMatchingInstance(self): 

"""Pass a class object and a class instance of a different type, and verify a default mapper can not 

be found.""" 

butler = self._setup(MapperTest, dp.Mapper) 

self.assertIsNone(butler._getDefaultMapper()) 

 

 

class ParseRootURI(unittest.TestCase): 

"""Verify that supported root URI schemas are identified and build the correct Storage. 

""" 

def setUp(self): 

self.testDir = tempfile.mkdtemp(dir=ROOT, prefix="ParseRootURI-") 

 

def tearDown(self): 

if os.path.exists(self.testDir): 

shutil.rmtree(self.testDir) 

 

def testAbsoluteFilePathWithSchema(self): 

"""Test writing & reading an absolute path that begins with 'file://""" 

uri = urllib.parse.urljoin('file://', self.testDir) 

args = dp.RepositoryArgs(root=uri, mapper='lsst.daf.persistence.Mapper') 

butler = dp.Butler(outputs=args) 

self.assertIsInstance(butler, dp.Butler) 

self.assertTrue(os.path.exists(os.path.join(self.testDir, 'repositoryCfg.yaml'))) 

 

try: 

butler2 = dp.Butler(inputs=uri) 

self.assertIsInstance(butler2, dp.Butler) 

except RuntimeError: 

self.fail("Butler init raised a runtime error loading input %s" % uri) 

 

def testAbsoluteFilePathWithoutSchema(self): 

"""Test writing and reading an absolute path that begins with '/' (not 'file://')""" 

uri = self.testDir 

args = dp.RepositoryArgs(root=uri, mapper='lsst.daf.persistence.Mapper') 

butler = dp.Butler(outputs=args) 

self.assertIsInstance(butler, dp.Butler) 

self.assertTrue(os.path.exists(os.path.join(uri, 'repositoryCfg.yaml'))) 

 

try: 

butler2 = dp.Butler(inputs=uri) 

self.assertIsInstance(butler2, dp.Butler) 

except RuntimeError: 

self.fail("Butler init raised a runtime error loading input %s" % uri) 

 

def testRelativeFilePath(self): 

"""Test writing & reading a relative filepath. 

 

Relative filepaths can not start with 'file://' so there will be no relative file path test starting 

with the 'file' schema.""" 

uri = os.path.relpath(self.testDir) 

args = dp.RepositoryArgs(root=uri, mapper='lsst.daf.persistence.Mapper') 

butler = dp.Butler(outputs=args) 

self.assertIsInstance(butler, dp.Butler) 

self.assertTrue(self.testDir, 'repositoryCfg.yaml') 

 

try: 

butler2 = dp.Butler(inputs=uri) 

self.assertIsInstance(butler2, dp.Butler) 

except RuntimeError: 

self.fail("Butler init raised a runtime error loading input %s" % uri) 

 

 

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

pass 

 

 

168 ↛ 169line 168 didn't jump to line 169, because the condition on line 168 was never trueif __name__ == '__main__': 

lsst.utils.tests.init() 

unittest.main()