Coverage for tests/test_location.py : 18%

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# 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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22import unittest
23import os.path
24import posixpath
26from lsst.daf.butler import LocationFactory, ButlerURI
27from lsst.daf.butler.core.location import os2posix, posix2os
30class LocationTestCase(unittest.TestCase):
31 """Tests for Location within datastore
32 """
34 def testButlerUri(self):
36 # Root to use for relative paths
37 testRoot = "/tmp/"
39 uriStrings = (
40 # Test string, forceAbsolute, scheme, netloc, path
41 # These are being tested with forceAbsolute=True
42 ("file:///rootDir/absolute/file.ext", True, "file", "", "/rootDir/absolute/file.ext"),
43 ("/rootDir/absolute/file.ext", True, "file", "", "/rootDir/absolute/file.ext"),
44 ("/rootDir/absolute/file.ext", False, "file", "", "/rootDir/absolute/file.ext"),
45 ("/rootDir/absolute/", True, "file", "", "/rootDir/absolute/"),
46 ("file:relative/file.ext", True, "file", "", posixpath.join(testRoot, "relative/file.ext")),
47 ("file:relative/directory/", True, "file", "", posixpath.join(testRoot, "relative/directory/")),
48 ("file://relative/file.ext", True, "file", "relative", "/file.ext"),
49 ("file:///absolute/directory/", True, "file", "", "/absolute/directory/"),
50 ("relative/file.ext", True, "", "", os.path.join(testRoot, "relative/file.ext")),
51 ("relative/file.ext", False, "", "", "relative/file.ext"),
52 ("s3://bucketname/rootDir/relative/file.ext", True, "s3", "bucketname",
53 "/rootDir/relative/file.ext"),
54 ("~/relative/file.ext", True, "file", "", os.path.expanduser("~/relative/file.ext")),
55 ("~/relative/file.ext", False, "file", "", os.path.expanduser("~/relative/file.ext")),
56 ("test/../relative/file.ext", True, "", "", os.path.join(testRoot, "relative/file.ext")),
57 ("test/../relative/file.ext", False, "", "", "relative/file.ext"),
58 )
60 for uriInfo in uriStrings:
61 uri = ButlerURI(uriInfo[0], root=testRoot, forceAbsolute=uriInfo[1])
62 with self.subTest(uri=uriInfo[0]):
63 self.assertEqual(uri.scheme, uriInfo[2], "test scheme")
64 self.assertEqual(uri.netloc, uriInfo[3], "test netloc")
65 self.assertEqual(uri.path, uriInfo[4], "test path")
67 # File replacement
68 uriStrings = (
69 ("relative/file.ext", "newfile.fits", "relative/newfile.fits"),
70 ("relative/", "newfile.fits", "relative/newfile.fits"),
71 ("isThisADirOrFile", "aFile.fits", "aFile.fits"),
72 ("https://www.lsst.org/butler/", "butler.yaml", "/butler/butler.yaml"),
73 ("s3://amazon/datastore/", "butler.yaml", "/datastore/butler.yaml"),
74 ("s3://amazon/datastore/mybutler.yaml", "butler.yaml", "/datastore/butler.yaml"),
75 )
77 for uriInfo in uriStrings:
78 uri = ButlerURI(uriInfo[0], forceAbsolute=False)
79 uri.updateFile(uriInfo[1])
80 with self.subTest(uri=uriInfo[0]):
81 self.assertEqual(uri.path, uriInfo[2])
83 def testFileLocation(self):
84 root = os.path.abspath(os.path.curdir)
85 factory = LocationFactory(root)
86 print(f"Factory created: {factory}")
88 pathInStore = "relative/path/file.ext"
89 loc1 = factory.fromPath(pathInStore)
91 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
92 self.assertEqual(loc1.pathInStore, pathInStore)
93 self.assertTrue(loc1.uri.startswith("file:///"))
94 self.assertTrue(loc1.uri.endswith("file.ext"))
95 loc1.updateExtension("fits")
96 self.assertTrue(loc1.uri.endswith("file.fits"))
97 loc1.updateExtension(None)
98 self.assertTrue(loc1.uri.endswith("file.fits"))
99 loc1.updateExtension("")
100 self.assertTrue(loc1.uri.endswith("file"))
102 def testRelativeRoot(self):
103 root = os.path.abspath(os.path.curdir)
104 factory = LocationFactory(os.path.curdir)
105 print(f"Factory created: {factory}")
107 pathInStore = "relative/path/file.ext"
108 loc1 = factory.fromPath(pathInStore)
110 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
111 self.assertEqual(loc1.pathInStore, pathInStore)
112 self.assertTrue(loc1.uri.startswith("file:///"))
114 def testHttpLocation(self):
115 root = "https://www.lsst.org/butler/datastore"
116 factory = LocationFactory(root)
117 print(f"Factory created: {factory}")
119 pathInStore = "relative/path/file.ext"
120 loc1 = factory.fromPath(pathInStore)
122 self.assertEqual(loc1.path, posixpath.join("/butler/datastore", pathInStore))
123 self.assertEqual(loc1.pathInStore, pathInStore)
124 self.assertTrue(loc1.uri.startswith("https://"))
125 self.assertTrue(loc1.uri.endswith("file.ext"))
126 loc1.updateExtension("fits")
127 self.assertTrue(loc1.uri.endswith("file.fits"))
129 def testPosix2OS(self):
130 """Test round tripping of the posix to os.path conversion helpers."""
131 testPaths = ("/a/b/c.e", "a/b", "a/b/", "/a/b", "/a/b/", "a/b/c.e")
132 for p in testPaths:
133 with self.subTest(path=p):
134 self.assertEqual(os2posix(posix2os(p)), p)
137if __name__ == "__main__": 137 ↛ 138line 137 didn't jump to line 138, because the condition on line 137 was never true
138 unittest.main()