Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix uri_parser read_preference validation

  • Loading branch information...
commit f56460b2936b84d187474757a2c59224704fd069 1 parent 8aa99a5
Craig Hobbs authored behackett committed
View
9 pymongo/common.py
@@ -122,9 +122,14 @@ def validate_timeout_or_none(option, value):
def validate_read_preference(dummy, value):
"""Validate read preference for a ReplicaSetConnection.
"""
- if value not in read_preferences.modes:
+ if value in read_preferences.modes:
+ return value
+
+ # Also allow string form of enum for uri_parser
+ try:
+ return read_preferences.mongos_enum(value)
+ except ValueError:
raise ConfigurationError("Not a valid read preference")
- return value
def validate_tag_sets(dummy, value):
View
3  pymongo/read_preferences.py
@@ -83,6 +83,9 @@ class ReadPreference:
def mongos_mode(mode):
return _mongos_modes[mode]
+def mongos_enum(enum):
+ return _mongos_modes.index(enum)
+
def select_primary(members):
for member in members:
View
6 test/test_uri_parser.py
@@ -25,6 +25,7 @@
split_options,
parse_uri)
from pymongo.errors import ConfigurationError, InvalidURI
+from pymongo import ReadPreference
class TestURI(unittest.TestCase):
@@ -268,5 +269,10 @@ def test_parse_uri(self):
parse_uri("mongodb://fred:foobar@localhost/"
"test.yield_historical.in?slaveok=true"))
+ res = copy.deepcopy(orig)
+ res['options'] = {'read_preference': ReadPreference.SECONDARY}
+ self.assertEqual(res,
+ parse_uri("mongodb://localhost/?read_preference=secondary"))
+
if __name__ == "__main__":
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.