Coverage for tests/test_posixStorage.py: 41%

Shortcuts 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

85 statements  

1# 

2# LSST Data Management System 

3# Copyright 2017 LSST Corporation. 

4# 

5# This product includes software developed by the 

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

7# 

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

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

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

11# (at your option) any later version. 

12# 

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

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

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

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

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

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

21# 

22 

23import os 

24import unittest 

25import lsst.daf.persistence as dp 

26import lsst.utils.tests 

27import shutil 

28import tempfile 

29 

30try: 

31 FileType = file 

32except NameError: 

33 from io import IOBase 

34 FileType = IOBase 

35 

36 

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

38 

39 

40def setup_module(module): 

41 lsst.utils.tests.init() 

42 

43 

44class GetParentFromSymlink(unittest.TestCase): 

45 """A test case for getting the relative path to parent from a symlink in PosixStorage.""" 

46 

47 def setUp(self): 

48 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='GetParentFromSymlink-') 

49 self.parentFolderPath = os.path.join(self.testDir, "theParent") 

50 self.childFolderPath = os.path.join(self.testDir, "theChild") 

51 self.parentlessFolderPath = os.path.join(self.testDir, "parentlessRepo") 

52 for p in (self.parentFolderPath, self.childFolderPath, self.parentlessFolderPath): 

53 os.makedirs(p) 

54 relpath = os.path.relpath(self.parentFolderPath, self.childFolderPath) 

55 os.symlink(relpath, os.path.join(self.childFolderPath, '_parent')) 

56 

57 def tearDown(self): 

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

59 shutil.rmtree(self.testDir) 

60 

61 def testV1RepoWithParen(self): 

62 parentPath = dp.PosixStorage.getParentSymlinkPath(self.childFolderPath) 

63 self.assertEqual(parentPath, os.path.relpath(self.parentFolderPath, self.childFolderPath)) 

64 

65 def testV1RepoWithoutParent(self): 

66 parentPath = dp.PosixStorage.getParentSymlinkPath(self.parentlessFolderPath) 

67 self.assertEqual(parentPath, None) 

68 

69 

70class TestRelativePath(unittest.TestCase): 

71 """A test case for the PosixStorage.relativePath function.""" 

72 

73 def setUp(self): 

74 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='TestRelativePath-') 

75 

76 def tearDown(self): 

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

78 shutil.rmtree(self.testDir) 

79 

80 def testRelativePath(self): 

81 """Test that a relative path returns the correct relative path for 

82 1. relative inputs, 2. absolute inputs.""" 

83 abspathA = os.path.join(self.testDir, 'a') 

84 abspathB = os.path.join(self.testDir, 'b') 

85 os.makedirs(abspathA) 

86 os.makedirs(abspathB) 

87 # 1. 

88 relpathA = os.path.relpath(abspathA) 

89 relpathB = os.path.relpath(abspathB) 

90 relpathAtoB = dp.PosixStorage.relativePath(relpathA, relpathB) 

91 self.assertEqual('../b', relpathAtoB) 

92 # 2. 

93 relpathAtoB = dp.PosixStorage.relativePath(abspathA, abspathB) 

94 self.assertEqual('../b', relpathAtoB) 

95 

96 

97class TestAbsolutePath(unittest.TestCase): 

98 """A test case for the PosixStorage.absolutePath function.""" 

99 

100 def setUp(self): 

101 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='TestAbsolutePath-') 

102 

103 def tearDown(self): 

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

105 shutil.rmtree(self.testDir) 

106 

107 def testAbsolutePath(self): 

108 """Tests that given a path and a relative path, the correct aboslute 

109 path to the relative path is returned.""" 

110 abspathA = os.path.join(self.testDir, 'a') 

111 abspathB = os.path.join(self.testDir, 'b') 

112 os.makedirs(abspathA) 

113 os.makedirs(abspathB) 

114 relpathA = os.path.relpath(abspathA) 

115 self.assertEqual(abspathB, 

116 dp.PosixStorage.absolutePath(abspathA, '../b')) 

117 self.assertEqual(abspathB, 

118 dp.PosixStorage.absolutePath(relpathA, '../b')) 

119 

120 

121class TestGetLocalFile(unittest.TestCase): 

122 """A test case for the PosixStorage.getLocalFile function.""" 

123 

124 def setUp(self): 

125 self.testDir = tempfile.mkdtemp(dir=ROOT, prefix='TestGetLocalFile-') 

126 

127 def tearDown(self): 

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

129 shutil.rmtree(self.testDir) 

130 

131 def testAbsolutePath(self): 

132 """Tests that GetLocalFile returns a file when it exists and returns 

133 None when it does not exist.""" 

134 storage = dp.PosixStorage(self.testDir, create=True) 

135 self.assertIsNone(storage.getLocalFile('foo.txt')) 

136 with open(os.path.join(self.testDir, 'foo.txt'), 'w') as f: 

137 f.write('foobarbaz') 

138 del f 

139 f = storage.getLocalFile('foo.txt') 

140 self.assertIsInstance(f, FileType) 

141 self.assertEqual(f.read(), 'foobarbaz') 

142 f.close() 

143 

144 

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

146 pass 

147 

148 

149if __name__ == '__main__': 149 ↛ 150line 149 didn't jump to line 150, because the condition on line 149 was never true

150 lsst.utils.tests.init() 

151 unittest.main()