From 52ccf4a6c53cb9438611e468ffd7d60299b99cf6 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Fri, 10 Jul 2026 14:20:10 +0300 Subject: [PATCH 1/2] fix: default_username does not respect os_ops default_username: - new signature is: def default_username(os_ops: typing.Optional[OsOperations] = None) -> str: new: default_username2 is added: - signature is: def default_username2(os_ops: OsOperations) -> str: NodeConnection::__init__: - uses default_username2(node.os_ops) Publication::__init__: - uses default_username2(node.os_ops) Subscription::__init__: - uses default_username2(node.os_ops) Amen. --- src/connection.py | 4 ++-- src/defaults.py | 26 ++++++++++++++++++++++++-- src/pubsub.py | 6 +++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/connection.py b/src/connection.py index b8dc49a9..b16edb23 100644 --- a/src/connection.py +++ b/src/connection.py @@ -14,7 +14,7 @@ from .defaults import \ default_dbname, \ - default_username + default_username2 from .exceptions import QueryException @@ -38,7 +38,7 @@ def __init__(self, # Set default arguments dbname = dbname or default_dbname() - username = username or default_username() + username = username or default_username2(node.os_ops) self._node = node diff --git a/src/defaults.py b/src/defaults.py index d77361d7..0f79feef 100644 --- a/src/defaults.py +++ b/src/defaults.py @@ -1,6 +1,9 @@ import datetime import struct import uuid +import typing + +from testgres.operations.os_ops import OsOperations from .config import testgres_config as tconf @@ -13,11 +16,30 @@ def default_dbname(): return 'postgres' -def default_username(): +def default_username(os_ops: typing.Optional[OsOperations] = None) -> str: """ Return default username (current user). """ - return tconf.os_ops.get_user() + assert os_ops is None or isinstance(os_ops, OsOperations) + + if os_ops is None: + os_ops = tconf.os_ops + + assert isinstance(os_ops, OsOperations) + result = default_username2(os_ops) + assert type(result) is str + return result + + +def default_username2(os_ops: OsOperations) -> str: + """ + Return default username (current user). + """ + assert isinstance(os_ops, OsOperations) + + result = os_ops.get_user() + assert type(result) is str + return result def generate_app_name(): diff --git a/src/pubsub.py b/src/pubsub.py index 98548f8e..cbc55c9b 100644 --- a/src/pubsub.py +++ b/src/pubsub.py @@ -45,7 +45,7 @@ from six import raise_from from .consts import LOGICAL_REPL_MAX_CATCHUP_ATTEMPTS -from .defaults import default_dbname, default_username +from .defaults import default_dbname, default_username2 from .exceptions import CatchUpException from .utils import options_string @@ -72,7 +72,7 @@ def __init__(self, name, node, tables=None, dbname=None, username=None): self.name = name self.node = node self.dbname = dbname or default_dbname() - self.username = username or default_username() + self.username = username or default_username2(node.os_ops) # create publication in database t = "table " + ", ".join(tables) if tables else "all tables" @@ -167,7 +167,7 @@ def __init__(self, self.node = node self.pub = publication self.dbname = dbname or default_dbname() - self.username = username or default_username() + self.username = username or default_username2(node.os_ops) # connection info conninfo = { From 1a07dfed655623908896c00fa5658573b24d77b8 Mon Sep 17 00:00:00 2001 From: "d.kovalenko" Date: Fri, 10 Jul 2026 14:46:06 +0300 Subject: [PATCH 2/2] tests for default_username and default_username2 are added --- tests/test_testgres_common.py | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index 88d023b0..f0521fd9 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -1,5 +1,7 @@ from __future__ import annotations +from .helpers.global_data import OsOpsDescrs +from .helpers.global_data import OsOpsDescr from .helpers.global_data import PostgresNodeService from .helpers.global_data import PostgresNodeServices from .helpers.global_data import OsOperations @@ -16,6 +18,9 @@ from src.utils import file_tail from src.utils import get_bin_path2 from src.utils import execute_utility2 +from src.defaults import default_username +from src.defaults import default_username2 +from src.config import testgres_config as tconf from src import ProcessType from src import NodeStatus from src import IsolationLevel @@ -71,6 +76,25 @@ def removing(os_ops: OsOperations, f): class TestTestgresCommon: + sm_os_ops_descrs: typing.List[OsOpsDescr] = [ + OsOpsDescrs.sm_local_os_ops_descr, + OsOpsDescrs.sm_remote_os_ops_descr + ] + + @pytest.fixture( + params=[ + pytest.param( + descr, + id=descr.sign, + ) + for descr in sm_os_ops_descrs + ], + ) + def os_ops_descr(self, request: pytest.FixtureRequest) -> OsOpsDescr: + assert isinstance(request, pytest.FixtureRequest) + assert isinstance(request.param, OsOpsDescr) + return request.param + sm_node_svcs: typing.List[PostgresNodeService] = [ PostgresNodeServices.sm_local, PostgresNodeServices.sm_local2, @@ -133,6 +157,37 @@ def test_version_management(self, node_svc: PostgresNodeService): assert (isinstance(node.version, PgVer)) assert (node.version == PgVer(version)) + def test_default_username( + self, + os_ops_descr: OsOpsDescr, + ): + assert type(os_ops_descr) is OsOpsDescr + assert isinstance(os_ops_descr.os_ops, OsOperations) + + os_ops = os_ops_descr.os_ops + assert isinstance(os_ops, OsOperations) + + assert default_username(os_ops) == os_ops.get_user() + assert default_username(os_ops) == os_ops.username + + assert default_username() == tconf.os_ops.username + assert default_username() == tconf.os_ops.get_user() + return + + def test_default_username2( + self, + os_ops_descr: OsOpsDescr, + ): + assert type(os_ops_descr) is OsOpsDescr + assert isinstance(os_ops_descr.os_ops, OsOperations) + + os_ops = os_ops_descr.os_ops + assert isinstance(os_ops, OsOperations) + + assert default_username2(os_ops) == os_ops.get_user() + assert default_username2(os_ops) == os_ops.username + return + def test_node_constructor__default(self): node = PostgresNode() assert node._os_ops is not None