Coverage for tests/test_connectionString.py: 39%
38 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-18 09:13 +0000
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-18 09:13 +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/>.
22"""Tests for ConnectionStringBuilder.
23"""
25import glob
26import os
27import os.path
28import unittest
30import lsst.daf.butler.registry.connectionString as ConnectionStringModule
31from lsst.daf.butler.registry import DbAuthError, RegistryConfig
32from lsst.daf.butler.registry.connectionString import ConnectionStringFactory
34TESTDIR = os.path.abspath(os.path.dirname(__file__))
37class ConnectionStringBuilderTestCase(unittest.TestCase):
38 """Tests for ConnectionStringBuilder."""
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")
44 def setUp(self):
45 self.resetDbAuthPathValue = ConnectionStringModule.DB_AUTH_PATH
46 ConnectionStringModule.DB_AUTH_PATH = self.credentialsFile
47 os.chmod(self.credentialsFile, 0o600)
49 def tearDown(self):
50 ConnectionStringModule.DB_AUTH_PATH = self.resetDbAuthPathValue
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]
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(
61 conStr.render_as_string(hide_password=False),
62 regConf["expected"],
63 "test connection string built from config",
64 )
66 def testRelVsAbsPath(self):
67 """Tests that relative and absolute paths are preserved."""
68 regConf = RegistryConfig(os.path.join(self.configDir, "registryConf1.yaml"))
69 regConf["db"] = "sqlite:///relative/path/conf1.sqlite3"
70 conStrFactory = ConnectionStringFactory()
71 conStr = conStrFactory.fromConfig(regConf)
72 self.assertEqual(str(conStr), "sqlite:///relative/path/conf1.sqlite3")
74 def testRaises(self):
75 """Test that DbAuthError propagates through the class."""
76 ConnectionStringModule.DB_AUTH_PATH = os.path.join(self.configDir, "badDbAuth2.yaml")
77 regConf = RegistryConfig(os.path.join(self.configDir, "registryConf2.yaml"))
78 conStrFactory = ConnectionStringFactory()
79 with self.assertRaises(DbAuthError):
80 conStrFactory.fromConfig(regConf)
83if __name__ == "__main__":
84 unittest.main()