Coverage for tests/test_schemeless.py: 12%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-25 09:29 +0000

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. 

11 

12import unittest 

13 

14from lsst.resources import ResourcePath 

15 

16 

17class SchemelessTestCase(unittest.TestCase): 

18 """Test the behavior of a schemeless URI.""" 

19 

20 def test_creation(self) -> None: 

21 """Test creation from schemeless URI.""" 

22 relative = "a/b/c.txt" 

23 abspath = "/a/b/c.txt" 

24 

25 relative_uri = ResourcePath(relative, forceAbsolute=False) 

26 self.assertFalse(relative_uri.scheme) 

27 self.assertFalse(relative_uri.isabs()) 

28 self.assertEqual(relative_uri.ospath, relative) 

29 

30 # Converted to a file URI. 

31 abs_uri = ResourcePath(relative, forceAbsolute=True) 

32 self.assertEqual(abs_uri.scheme, "file") 

33 self.assertTrue(abs_uri.isabs()) 

34 

35 # An absolute path is converted to a file URI. 

36 file_uri = ResourcePath(abspath) 

37 self.assertEqual(file_uri.scheme, "file") 

38 self.assertTrue(file_uri.isabs()) 

39 

40 # Use a prefix root. 

41 prefix = "/a/b/" 

42 abs_uri = ResourcePath(relative, root=prefix) 

43 self.assertEqual(abs_uri.ospath, f"{prefix}{relative}") 

44 self.assertEqual(abs_uri.scheme, "file") 

45 

46 # Use a file prefix. 

47 prefix = "file://localhost/a/b/" 

48 prefix_uri = ResourcePath(prefix) 

49 file_uri = ResourcePath(relative, root=prefix_uri) 

50 self.assertEqual(str(file_uri), f"file://{prefix_uri.ospath}{relative}") 

51 

52 # Fragments should be fine. 

53 relative_uri = ResourcePath(relative + "#frag", forceAbsolute=False) 

54 self.assertEqual(str(relative_uri), f"{relative}#frag") 

55 

56 file_uri = ResourcePath(relative + "#frag", root=prefix_uri) 

57 self.assertEqual(str(file_uri), f"file://{prefix_uri.ospath}{relative}#frag") 

58 

59 # For historical reasons a a root can not be anything other 

60 # than a file. This does not really make sense in the general 

61 # sense but can be implemented using uri.join(). 

62 with self.assertRaises(ValueError): 

63 ResourcePath(relative, root=ResourcePath("resource://lsst.resources/something.txt")) 

64 

65 

66if __name__ == "__main__": 

67 unittest.main()