Coverage for tests/test_location.py: 14%
88 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-27 03:00 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-27 03:00 -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 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 copy
29import os.path
30import posixpath
31import unittest
33from lsst.daf.butler import Location, LocationFactory
34from lsst.resources import ResourcePath
37class LocationTestCase(unittest.TestCase):
38 """Tests for Location within datastore"""
40 def testFileLocation(self):
41 root = os.path.abspath(os.path.curdir)
42 factory = LocationFactory(root)
43 print(f"Factory created: {factory}")
45 pathInStore = "relative/path/file.ext"
46 loc1 = factory.fromPath(pathInStore)
48 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
49 self.assertEqual(loc1.pathInStore.path, pathInStore)
50 self.assertTrue(loc1.uri.geturl().startswith("file:///"))
51 self.assertTrue(loc1.uri.geturl().endswith("file.ext"))
52 loc1.updateExtension("fits")
53 self.assertTrue(loc1.uri.geturl().endswith("file.fits"), f"Checking 'fits' extension in {loc1.uri}")
54 loc1.updateExtension("fits.gz")
55 self.assertEqual(loc1.uri.basename(), "file.fits.gz")
56 self.assertTrue(
57 loc1.uri.geturl().endswith("file.fits.gz"), f"Checking 'fits.gz' extension in {loc1.uri}"
58 )
59 self.assertEqual(loc1.getExtension(), ".fits.gz")
60 loc1.updateExtension(".jpeg")
61 self.assertTrue(loc1.uri.geturl().endswith("file.jpeg"), f"Checking 'jpeg' extension in {loc1.uri}")
62 loc1.updateExtension(None)
63 self.assertTrue(
64 loc1.uri.geturl().endswith("file.jpeg"), f"Checking unchanged extension in {loc1.uri}"
65 )
66 loc1.updateExtension("")
67 self.assertTrue(loc1.uri.geturl().endswith("file"), f"Checking no extension in {loc1.uri}")
68 self.assertEqual(loc1.getExtension(), "")
70 loc2 = factory.fromPath(pathInStore)
71 loc3 = factory.fromPath(pathInStore)
72 self.assertEqual(loc2, loc3)
74 def test_clone(self):
75 """Test that cloning works."""
76 factory = LocationFactory(ResourcePath("."))
77 loc = factory.fromPath("test.txt")
78 loc2 = loc.clone()
79 self.assertIs(loc2._path, loc._path)
81 # Check that modifying one does not change the first.
82 loc2.updateExtension(".json")
83 self.assertTrue(loc.uri.path.endswith("test.txt"))
84 self.assertTrue(loc2.uri.path.endswith("test.json"))
86 # Test copying.
87 self.assertIs(copy.copy(loc)._path, loc._path)
88 self.assertIs(copy.deepcopy(loc)._path, loc._path)
90 def testAbsoluteLocations(self):
91 """Using a pathInStore that refers to absolute URI."""
92 loc = Location(None, "file:///something.txt")
93 self.assertEqual(loc.pathInStore.path, "/something.txt")
94 self.assertEqual(str(loc.uri), "file:///something.txt")
96 with self.assertRaises(ValueError):
97 Location(None, "relative.txt")
99 def testBadLocations(self):
100 with self.assertRaises(ValueError):
101 Location([1, 2, 3], "something.txt")
103 with self.assertRaises(ValueError):
104 Location(ResourcePath("a/b/c", forceAbsolute=False), "d.txt")
106 with self.assertRaises(ValueError):
107 Location("/a/c", "/a/c")
109 def testRelativeRoot(self):
110 root = os.path.abspath(os.path.curdir)
111 factory = LocationFactory(os.path.curdir)
113 pathInStore = "relative/path/file.ext"
114 loc1 = factory.fromPath(pathInStore)
116 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
117 self.assertEqual(loc1.pathInStore.path, pathInStore)
118 self.assertEqual(loc1.uri.scheme, "file")
120 with self.assertRaises(ValueError):
121 factory.fromPath("../something")
123 def testQuotedRoot(self):
124 """Test we can handle quoted characters."""
125 root = "/a/b/c+1/d"
126 factory = LocationFactory(root)
128 pathInStore = "relative/path/file.ext.gz"
130 for pathInStore in (
131 "relative/path/file.ext.gz",
132 "relative/path+2/file.ext.gz",
133 "relative/path+3/file&.ext.gz",
134 ):
135 loc1 = factory.fromPath(pathInStore)
137 self.assertEqual(loc1.pathInStore.path, pathInStore)
138 self.assertEqual(loc1.path, os.path.join(root, pathInStore))
139 self.assertIn("%", str(loc1.uri))
140 self.assertEqual(loc1.getExtension(), ".ext.gz")
142 def testHttpLocation(self):
143 root = "https://www.lsst.org/butler/datastore"
144 factory = LocationFactory(root)
145 print(f"Factory created: {factory}")
147 pathInStore = "relative/path/file.ext"
148 loc1 = factory.fromPath(pathInStore)
150 self.assertEqual(loc1.path, posixpath.join("/butler/datastore", pathInStore))
151 self.assertEqual(loc1.pathInStore.path, pathInStore)
152 self.assertEqual(loc1.uri.scheme, "https")
153 self.assertEqual(loc1.uri.basename(), "file.ext")
154 loc1.updateExtension("fits")
155 self.assertTrue(loc1.uri.basename(), "file.fits")
158if __name__ == "__main__":
159 unittest.main()