From 8bec8a167187dc78c40f3dc03f72203ad7c76a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Thu, 8 Feb 2024 12:39:37 +0500 Subject: [PATCH] postgresql: better --host option support Minor changes: * PEP 3135 "super()" function --- pifpaf/drivers/postgresql.py | 9 ++++++--- pifpaf/tests/test_drivers.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pifpaf/drivers/postgresql.py b/pifpaf/drivers/postgresql.py index e817e9d..e0f6e1a 100644 --- a/pifpaf/drivers/postgresql.py +++ b/pifpaf/drivers/postgresql.py @@ -40,15 +40,15 @@ def get_options(cls): def __init__(self, port=DEFAULT_PORT, host=DEFAULT_HOST, sync=DEFAULT_SYNC, **kwargs): """Create a new PostgreSQL instance.""" - super(PostgreSQLDriver, self).__init__(**kwargs) + super().__init__(**kwargs) self.port = port self.host = host self.sync = sync def _setUp(self): - super(PostgreSQLDriver, self)._setUp() + super()._setUp() self.putenv("PGPORT", str(self.port), True) - self.putenv("PGHOST", self.tempdir, True) + self.putenv("PGHOST", self.host or self.tempdir, True) self.putenv("PGDATA", self.tempdir, True) self.putenv("PGDATABASE", "postgres", True) _, pgbindir = self._exec(["pg_config", "--bindir"], stdout=True) @@ -65,6 +65,9 @@ def _setUp(self): % (self.tempdir, self.port, self.host), "start"], allow_debug=False) self.addCleanup(self._exec, [pgctl, "-w", "stop"]) + self.url = "postgresql://localhost/postgres?host=%s&port=%d" % ( self.tempdir, self.port) + if self.host: + self.url = "postgresql://%s:%d/postgres" % (self.host, self.port) self.putenv("URL", self.url) diff --git a/pifpaf/tests/test_drivers.py b/pifpaf/tests/test_drivers.py index 764c0a7..ac6b794 100644 --- a/pifpaf/tests/test_drivers.py +++ b/pifpaf/tests/test_drivers.py @@ -252,6 +252,17 @@ def test_postgresql(self): os.getenv("PIFPAF_URL")) self._run("psql template1 -c 'CREATE TABLE FOOBAR();'") + @testtools.skipUnless(spawn.find_executable("pg_config"), + "pg_config not found") + def test_postgresql_host(self): + host = "127.0.98.25" + port = 9825 + self.useFixture(postgresql.PostgreSQLDriver(host=host, port=port)) + self.assertEqual( + "postgresql://%s:%d/postgres" % (host, port), + os.getenv("PIFPAF_URL")) + self._run("psql template1 -c 'CREATE TABLE FOOBAR();'") + @testtools.skipUnless(spawn.find_executable("pg_config"), "pg_config not found") def test_postgresql_async(self):