Coverage for tests/test_resource.py: 31%
45 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-13 02:31 -0700
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-13 02:31 -0700
1# This file is part of lsst-resources.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import unittest
14from lsst.resources import ResourcePath
15from lsst.resources.tests import GenericTestCase
18class ResourceTestCase(GenericTestCase, unittest.TestCase):
19 scheme = "resource"
20 netloc = "lsst.resources"
23class ResourceReadTestCase(unittest.TestCase):
24 """Test that resource information can be read.
26 Python package resources are read-only.
27 """
29 # No resources in this package so need a resource in the main
30 # python distribution.
31 scheme = "resource"
32 netloc = "idlelib"
34 def setUp(self):
35 self.root = f"{self.scheme}://{self.netloc}"
36 self.root_uri = ResourcePath(self.root)
38 def test_read(self):
39 uri = self.root_uri.join("Icons/README.txt")
40 self.assertTrue(uri.exists(), f"Check {uri} exists")
42 content = uri.read().decode()
43 self.assertIn("IDLE", content)
45 truncated = uri.read(size=9).decode()
46 self.assertEqual(truncated, content[:9])
48 d = self.root_uri.join("Icons/", forceDirectory=True)
49 self.assertTrue(uri.exists(), f"Check directory {d} exists")
51 j = d.join("README.txt")
52 self.assertEqual(uri, j)
53 self.assertFalse(j.dirLike)
54 self.assertFalse(j.isdir())
55 not_there = d.join("not-there.yaml")
56 self.assertFalse(not_there.exists())
58 bad = ResourcePath(f"{self.scheme}://bad.module/not.yaml")
59 multi = ResourcePath.mexists([uri, bad, not_there])
60 self.assertTrue(multi[uri])
61 self.assertFalse(multi[bad])
62 self.assertFalse(multi[not_there])
64 def test_open(self):
65 uri = self.root_uri.join("Icons/README.txt")
66 with uri.open("rb") as buffer:
67 content = buffer.read()
68 self.assertEqual(uri.read(), content)
70 with uri.open("r") as buffer:
71 content = buffer.read()
72 self.assertEqual(uri.read().decode(), content)
74 # Read only.
75 with self.assertRaises(RuntimeError):
76 with uri.open("w") as buffer:
77 pass
80if __name__ == "__main__": 80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never true
81 unittest.main()