Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3175125

Browse files
authored
Merge pull request #33 from Valeria1235/simplified_enums
Simplified enums
2 parents be1e3c5 + bc28551 commit 3175125

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 .connection import \
12-
IsolationLevel, \
1312
NodeConnection, \
1413
InternalError, \
1514
ProgrammingError
1615

1716
from .exceptions import *
18-
from .node import NodeStatus, PostgresNode
17+
from .enums import *
18+
from .node import PostgresNode
1919

2020
from .utils import \
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
from six import raise_from
77
from tempfile import mkdtemp
88

9+
from .enums import XLogMethod
10+
911
from .consts import \
1012
DATA_DIR, \
1113
TMP_NODE, \
@@ -50,7 +52,13 @@ def __init__(self,
5052
if not node.status():
5153
raise BackupException('Node must be running')
5254

53-
# yapf: disable
55+
# Check arguments
56+
if not isinstance(xlog_method, XLogMethod):
57+
try:
58+
xlog_method = XLogMethod(xlog_method)
59+
except ValueError:
60+
raise BackupException('Invalid xlog_method "{}"'.format(xlog_method))
61+
5462
# Set default arguments
5563
username = username or default_username()
5664
base_dir = base_dir or mkdtemp(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
except ImportError:
1010
raise ImportError("You must have psycopg2 or pg8000 modules installed")
1111

12+
from .enums import IsolationLevel
13+
1214
from .defaults import \
1315
default_dbname, \
1416
default_username
1517

16-
from enum import Enum
17-
1818
from .exceptions import QueryException
1919

2020

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

2525

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

7466
def begin(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-
assert level_idx in range(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+
if not isinstance(isolation_level, IsolationLevel):
9469
# Get name of isolation level
9570
level_str = str(isolation_level).lower()
9671

9772
# Validate level string
98-
if level_str not in levels:
73+
try:
74+
isolation_level = IsolationLevel(level_str)
75+
except ValueError:
9976
error = 'Invalid isolation level "{}"'
10077
raise QueryException(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
return self
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 .enums import XLogMethod
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+
from enum import Enum, IntEnum
2+
3+
4+
class XLogMethod(Enum):
5+
"""
6+
Available WAL methods for NodeBackup
7+
"""
8+
9+
none = 'none'
10+
fetch = 'fetch'
11+
stream = 'stream'
12+
13+
14+
class IsolationLevel(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+
class NodeStatus(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+
return self == 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
import subprocess
77
import time
88

9-
from enum import Enum
109
from shutil import rmtree
1110
from six import raise_from
1211
from tempfile import mkstemp, mkdtemp
1312

13+
from .enums import NodeStatus
14+
1415
from .cache import cached_initdb
1516

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

62-
63-
class NodeStatus(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-
return self.value == NodeStatus.Running.value
73-
74-
# for Python 2.x
75-
__nonzero__ = __bool__
63+
from .backup import NodeBackup
7664

7765

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

861-
from .backup import NodeBackup
862849
return NodeBackup(node=self, **kwargs)
863850

864851
def replicate(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
with self.assertRaises(BackupException):
361361
backup.spawn_primary()
362362

363+
def test_backup_wrong_xlog_method(self):
364+
with get_new_node() as node:
365+
node.init(allow_streaming=True).start()
366+
367+
with self.assertRaises(BackupException, msg='Invalid xlog_method "wrong"'):
368+
node.backup(xlog_method='wrong')
369+
363370
def test_replicate(self):
364371
with get_new_node() as node:
365372
node.init(allow_streaming=True).start()

0 commit comments

Comments
 (0)