diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..05702e9 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,41 @@ +from ..testgres import TestgresConfig +from ..testgres import configure_testgres +from ..testgres import scoped_config +from ..testgres import pop_config + +from .. import testgres + +import pytest + + +class TestConfig: + def test_config_stack(self): + # no such option + with pytest.raises(expected_exception=TypeError): + configure_testgres(dummy=True) + + # we have only 1 config in stack + with pytest.raises(expected_exception=IndexError): + pop_config() + + d0 = TestgresConfig.cached_initdb_dir + d1 = 'dummy_abc' + d2 = 'dummy_def' + + with scoped_config(cached_initdb_dir=d1) as c1: + assert (c1.cached_initdb_dir == d1) + + with scoped_config(cached_initdb_dir=d2) as c2: + stack_size = len(testgres.config.config_stack) + + # try to break a stack + with pytest.raises(expected_exception=TypeError): + with scoped_config(dummy=True): + pass + + assert (c2.cached_initdb_dir == d2) + assert (len(testgres.config.config_stack) == stack_size) + + assert (c1.cached_initdb_dir == d1) + + assert (TestgresConfig.cached_initdb_dir == d0) diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index b286a1c..c384dfb 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -6,7 +6,6 @@ from ..testgres.node import PgVer from ..testgres.node import PostgresNode from ..testgres.utils import get_pg_version2 -from ..testgres.utils import get_pg_config2 from ..testgres.utils import file_tail from ..testgres.utils import get_bin_path2 from ..testgres import ProcessType @@ -106,6 +105,32 @@ def test_version_management(self, node_svc: PostgresNodeService): assert (isinstance(node.version, PgVer)) assert (node.version == PgVer(version)) + def test_node_repr(self, node_svc: PostgresNodeService): + with __class__.helper__get_node(node_svc).init() as node: + pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" + assert re.match(pattern, str(node)) is not None + + def test_custom_init(self, node_svc: PostgresNodeService): + assert isinstance(node_svc, PostgresNodeService) + + with __class__.helper__get_node(node_svc) as node: + # enable page checksums + node.init(initdb_params=['-k']).start() + + with __class__.helper__get_node(node_svc) as node: + node.init( + allow_streaming=True, + initdb_params=['--auth-local=reject', '--auth-host=reject']) + + hba_file = os.path.join(node.data_dir, 'pg_hba.conf') + lines = node.os_ops.readlines(hba_file) + + # check number of lines + assert (len(lines) >= 6) + + # there should be no trust entries at all + assert not (any('trust' in s for s in lines)) + def test_double_init(self, node_svc: PostgresNodeService): assert isinstance(node_svc, PostgresNodeService) @@ -1075,33 +1100,6 @@ def test_dump(self, node_svc: PostgresNodeService): res = node3.execute(query_select) assert (res == [(1, ), (2, )]) - def test_get_pg_config2(self, node_svc: PostgresNodeService): - assert isinstance(node_svc, PostgresNodeService) - - # check same instances - a = get_pg_config2(node_svc.os_ops, None) - b = get_pg_config2(node_svc.os_ops, None) - assert (id(a) == id(b)) - - # save right before config change - c1 = get_pg_config2(node_svc.os_ops, None) - - # modify setting for this scope - with scoped_config(cache_pg_config=False) as config: - # sanity check for value - assert not (config.cache_pg_config) - - # save right after config change - c2 = get_pg_config2(node_svc.os_ops, None) - - # check different instances after config change - assert (id(c1) != id(c2)) - - # check different instances - a = get_pg_config2(node_svc.os_ops, None) - b = get_pg_config2(node_svc.os_ops, None) - assert (id(a) != id(b)) - def test_pgbench(self, node_svc: PostgresNodeService): assert isinstance(node_svc, PostgresNodeService) diff --git a/tests/test_testgres_local.py b/tests/test_testgres_local.py index bef80d0..9dbd455 100644 --- a/tests/test_testgres_local.py +++ b/tests/test_testgres_local.py @@ -9,28 +9,18 @@ from .. import testgres -from ..testgres import \ - StartNodeException, \ - ExecUtilException, \ - NodeApp - -from ..testgres import \ - TestgresConfig, \ - configure_testgres, \ - scoped_config, \ - pop_config - -from ..testgres import \ - get_new_node - -from ..testgres import \ - get_bin_path, \ - get_pg_config, \ - get_pg_version +from ..testgres import StartNodeException +from ..testgres import ExecUtilException +from ..testgres import NodeApp +from ..testgres import scoped_config +from ..testgres import get_new_node +from ..testgres import get_bin_path +from ..testgres import get_pg_config +from ..testgres import get_pg_version # NOTE: those are ugly imports -from ..testgres import bound_ports -from ..testgres.utils import PgVer, parse_pg_version +from ..testgres.utils import bound_ports +from ..testgres.utils import PgVer from ..testgres.node import ProcessProxy @@ -75,31 +65,6 @@ def rm_carriage_returns(out): class TestTestgresLocal: - def test_node_repr(self): - with get_new_node() as node: - pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" - assert re.match(pattern, str(node)) is not None - - def test_custom_init(self): - with get_new_node() as node: - # enable page checksums - node.init(initdb_params=['-k']).start() - - with get_new_node() as node: - node.init( - allow_streaming=True, - initdb_params=['--auth-local=reject', '--auth-host=reject']) - - hba_file = os.path.join(node.data_dir, 'pg_hba.conf') - with open(hba_file, 'r') as conf: - lines = conf.readlines() - - # check number of lines - assert (len(lines) >= 6) - - # there should be no trust entries at all - assert not (any('trust' in s for s in lines)) - def test_pg_config(self): # check same instances a = get_pg_config() @@ -125,37 +90,6 @@ def test_pg_config(self): b = get_pg_config() assert (id(a) != id(b)) - def test_config_stack(self): - # no such option - with pytest.raises(expected_exception=TypeError): - configure_testgres(dummy=True) - - # we have only 1 config in stack - with pytest.raises(expected_exception=IndexError): - pop_config() - - d0 = TestgresConfig.cached_initdb_dir - d1 = 'dummy_abc' - d2 = 'dummy_def' - - with scoped_config(cached_initdb_dir=d1) as c1: - assert (c1.cached_initdb_dir == d1) - - with scoped_config(cached_initdb_dir=d2) as c2: - stack_size = len(testgres.config.config_stack) - - # try to break a stack - with pytest.raises(expected_exception=TypeError): - with scoped_config(dummy=True): - pass - - assert (c2.cached_initdb_dir == d2) - assert (len(testgres.config.config_stack) == stack_size) - - assert (c1.cached_initdb_dir == d1) - - assert (TestgresConfig.cached_initdb_dir == d0) - def test_ports_management(self): assert bound_ports is not None assert type(bound_ports) == set # noqa: E721 @@ -234,16 +168,6 @@ def test_upgrade_node(self): node_new.start() assert (b'Upgrade Complete' in res) - def test_parse_pg_version(self): - # Linux Mint - assert parse_pg_version("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)") == "15.5" - # Linux Ubuntu - assert parse_pg_version("postgres (PostgreSQL) 12.17") == "12.17" - # Windows - assert parse_pg_version("postgres (PostgreSQL) 11.4") == "11.4" - # Macos - assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9" - class tagPortManagerProxy: sm_prev_testgres_reserve_port = None sm_prev_testgres_release_port = None diff --git a/tests/test_testgres_remote.py b/tests/test_testgres_remote.py index ef4bd0c..e38099b 100755 --- a/tests/test_testgres_remote.py +++ b/tests/test_testgres_remote.py @@ -1,6 +1,5 @@ # coding: utf-8 import os -import re import pytest import logging @@ -10,19 +9,14 @@ from .. import testgres -from ..testgres.exceptions import \ - InitNodeException, \ - ExecUtilException +from ..testgres.exceptions import InitNodeException +from ..testgres.exceptions import ExecUtilException -from ..testgres.config import \ - TestgresConfig, \ - configure_testgres, \ - scoped_config, \ - pop_config, testgres_config +from ..testgres.config import scoped_config +from ..testgres.config import testgres_config -from ..testgres import \ - get_bin_path, \ - get_pg_config +from ..testgres import get_bin_path +from ..testgres import get_pg_config # NOTE: those are ugly imports @@ -58,30 +52,6 @@ def implicit_fixture(self): testgres_config.set_os_ops(os_ops=prev_ops) assert testgres_config.os_ops is prev_ops - def test_node_repr(self): - with __class__.helper__get_node() as node: - pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)" - assert re.match(pattern, str(node)) is not None - - def test_custom_init(self): - with __class__.helper__get_node() as node: - # enable page checksums - node.init(initdb_params=['-k']).start() - - with __class__.helper__get_node() as node: - node.init( - allow_streaming=True, - initdb_params=['--auth-local=reject', '--auth-host=reject']) - - hba_file = os.path.join(node.data_dir, 'pg_hba.conf') - lines = node.os_ops.readlines(hba_file) - - # check number of lines - assert (len(lines) >= 6) - - # there should be no trust entries at all - assert not (any('trust' in s for s in lines)) - def test_init__LANG_ะก(self): # PBCKP-1744 prev_LANG = os.environ.get("LANG") @@ -193,37 +163,6 @@ def test_pg_config(self): b = get_pg_config() assert (id(a) != id(b)) - def test_config_stack(self): - # no such option - with pytest.raises(expected_exception=TypeError): - configure_testgres(dummy=True) - - # we have only 1 config in stack - with pytest.raises(expected_exception=IndexError): - pop_config() - - d0 = TestgresConfig.cached_initdb_dir - d1 = 'dummy_abc' - d2 = 'dummy_def' - - with scoped_config(cached_initdb_dir=d1) as c1: - assert (c1.cached_initdb_dir == d1) - - with scoped_config(cached_initdb_dir=d2) as c2: - stack_size = len(testgres.config.config_stack) - - # try to break a stack - with pytest.raises(expected_exception=TypeError): - with scoped_config(dummy=True): - pass - - assert (c2.cached_initdb_dir == d2) - assert (len(testgres.config.config_stack) == stack_size) - - assert (c1.cached_initdb_dir == d1) - - assert (TestgresConfig.cached_initdb_dir == d0) - @staticmethod def helper__get_node(name=None): svc = PostgresNodeServices.sm_remote diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..d4a4c9a --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,62 @@ +from .helpers.global_data import OsOpsDescr +from .helpers.global_data import OsOpsDescrs +from .helpers.global_data import OsOperations + +from ..testgres.utils import parse_pg_version +from ..testgres.utils import get_pg_config2 +from ..testgres import scoped_config + +import pytest + + +class TestUtils: + sm_os_ops_descrs: list[OsOpsDescr] = [ + OsOpsDescrs.sm_local_os_ops_descr, + OsOpsDescrs.sm_remote_os_ops_descr + ] + + @pytest.fixture( + params=[descr.os_ops for descr in sm_os_ops_descrs], + ids=[descr.sign for descr in sm_os_ops_descrs] + ) + def os_ops(self, request: pytest.FixtureRequest) -> OsOperations: + assert isinstance(request, pytest.FixtureRequest) + assert isinstance(request.param, OsOperations) + return request.param + + def test_parse_pg_version(self): + # Linux Mint + assert parse_pg_version("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)") == "15.5" + # Linux Ubuntu + assert parse_pg_version("postgres (PostgreSQL) 12.17") == "12.17" + # Windows + assert parse_pg_version("postgres (PostgreSQL) 11.4") == "11.4" + # Macos + assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9" + + def test_get_pg_config2(self, os_ops: OsOperations): + assert isinstance(os_ops, OsOperations) + + # check same instances + a = get_pg_config2(os_ops, None) + b = get_pg_config2(os_ops, None) + assert (id(a) == id(b)) + + # save right before config change + c1 = get_pg_config2(os_ops, None) + + # modify setting for this scope + with scoped_config(cache_pg_config=False) as config: + # sanity check for value + assert not (config.cache_pg_config) + + # save right after config change + c2 = get_pg_config2(os_ops, None) + + # check different instances after config change + assert (id(c1) != id(c2)) + + # check different instances + a = get_pg_config2(os_ops, None) + b = get_pg_config2(os_ops, None) + assert (id(a) != id(b))
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: