Coverage for tests/test_server.py: 28%

81 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-05 02:03 -0800

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

23import unittest 

24import uuid 

25 

26try: 

27 # Failing to import any of these should disable the tests. 

28 import lsst.daf.butler.server 

29 from fastapi.testclient import TestClient 

30 from lsst.daf.butler.server import app 

31except ImportError: 

32 TestClient = None 

33 app = None 

34 

35from lsst.daf.butler import Butler, CollectionType, Config, DataCoordinate, DatasetRef 

36from lsst.daf.butler.tests import addDatasetType 

37from lsst.daf.butler.tests.utils import MetricTestRepo, makeTestTempDir, removeTestTempDir 

38 

39TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

40 

41 

42@unittest.skipIf(TestClient is None or app is None, "FastAPI not installed.") 

43class ButlerClientServerTestCase(unittest.TestCase): 

44 """Test for Butler client/server.""" 

45 

46 @classmethod 

47 def setUpClass(cls): 

48 # First create a butler and populate it. 

49 cls.root = makeTestTempDir(TESTDIR) 

50 cls.repo = MetricTestRepo(root=cls.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml")) 

51 

52 # Add a collection chain. 

53 cls.repo.butler.registry.registerCollection("chain", CollectionType.CHAINED) 

54 cls.repo.butler.registry.setCollectionChain("chain", ["ingest"]) 

55 

56 # Globally change where the server thinks its butler repository 

57 # is located. This will prevent any other server tests and is 

58 # not a long term fix. 

59 lsst.daf.butler.server.BUTLER_ROOT = cls.root 

60 cls.client = TestClient(app) 

61 

62 # Create a client butler. We need to modify the contents of the 

63 # server configuration to reflect the use of the test client. 

64 response = cls.client.get("/butler/butler.json") 

65 config = Config(response.json()) 

66 config["registry", "db"] = cls.client 

67 

68 # Since there is no client datastore we also need to specify 

69 # the datastore root. 

70 config["datastore", "root"] = cls.root 

71 cls.butler = Butler(config) 

72 

73 @classmethod 

74 def tearDownClass(cls): 

75 removeTestTempDir(cls.root) 

76 

77 def test_simple(self): 

78 response = self.client.get("/butler/") 

79 self.assertEqual(response.status_code, 200) 

80 self.assertIn("Butler Server", response.json()) 

81 

82 response = self.client.get("/butler/butler.json") 

83 self.assertEqual(response.status_code, 200) 

84 self.assertIn("registry", response.json()) 

85 

86 response = self.client.get("/butler/v1/universe") 

87 self.assertEqual(response.status_code, 200) 

88 self.assertIn("namespace", response.json()) 

89 

90 def test_registry(self): 

91 universe = self.butler.registry.dimensions 

92 self.assertEqual(universe.namespace, "daf_butler") 

93 

94 dataset_type = self.butler.registry.getDatasetType("test_metric_comp") 

95 self.assertEqual(dataset_type.name, "test_metric_comp") 

96 

97 dataset_types = list(self.butler.registry.queryDatasetTypes(...)) 

98 self.assertIn("test_metric_comp", [ds.name for ds in dataset_types]) 

99 dataset_types = list(self.butler.registry.queryDatasetTypes("test_*")) 

100 self.assertEqual(len(dataset_types), 1) 

101 

102 collections = self.butler.registry.queryCollections( 

103 ..., collectionTypes={CollectionType.RUN, CollectionType.TAGGED} 

104 ) 

105 self.assertEqual(len(collections), 2, collections) 

106 

107 collection_type = self.butler.registry.getCollectionType("ingest") 

108 self.assertEqual(collection_type.name, "TAGGED") 

109 

110 chain = self.butler.registry.getCollectionChain("chain") 

111 self.assertEqual([coll for coll in chain], ["ingest"]) 

112 

113 datasets = list(self.butler.registry.queryDatasets("test_metric_comp", collections=...)) 

114 self.assertEqual(len(datasets), 2) 

115 

116 ref = self.butler.registry.getDataset(datasets[0].id) 

117 self.assertEqual(ref, datasets[0]) 

118 

119 locations = self.butler.registry.getDatasetLocations(ref) 

120 self.assertEqual(locations[0], "FileDatastore@<butlerRoot>") 

121 

122 fake_ref = DatasetRef( 

123 dataset_type, 

124 dataId={"instrument": "DummyCamComp", "physical_filter": "d-r", "visit": 424}, 

125 id=uuid.uuid4(), 

126 run="missing", 

127 ) 

128 locations = self.butler.registry.getDatasetLocations(fake_ref) 

129 self.assertEqual(locations, []) 

130 

131 dataIds = list(self.butler.registry.queryDataIds("visit", dataId={"instrument": "DummyCamComp"})) 

132 self.assertEqual(len(dataIds), 2) 

133 

134 # Create a DataCoordinate to test the alternate path for specifying 

135 # a data ID. 

136 data_id = DataCoordinate.standardize( 

137 {"instrument": "DummyCamComp"}, universe=self.butler.registry.dimensions 

138 ) 

139 records = list(self.butler.registry.queryDimensionRecords("physical_filter", dataId=data_id)) 

140 self.assertEqual(len(records), 1) 

141 

142 def test_experimental(self): 

143 """Experimental interfaces.""" 

144 # Got URI testing we can not yet support disassembly so must 

145 # add a dataset with a different dataset type. 

146 datasetType = addDatasetType( 

147 self.repo.butler, "metric", {"instrument", "visit"}, "StructuredCompositeReadCompNoDisassembly" 

148 ) 

149 

150 self.repo.addDataset({"instrument": "DummyCamComp", "visit": 424}, datasetType=datasetType) 

151 self.butler.registry.refresh() 

152 

153 # Need a DatasetRef. 

154 datasets = list(self.butler.registry.queryDatasets("metric", collections=...)) 

155 

156 response = self.client.get(f"/butler/v1/uri/{datasets[0].id}") 

157 self.assertEqual(response.status_code, 200) 

158 self.assertIn("file://", response.json()) 

159 

160 

161if __name__ == "__main__": 161 ↛ 162line 161 didn't jump to line 162, because the condition on line 161 was never true

162 unittest.main()