Coverage for tests/test_connectionString.py: 46%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

40 statements  

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 unittest 

26import os 

27import os.path 

28import glob 

29 

30from lsst.daf.butler.registry import RegistryConfig, DbAuthError 

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

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 configDir = os.path.join(TESTDIR, "config", "dbAuth") 

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

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

42 

43 def setUp(self): 

44 self.resetDbAuthPathValue = ConnectionStringModule.DB_AUTH_PATH 

45 ConnectionStringModule.DB_AUTH_PATH = self.credentialsFile 

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

47 

48 def tearDown(self): 

49 ConnectionStringModule.DB_AUTH_PATH = self.resetDbAuthPathValue 

50 

51 def testBuilder(self): 

52 """Tests ConnectionStringFactory returns correct connection strings. 

53 """ 

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'], 

61 "test connection string built from config") 

62 

63 def testRelVsAbsPath(self): 

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

65 regConf = RegistryConfig(os.path.join(self.configDir, 'registryConf1.yaml')) 

66 regConf['db'] = 'sqlite:///relative/path/conf1.sqlite3' 

67 conStrFactory = ConnectionStringFactory() 

68 conStr = conStrFactory.fromConfig(regConf) 

69 self.assertEqual(str(conStr), 'sqlite:///relative/path/conf1.sqlite3') 

70 

71 def testRaises(self): 

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

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

74 regConf = RegistryConfig(os.path.join(self.configDir, 'registryConf2.yaml')) 

75 conStrFactory = ConnectionStringFactory() 

76 with self.assertRaises(DbAuthError): 

77 conStrFactory.fromConfig(regConf) 

78 

79 

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

81 unittest.main()