Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit94d7572

Browse files
[#240] Using of node.psql with other host and port (#242)
* [FIX] Tests include testgres by right way through importWhen we do not have root __init__.py tests must to import testgres through"import testgres"not through"from <relative_path> import testgres"* [#240] Using of node.psql with other host and portThis patch adds the support of using other host and port in the following methods:- PostgresNode.psql (explicit new args: host and port)- PostgresNode.safe_psql (indirectly through **kwargs)It allows to run psql utility from one PostgreSQL instance to work with another one.If explicit host and port are not defined (are None), PostgresNode will use own ones.This patchcloses#240.
1 parent1a662f1 commit94d7572

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

‎testgres/node.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,8 @@ def psql(self,
13721372
dbname=None,
13731373
username=None,
13741374
input=None,
1375+
host:typing.Optional[str]=None,
1376+
port:typing.Optional[int]=None,
13751377
**variables):
13761378
"""
13771379
Execute a query using psql.
@@ -1382,6 +1384,8 @@ def psql(self,
13821384
dbname: database name to connect to.
13831385
username: database user name.
13841386
input: raw input to be passed.
1387+
host: an explicit host of server.
1388+
port: an explicit port of server.
13851389
**variables: vars to be set before execution.
13861390
13871391
Returns:
@@ -1393,13 +1397,19 @@ def psql(self,
13931397
>>> psql(query='select 3', ON_ERROR_STOP=1)
13941398
"""
13951399

1400+
asserthostisNoneortype(host)==str# noqa: E721
1401+
assertportisNoneortype(port)==int# noqa: E721
1402+
asserttype(variables)==dict# noqa: E721
1403+
13961404
returnself._psql(
13971405
ignore_errors=True,
13981406
query=query,
13991407
filename=filename,
14001408
dbname=dbname,
14011409
username=username,
14021410
input=input,
1411+
host=host,
1412+
port=port,
14031413
**variables
14041414
)
14051415

@@ -1411,7 +1421,11 @@ def _psql(
14111421
dbname=None,
14121422
username=None,
14131423
input=None,
1424+
host:typing.Optional[str]=None,
1425+
port:typing.Optional[int]=None,
14141426
**variables):
1427+
asserthostisNoneortype(host)==str# noqa: E721
1428+
assertportisNoneortype(port)==int# noqa: E721
14151429
asserttype(variables)==dict# noqa: E721
14161430

14171431
#
@@ -1424,10 +1438,21 @@ def _psql(
14241438
else:
14251439
raiseException("Input data must be None or bytes.")
14261440

1441+
ifhostisNone:
1442+
host=self.host
1443+
1444+
ifportisNone:
1445+
port=self.port
1446+
1447+
asserthostisnotNone
1448+
assertportisnotNone
1449+
asserttype(host)==str# noqa: E721
1450+
asserttype(port)==int# noqa: E721
1451+
14271452
psql_params= [
14281453
self._get_bin_path("psql"),
1429-
"-p",str(self.port),
1430-
"-h",self.host,
1454+
"-p",str(port),
1455+
"-h",host,
14311456
"-U",usernameorself.os_ops.username,
14321457
"-d",dbnameordefault_dbname(),
14331458
"-X",# no .psqlrc

‎tests/test_testgres_common.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,89 @@ def test_psql(self, node_svc: PostgresNodeService):
678678
r=node.safe_psql('select 1')# raises!
679679
logging.error("node.safe_psql returns [{}]".format(r))
680680

681+
deftest_psql__another_port(self,node_svc:PostgresNodeService):
682+
assertisinstance(node_svc,PostgresNodeService)
683+
with__class__.helper__get_node(node_svc).init()asnode1:
684+
with__class__.helper__get_node(node_svc).init()asnode2:
685+
node1.start()
686+
node2.start()
687+
assertnode1.port!=node2.port
688+
assertnode1.host==node2.host
689+
690+
node1.stop()
691+
692+
logging.info("test table in node2 is creating ...")
693+
node2.safe_psql(
694+
dbname="postgres",
695+
query="create table test (id integer);"
696+
)
697+
698+
logging.info("try to find test table through node1.psql ...")
699+
res=node1.psql(
700+
dbname="postgres",
701+
query="select count(*) from pg_class where relname='test'",
702+
host=node2.host,
703+
port=node2.port,
704+
)
705+
assert (__class__.helper__rm_carriage_returns(res)== (0,b'1\n',b''))
706+
707+
deftest_psql__another_bad_host(self,node_svc:PostgresNodeService):
708+
assertisinstance(node_svc,PostgresNodeService)
709+
with__class__.helper__get_node(node_svc).init()asnode:
710+
logging.info("try to execute node1.psql ...")
711+
res=node.psql(
712+
dbname="postgres",
713+
query="select count(*) from pg_class where relname='test'",
714+
host="DUMMY_HOST_NAME",
715+
port=node.port,
716+
)
717+
718+
res2=__class__.helper__rm_carriage_returns(res)
719+
720+
assertres2[0]!=0
721+
assertb"DUMMY_HOST_NAME"inres[2]
722+
723+
deftest_safe_psql__another_port(self,node_svc:PostgresNodeService):
724+
assertisinstance(node_svc,PostgresNodeService)
725+
with__class__.helper__get_node(node_svc).init()asnode1:
726+
with__class__.helper__get_node(node_svc).init()asnode2:
727+
node1.start()
728+
node2.start()
729+
assertnode1.port!=node2.port
730+
assertnode1.host==node2.host
731+
732+
node1.stop()
733+
734+
logging.info("test table in node2 is creating ...")
735+
node2.safe_psql(
736+
dbname="postgres",
737+
query="create table test (id integer);"
738+
)
739+
740+
logging.info("try to find test table through node1.psql ...")
741+
res=node1.safe_psql(
742+
dbname="postgres",
743+
query="select count(*) from pg_class where relname='test'",
744+
host=node2.host,
745+
port=node2.port,
746+
)
747+
assert (__class__.helper__rm_carriage_returns(res)==b'1\n')
748+
749+
deftest_safe_psql__another_bad_host(self,node_svc:PostgresNodeService):
750+
assertisinstance(node_svc,PostgresNodeService)
751+
with__class__.helper__get_node(node_svc).init()asnode:
752+
logging.info("try to execute node1.psql ...")
753+
754+
withpytest.raises(expected_exception=Exception)asx:
755+
node.safe_psql(
756+
dbname="postgres",
757+
query="select count(*) from pg_class where relname='test'",
758+
host="DUMMY_HOST_NAME",
759+
port=node.port,
760+
)
761+
762+
assert"DUMMY_HOST_NAME"instr(x.value)
763+
681764
deftest_safe_psql__expect_error(self,node_svc:PostgresNodeService):
682765
assertisinstance(node_svc,PostgresNodeService)
683766
with__class__.helper__get_node(node_svc).init().start()asnode:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp