Coverage for tests/test_server.py: 58%

37 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-27 09:43 +0000

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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28import os.path 

29import unittest 

30 

31try: 

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

33 from fastapi.testclient import TestClient 

34 from lsst.daf.butler.remote_butler import RemoteButler 

35 from lsst.daf.butler.remote_butler.server import Factory, app, factory_dependency 

36except ImportError: 

37 TestClient = None 

38 app = None 

39 

40from lsst.daf.butler import Butler 

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

42 

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

44 

45 

46def _make_remote_butler(http_client): 

47 return RemoteButler( 

48 config={ 

49 "remote_butler": { 

50 # This URL is ignored because we override the HTTP client, but 

51 # must be valid to satisfy the config validation 

52 "url": "https://test.example" 

53 } 

54 }, 

55 http_client=http_client, 

56 ) 

57 

58 

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

60class ButlerClientServerTestCase(unittest.TestCase): 

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

62 

63 @classmethod 

64 def setUpClass(cls): 

65 # First create a butler and populate it. 

66 cls.root = makeTestTempDir(TESTDIR) 

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

68 # Override the server's Butler initialization to point at our test repo 

69 server_butler = Butler.from_config(cls.root) 

70 

71 def create_factory_dependency(): 

72 return Factory(butler=server_butler) 

73 

74 app.dependency_overrides[factory_dependency] = create_factory_dependency 

75 

76 # Set up the RemoteButler that will connect to the server 

77 cls.client = TestClient(app) 

78 cls.butler = _make_remote_butler(cls.client) 

79 

80 @classmethod 

81 def tearDownClass(cls): 

82 del app.dependency_overrides[factory_dependency] 

83 removeTestTempDir(cls.root) 

84 

85 def test_simple(self): 

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_remote_butler(self): 

91 universe = self.butler.dimensions 

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

93 

94 

95if __name__ == "__main__": 

96 unittest.main()