Coverage for tests/test_location.py: 13%
77 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-06 10:53 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-06 10:53 +0000
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import os.path
29import posixpath
30import unittest
32from lsst.daf.butler import Location, LocationFactory
33from lsst.resources import ResourcePath
36class LocationTestCase(unittest.TestCase):
37 """Tests for Location within datastore"""
39 def testFileLocation(self):
40 root = os.path.abspath(os.path.curdir)
41 factory = LocationFactory(root)
42 print(f"Factory created: {factory}")
44 pathInStore = "relative/path/file.ext"
45 loc1 = factory.fromPath(pathInStore)
47 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
48 self.assertEqual(loc1.pathInStore.path, pathInStore)
49 self.assertTrue(loc1.uri.geturl().startswith("file:///"))
50 self.assertTrue(loc1.uri.geturl().endswith("file.ext"))
51 loc1.updateExtension("fits")
52 self.assertTrue(loc1.uri.geturl().endswith("file.fits"), f"Checking 'fits' extension in {loc1.uri}")
53 loc1.updateExtension("fits.gz")
54 self.assertEqual(loc1.uri.basename(), "file.fits.gz")
55 self.assertTrue(
56 loc1.uri.geturl().endswith("file.fits.gz"), f"Checking 'fits.gz' extension in {loc1.uri}"
57 )
58 self.assertEqual(loc1.getExtension(), ".fits.gz")
59 loc1.updateExtension(".jpeg")
60 self.assertTrue(loc1.uri.geturl().endswith("file.jpeg"), f"Checking 'jpeg' extension in {loc1.uri}")
61 loc1.updateExtension(None)
62 self.assertTrue(
63 loc1.uri.geturl().endswith("file.jpeg"), f"Checking unchanged extension in {loc1.uri}"
64 )
65 loc1.updateExtension("")
66 self.assertTrue(loc1.uri.geturl().endswith("file"), f"Checking no extension in {loc1.uri}")
67 self.assertEqual(loc1.getExtension(), "")
69 loc2 = factory.fromPath(pathInStore)
70 loc3 = factory.fromPath(pathInStore)
71 self.assertEqual(loc2, loc3)
73 def testAbsoluteLocations(self):
74 """Using a pathInStore that refers to absolute URI."""
75 loc = Location(None, "file:///something.txt")
76 self.assertEqual(loc.pathInStore.path, "/something.txt")
77 self.assertEqual(str(loc.uri), "file:///something.txt")
79 with self.assertRaises(ValueError):
80 Location(None, "relative.txt")
82 def testBadLocations(self):
83 with self.assertRaises(ValueError):
84 Location([1, 2, 3], "something.txt")
86 with self.assertRaises(ValueError):
87 Location(ResourcePath("a/b/c", forceAbsolute=False), "d.txt")
89 with self.assertRaises(ValueError):
90 Location("/a/c", "/a/c")
92 def testRelativeRoot(self):
93 root = os.path.abspath(os.path.curdir)
94 factory = LocationFactory(os.path.curdir)
96 pathInStore = "relative/path/file.ext"
97 loc1 = factory.fromPath(pathInStore)
99 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
100 self.assertEqual(loc1.pathInStore.path, pathInStore)
101 self.assertEqual(loc1.uri.scheme, "file")
103 with self.assertRaises(ValueError):
104 factory.fromPath("../something")
106 def testQuotedRoot(self):
107 """Test we can handle quoted characters."""
108 root = "/a/b/c+1/d"
109 factory = LocationFactory(root)
111 pathInStore = "relative/path/file.ext.gz"
113 for pathInStore in (
114 "relative/path/file.ext.gz",
115 "relative/path+2/file.ext.gz",
116 "relative/path+3/file&.ext.gz",
117 ):
118 loc1 = factory.fromPath(pathInStore)
120 self.assertEqual(loc1.pathInStore.path, pathInStore)
121 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
122 self.assertIn("%", str(loc1.uri))
123 self.assertEqual(loc1.getExtension(), ".ext.gz")
125 def testHttpLocation(self):
126 root = "https://www.lsst.org/butler/datastore"
127 factory = LocationFactory(root)
128 print(f"Factory created: {factory}")
130 pathInStore = "relative/path/file.ext"
131 loc1 = factory.fromPath(pathInStore)
133 self.assertEqual(loc1.path, posixpath.join("/butler/datastore", pathInStore))
134 self.assertEqual(loc1.pathInStore.path, pathInStore)
135 self.assertEqual(loc1.uri.scheme, "https")
136 self.assertEqual(loc1.uri.basename(), "file.ext")
137 loc1.updateExtension("fits")
138 self.assertTrue(loc1.uri.basename(), "file.fits")
141if __name__ == "__main__":
142 unittest.main()