Coverage for tests/test_connectionString.py: 40%

40 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-25 02:36 -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 

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 DbAuthError, RegistryConfig 

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

33 

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

35 

36 

37class ConnectionStringBuilderTestCase(unittest.TestCase): 

38 """Tests for ConnectionStringBuilder.""" 

39 

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

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

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

43 

44 def setUp(self): 

45 self.resetDbAuthPathValue = ConnectionStringModule.DB_AUTH_PATH 

46 ConnectionStringModule.DB_AUTH_PATH = self.credentialsFile 

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

48 

49 def tearDown(self): 

50 ConnectionStringModule.DB_AUTH_PATH = self.resetDbAuthPathValue 

51 

52 def testBuilder(self): 

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

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

55 

56 conStrFactory = ConnectionStringFactory() 

57 for regConf, fileName in zip(regConfigs, self.configFiles): 

58 conStr = conStrFactory.fromConfig(regConf) 

59 with self.subTest(confFile=fileName): 

60 self.assertEqual(str(conStr), regConf["expected"], "test connection string built from config") 

61 

62 def testRelVsAbsPath(self): 

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

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

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

66 conStrFactory = ConnectionStringFactory() 

67 conStr = conStrFactory.fromConfig(regConf) 

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

69 

70 def testRaises(self): 

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

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

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

74 conStrFactory = ConnectionStringFactory() 

75 with self.assertRaises(DbAuthError): 

76 conStrFactory.fromConfig(regConf) 

77 

78 

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

80 unittest.main()