Coverage for tests/test_connectionString.py: 40%

39 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-25 15:14 +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 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 

22"""Tests for ConnectionStringBuilder. 

23""" 

24 

25import glob 

26import os 

27import os.path 

28import unittest 

29 

30import lsst.daf.butler.registry.connectionString as ConnectionStringModule 

31from lsst.daf.butler.registry import RegistryConfig 

32from lsst.daf.butler.registry.connectionString import ConnectionStringFactory 

33from lsst.utils.db_auth import DbAuthError 

34 

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

36 

37 

38class ConnectionStringBuilderTestCase(unittest.TestCase): 

39 """Tests for ConnectionStringBuilder.""" 

40 

41 configDir = os.path.join(TESTDIR, "config", "dbAuth") 

42 configFiles = glob.glob(os.path.join(configDir, "registryConf*")) 

43 credentialsFile = os.path.join(configDir, "db-auth.yaml") 

44 

45 def setUp(self): 

46 self.resetDbAuthPathValue = ConnectionStringModule.DB_AUTH_PATH 

47 ConnectionStringModule.DB_AUTH_PATH = self.credentialsFile 

48 os.chmod(self.credentialsFile, 0o600) 

49 

50 def tearDown(self): 

51 ConnectionStringModule.DB_AUTH_PATH = self.resetDbAuthPathValue 

52 

53 def testBuilder(self): 

54 """Tests ConnectionStringFactory returns correct connection strings.""" 

55 regConfigs = [RegistryConfig(os.path.join(self.configDir, name)) for name in self.configFiles] 

56 

57 conStrFactory = ConnectionStringFactory() 

58 for regConf, fileName in zip(regConfigs, self.configFiles, strict=True): 

59 conStr = conStrFactory.fromConfig(regConf) 

60 with self.subTest(confFile=fileName): 

61 self.assertEqual( 

62 conStr.render_as_string(hide_password=False), 

63 regConf["expected"], 

64 "test connection string built from config", 

65 ) 

66 

67 def testRelVsAbsPath(self): 

68 """Tests that relative and absolute paths are preserved.""" 

69 regConf = RegistryConfig(os.path.join(self.configDir, "registryConf1.yaml")) 

70 regConf["db"] = "sqlite:///relative/path/conf1.sqlite3" 

71 conStrFactory = ConnectionStringFactory() 

72 conStr = conStrFactory.fromConfig(regConf) 

73 self.assertEqual(str(conStr), "sqlite:///relative/path/conf1.sqlite3") 

74 

75 def testRaises(self): 

76 """Test that DbAuthError propagates through the class.""" 

77 ConnectionStringModule.DB_AUTH_PATH = os.path.join(self.configDir, "badDbAuth2.yaml") 

78 regConf = RegistryConfig(os.path.join(self.configDir, "registryConf2.yaml")) 

79 conStrFactory = ConnectionStringFactory() 

80 with self.assertRaises(DbAuthError): 

81 conStrFactory.fromConfig(regConf) 

82 

83 

84if __name__ == "__main__": 

85 unittest.main()