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

Commit3175125

Browse files
authored
Merge pull request#33 from Valeria1235/simplified_enums
Simplified enums
2 parentsbe1e3c5 +bc28551 commit3175125

File tree

7 files changed

+70
-55
lines changed

7 files changed

+70
-55
lines changed

‎testgres/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
pop_config
1010

1111
from .connectionimport \
12-
IsolationLevel, \
1312
NodeConnection, \
1413
InternalError, \
1514
ProgrammingError
1615

1716
from .exceptionsimport*
18-
from .nodeimportNodeStatus,PostgresNode
17+
from .enumsimport*
18+
from .nodeimportPostgresNode
1919

2020
from .utilsimport \
2121
reserve_port, \

‎testgres/backup.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
fromsiximportraise_from
77
fromtempfileimportmkdtemp
88

9+
from .enumsimportXLogMethod
10+
911
from .constsimport \
1012
DATA_DIR, \
1113
TMP_NODE, \
@@ -50,7 +52,13 @@ def __init__(self,
5052
ifnotnode.status():
5153
raiseBackupException('Node must be running')
5254

53-
# yapf: disable
55+
# Check arguments
56+
ifnotisinstance(xlog_method,XLogMethod):
57+
try:
58+
xlog_method=XLogMethod(xlog_method)
59+
exceptValueError:
60+
raiseBackupException('Invalid xlog_method "{}"'.format(xlog_method))
61+
5462
# Set default arguments
5563
username=usernameordefault_username()
5664
base_dir=base_dirormkdtemp(prefix=TMP_BACKUP)
@@ -72,7 +80,7 @@ def __init__(self,
7280
"-h",node.host,
7381
"-U",username,
7482
"-D",data_dir,
75-
"-X",xlog_method
83+
"-X",xlog_method.value
7684
]
7785
execute_utility(_params,self.log_file)
7886

‎testgres/connection.py

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
exceptImportError:
1010
raiseImportError("You must have psycopg2 or pg8000 modules installed")
1111

12+
from .enumsimportIsolationLevel
13+
1214
from .defaultsimport \
1315
default_dbname, \
1416
default_username
1517

16-
fromenumimportEnum
17-
1818
from .exceptionsimportQueryException
1919

2020

@@ -23,14 +23,6 @@
2323
ProgrammingError=pglib.ProgrammingError
2424

2525

26-
classIsolationLevel(Enum):
27-
"""
28-
Transaction isolation level for NodeConnection
29-
"""
30-
31-
ReadUncommitted,ReadCommitted,RepeatableRead,Serializable=range(4)
32-
33-
3426
classNodeConnection(object):
3527
"""
3628
Transaction wrapper returned by Node
@@ -72,39 +64,21 @@ def __exit__(self, type, value, traceback):
7264
self.close()
7365

7466
defbegin(self,isolation_level=IsolationLevel.ReadCommitted):
75-
# yapf: disable
76-
levels= [
77-
'read uncommitted',
78-
'read committed',
79-
'repeatable read',
80-
'serializable'
81-
]
82-
83-
# Check if level is an IsolationLevel
84-
if (isinstance(isolation_level,IsolationLevel)):
85-
86-
# Get index of isolation level
87-
level_idx=isolation_level.value
88-
assertlevel_idxinrange(4)
89-
90-
# Replace isolation level with its name
91-
isolation_level=levels[level_idx]
92-
93-
else:
67+
# Check if level isn't an IsolationLevel
68+
ifnotisinstance(isolation_level,IsolationLevel):
9469
# Get name of isolation level
9570
level_str=str(isolation_level).lower()
9671

9772
# Validate level string
98-
iflevel_strnotinlevels:
73+
try:
74+
isolation_level=IsolationLevel(level_str)
75+
exceptValueError:
9976
error='Invalid isolation level "{}"'
10077
raiseQueryException(error.format(level_str))
10178

102-
# Replace isolation level with its name
103-
isolation_level=level_str
104-
10579
# Set isolation level
10680
cmd='SET TRANSACTION ISOLATION LEVEL {}'
107-
self.cursor.execute(cmd.format(isolation_level))
81+
self.cursor.execute(cmd.format(isolation_level.value))
10882

10983
returnself
11084

‎testgres/consts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# coding: utf-8
22

3+
from .enumsimportXLogMethod
4+
35
# names for dirs in base_dir
46
DATA_DIR="data"
57
LOGS_DIR="logs"
@@ -25,4 +27,4 @@
2527
BACKUP_LOG_FILE="backup.log"
2628

2729
# default argument value
28-
DEFAULT_XLOG_METHOD="fetch"
30+
DEFAULT_XLOG_METHOD=XLogMethod.fetch

‎testgres/enums.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
fromenumimportEnum,IntEnum
2+
3+
4+
classXLogMethod(Enum):
5+
"""
6+
Available WAL methods for NodeBackup
7+
"""
8+
9+
none='none'
10+
fetch='fetch'
11+
stream='stream'
12+
13+
14+
classIsolationLevel(Enum):
15+
"""
16+
Transaction isolation level for NodeConnection
17+
"""
18+
19+
ReadUncommitted='read uncommitted'
20+
ReadCommitted='read committed'
21+
RepeatableRead='repeatable read'
22+
Serializable='serializable'
23+
24+
25+
classNodeStatus(IntEnum):
26+
"""
27+
Status of a PostgresNode
28+
"""
29+
30+
Running,Stopped,Uninitialized=range(3)
31+
32+
# for Python 3.x
33+
def__bool__(self):
34+
returnself==NodeStatus.Running
35+
36+
# for Python 2.x
37+
__nonzero__=__bool__

‎testgres/node.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
importsubprocess
77
importtime
88

9-
fromenumimportEnum
109
fromshutilimportrmtree
1110
fromsiximportraise_from
1211
fromtempfileimportmkstemp,mkdtemp
1312

13+
from .enumsimportNodeStatus
14+
1415
from .cacheimportcached_initdb
1516

1617
from .configimporttestgres_config
@@ -59,20 +60,7 @@
5960
release_port, \
6061
execute_utility
6162

62-
63-
classNodeStatus(Enum):
64-
"""
65-
Status of a PostgresNode
66-
"""
67-
68-
Running,Stopped,Uninitialized=range(3)
69-
70-
# for Python 3.x
71-
def__bool__(self):
72-
returnself.value==NodeStatus.Running.value
73-
74-
# for Python 2.x
75-
__nonzero__=__bool__
63+
from .backupimportNodeBackup
7664

7765

7866
classPostgresNode(object):
@@ -858,7 +846,6 @@ def backup(self, **kwargs):
858846
A smart object of type NodeBackup.
859847
"""
860848

861-
from .backupimportNodeBackup
862849
returnNodeBackup(node=self,**kwargs)
863850

864851
defreplicate(self,name=None,**kwargs):

‎tests/test_simple.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ def test_backup_exhaust(self):
360360
withself.assertRaises(BackupException):
361361
backup.spawn_primary()
362362

363+
deftest_backup_wrong_xlog_method(self):
364+
withget_new_node()asnode:
365+
node.init(allow_streaming=True).start()
366+
367+
withself.assertRaises(BackupException,msg='Invalid xlog_method "wrong"'):
368+
node.backup(xlog_method='wrong')
369+
363370
deftest_replicate(self):
364371
withget_new_node()asnode:
365372
node.init(allow_streaming=True).start()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp