Coverage for tests/test_server.py: 27%
79 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 02:05 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 02:05 -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 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/>.
22import os.path
23import unittest
24import uuid
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
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
39TESTDIR = os.path.abspath(os.path.dirname(__file__))
42@unittest.skipIf(TestClient is None or app is None, "FastAPI not installed.")
43class ButlerClientServerTestCase(unittest.TestCase):
44 """Test for Butler client/server."""
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"))
52 # Add a collection chain.
53 cls.repo.butler.registry.registerCollection("chain", CollectionType.CHAINED)
54 cls.repo.butler.registry.setCollectionChain("chain", ["ingest"])
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)
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
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)
73 @classmethod
74 def tearDownClass(cls):
75 removeTestTempDir(cls.root)
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())
82 response = self.client.get("/butler/butler.json")
83 self.assertEqual(response.status_code, 200)
84 self.assertIn("registry", response.json())
86 response = self.client.get("/butler/v1/universe")
87 self.assertEqual(response.status_code, 200)
88 self.assertIn("namespace", response.json())
90 def test_registry(self):
91 universe = self.butler.registry.dimensions
92 self.assertEqual(universe.namespace, "daf_butler")
94 dataset_type = self.butler.registry.getDatasetType("test_metric_comp")
95 self.assertEqual(dataset_type.name, "test_metric_comp")
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)
102 collections = self.butler.registry.queryCollections(
103 ..., collectionTypes={CollectionType.RUN, CollectionType.TAGGED}
104 )
105 self.assertEqual(len(collections), 2, collections)
107 collection_type = self.butler.registry.getCollectionType("ingest")
108 self.assertEqual(collection_type.name, "TAGGED")
110 chain = self.butler.registry.getCollectionChain("chain")
111 self.assertEqual([coll for coll in chain], ["ingest"])
113 datasets = list(self.butler.registry.queryDatasets("test_metric_comp", collections=...))
114 self.assertEqual(len(datasets), 2)
116 ref = self.butler.registry.getDataset(datasets[0].id)
117 self.assertEqual(ref, datasets[0])
119 locations = self.butler.registry.getDatasetLocations(ref)
120 self.assertEqual(locations[0], "FileDatastore@<butlerRoot>")
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, [])
131 dataIds = list(self.butler.registry.queryDataIds("visit", dataId={"instrument": "DummyCamComp"}))
132 self.assertEqual(len(dataIds), 2)
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)
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 )
150 self.repo.addDataset({"instrument": "DummyCamComp", "visit": 424}, datasetType=datasetType)
151 self.butler.registry.refresh()
153 # Need a DatasetRef.
154 datasets = list(self.butler.registry.queryDatasets("metric", collections=...))
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())
161if __name__ == "__main__":
162 unittest.main()