Coverage for tests/test_index.py: 22%

50 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-16 03:20 -0700

1# This file is part of dax_apdb. 

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/>. 

21 

22import os 

23import shutil 

24import tempfile 

25import unittest 

26 

27from lsst.dax.apdb.apdbIndex import ApdbIndex 

28from lsst.resources import ResourcePath 

29 

30VALID_INDEX = """\ 

31dev: "/path/to/config-file.yaml" 

32"prod/pex_config": "s3://bucket/apdb-prod.py" 

33"prod/yaml": "s3://bucket/apdb-prod.yaml" 

34""" 

35 

36BAD_INDEX = """\ 

37prod: 

38 pex_config: "s3://bucket/apdb-prod.py" 

39 yaml: "s3://bucket/apdb-prod.yaml" 

40""" 

41 

42 

43class ApdbIndexTestCase(unittest.TestCase): 

44 """A test case for ApdbIndex class.""" 

45 

46 def setUp(self) -> None: 

47 self.tempdir = tempfile.mkdtemp() 

48 self.valid_index_path = os.path.join(self.tempdir, "apdb-index.yaml") 

49 with open(self.valid_index_path, "w") as index_file: 

50 index_file.write(VALID_INDEX) 

51 self.bad_index_path = os.path.join(self.tempdir, "bad-index.yaml") 

52 with open(self.bad_index_path, "w") as index_file: 

53 index_file.write(BAD_INDEX) 

54 self.missing_index_path = os.path.join(self.tempdir, "missing-index.yaml") 

55 

56 def tearDown(self) -> None: 

57 shutil.rmtree(self.tempdir, ignore_errors=True) 

58 

59 def test_get_apdb_uri(self) -> None: 

60 """Check get_apdb_uri against existing valid file.""" 

61 index = ApdbIndex(self.valid_index_path) 

62 self.assertEqual(index.get_apdb_uri("dev"), ResourcePath("/path/to/config-file.yaml")) 

63 self.assertEqual(index.get_apdb_uri("dev", "yaml"), ResourcePath("/path/to/config-file.yaml")) 

64 self.assertEqual(index.get_apdb_uri("dev", "anything"), ResourcePath("/path/to/config-file.yaml")) 

65 self.assertEqual(index.get_apdb_uri("prod/yaml"), ResourcePath("s3://bucket/apdb-prod.yaml")) 

66 self.assertEqual(index.get_apdb_uri("prod/pex_config"), ResourcePath("s3://bucket/apdb-prod.py")) 

67 with self.assertRaises(ValueError): 

68 index.get_apdb_uri("prod") 

69 with self.assertRaises(ValueError): 

70 index.get_apdb_uri("prod", "anything") 

71 with self.assertRaises(ValueError): 

72 index.get_apdb_uri("anything") 

73 

74 def test_missing_config(self) -> None: 

75 """Check get_apdb_uri when index file is missing.""" 

76 index = ApdbIndex(self.missing_index_path) 

77 with self.assertRaises(FileNotFoundError): 

78 index.get_apdb_uri("prod") 

79 

80 def test_bad_config(self) -> None: 

81 """Check get_apdb_uri when badly formatted YAML file.""" 

82 # Constructor does not read index, all errors happen in get_apdb_uri 

83 # during delayed initialization. 

84 index = ApdbIndex(self.bad_index_path) 

85 with self.assertRaises(TypeError): 

86 index.get_apdb_uri("prod") 

87 

88 def test_get_entries(self) -> None: 

89 """Check get_entries.""" 

90 index = ApdbIndex(self.valid_index_path) 

91 self.assertEqual( 

92 index.get_entries(), 

93 { 

94 "dev": "/path/to/config-file.yaml", 

95 "prod/pex_config": "s3://bucket/apdb-prod.py", 

96 "prod/yaml": "s3://bucket/apdb-prod.yaml", 

97 }, 

98 ) 

99 

100 index = ApdbIndex(self.bad_index_path) 

101 with self.assertRaises(TypeError): 

102 index.get_entries() 

103 

104 index = ApdbIndex(self.missing_index_path) 

105 with self.assertRaises(FileNotFoundError): 

106 index.get_entries() 

107 

108 

109if __name__ == "__main__": 

110 unittest.main()