Coverage for tests/test_location.py: 17%
79 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-15 02:06 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-15 02:06 -0700
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 os.path
23import posixpath
24import unittest
26from lsst.daf.butler import Location, LocationFactory
27from lsst.resources import ResourcePath
30class LocationTestCase(unittest.TestCase):
31 """Tests for Location within datastore"""
33 def testFileLocation(self):
34 root = os.path.abspath(os.path.curdir)
35 factory = LocationFactory(root)
36 print(f"Factory created: {factory}")
38 pathInStore = "relative/path/file.ext"
39 loc1 = factory.fromPath(pathInStore)
41 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
42 self.assertEqual(loc1.pathInStore.path, pathInStore)
43 self.assertTrue(loc1.uri.geturl().startswith("file:///"))
44 self.assertTrue(loc1.uri.geturl().endswith("file.ext"))
45 loc1.updateExtension("fits")
46 self.assertTrue(loc1.uri.geturl().endswith("file.fits"), f"Checking 'fits' extension in {loc1.uri}")
47 loc1.updateExtension("fits.gz")
48 self.assertEqual(loc1.uri.basename(), "file.fits.gz")
49 self.assertTrue(
50 loc1.uri.geturl().endswith("file.fits.gz"), f"Checking 'fits.gz' extension in {loc1.uri}"
51 )
52 self.assertEqual(loc1.getExtension(), ".fits.gz")
53 loc1.updateExtension(".jpeg")
54 self.assertTrue(loc1.uri.geturl().endswith("file.jpeg"), f"Checking 'jpeg' extension in {loc1.uri}")
55 loc1.updateExtension(None)
56 self.assertTrue(
57 loc1.uri.geturl().endswith("file.jpeg"), f"Checking unchanged extension in {loc1.uri}"
58 )
59 loc1.updateExtension("")
60 self.assertTrue(loc1.uri.geturl().endswith("file"), f"Checking no extension in {loc1.uri}")
61 self.assertEqual(loc1.getExtension(), "")
63 loc2 = factory.fromPath(pathInStore)
64 loc3 = factory.fromPath(pathInStore)
65 self.assertEqual(loc2, loc3)
67 def testAbsoluteLocations(self):
68 """Using a pathInStore that refers to absolute URI."""
69 loc = Location(None, "file:///something.txt")
70 self.assertEqual(loc.pathInStore.path, "/something.txt")
71 self.assertEqual(str(loc.uri), "file:///something.txt")
73 with self.assertRaises(ValueError):
74 Location(None, "relative.txt")
76 def testBadLocations(self):
77 with self.assertRaises(ValueError):
78 Location([1, 2, 3], "something.txt")
80 with self.assertRaises(ValueError):
81 Location(ResourcePath("a/b/c", forceAbsolute=False), "d.txt")
83 with self.assertRaises(ValueError):
84 Location("/a/c", "/a/c")
86 def testRelativeRoot(self):
87 root = os.path.abspath(os.path.curdir)
88 factory = LocationFactory(os.path.curdir)
90 pathInStore = "relative/path/file.ext"
91 loc1 = factory.fromPath(pathInStore)
93 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
94 self.assertEqual(loc1.pathInStore.path, pathInStore)
95 self.assertEqual(loc1.uri.scheme, "file")
97 with self.assertRaises(ValueError):
98 factory.fromPath("../something")
100 def testQuotedRoot(self):
101 """Test we can handle quoted characters."""
102 root = "/a/b/c+1/d"
103 factory = LocationFactory(root)
105 pathInStore = "relative/path/file.ext.gz"
107 for pathInStore in (
108 "relative/path/file.ext.gz",
109 "relative/path+2/file.ext.gz",
110 "relative/path+3/file&.ext.gz",
111 ):
112 loc1 = factory.fromPath(pathInStore)
114 self.assertEqual(loc1.pathInStore.path, pathInStore)
115 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
116 self.assertIn("%", str(loc1.uri))
117 self.assertEqual(loc1.getExtension(), ".ext.gz")
119 def testHttpLocation(self):
120 root = "https://www.lsst.org/butler/datastore"
121 factory = LocationFactory(root)
122 print(f"Factory created: {factory}")
124 pathInStore = "relative/path/file.ext"
125 loc1 = factory.fromPath(pathInStore)
127 self.assertEqual(loc1.path, posixpath.join("/butler/datastore", pathInStore))
128 self.assertEqual(loc1.pathInStore.path, pathInStore)
129 self.assertEqual(loc1.uri.scheme, "https")
130 self.assertEqual(loc1.uri.basename(), "file.ext")
131 loc1.updateExtension("fits")
132 self.assertTrue(loc1.uri.basename(), "file.fits")
135if __name__ == "__main__": 135 ↛ 136line 135 didn't jump to line 136, because the condition on line 135 was never true
136 unittest.main()