Skip to content

Test refactoring #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 26 additions & 28 deletions tests/test_testgres_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
96 changes: 10 additions & 86 deletions tests/test_testgres_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
73 changes: 6 additions & 67 deletions tests/test_testgres_remote.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# coding: utf-8
import os
import re

import pytest
import logging
Expand All @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
Loading