Skip to content

Commit

Permalink
- Test for MONGO_OPTIONS config setting for app and resource.
Browse files Browse the repository at this point in the history
- Flask-PyMongo compatibility for MONGO_CONNECT config setting.
  • Loading branch information
mrs83 committed Oct 21, 2016
1 parent 2f0aaad commit c580f55
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ uppercase.

``MONGO_OPTIONS`` MongoDB keyword arguments to passed to
MongoClient class ``__init__``.
Defaults to ``{}``.
Defaults to ``{'connect': True}``.

``MONGO_HOST`` MongoDB server address. Defaults to ``localhost``.

Expand Down
5 changes: 5 additions & 0 deletions eve/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,8 @@
# Explicitly set default write_concern to 'safe' (do regular
# aknowledged writes). This is also the current PyMongo/Mongo default setting.
MONGO_WRITE_CONCERN = {'w': 1}
MONGO_OPTIONS = {
'connect': True
}
# Compatibility for flask-pymongo.
MONGO_CONNECT = MONGO_OPTIONS['connect']
12 changes: 12 additions & 0 deletions eve/flaskapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ def load_config(self):
if os.environ.get(envvar):
self.config.from_envvar(envvar)

# flask-pymongo compatibility
self.config['MONGO_CONNECT'] = self.config['MONGO_OPTIONS'].get(
'connect', True
)

def validate_domain_struct(self):
""" Validates that Eve configuration settings conform to the
requirements.
Expand Down Expand Up @@ -899,6 +904,13 @@ def register_resource(self, resource, settings):

create_index(self, resource, name, list_of_keys, index_options)

# flask-pymongo compatibility.
if 'MONGO_OPTIONS' in self.config['DOMAIN']:
connect = self.config['DOMAIN']['MONGO_OPTIONS'].get(
'connect', True
)
self.config['DOMAIN']['MONGO_CONNECT'] = connect

def register_error_handlers(self):
""" Register custom error handlers so we make sure that all errors
return a parseable body.
Expand Down
36 changes: 36 additions & 0 deletions eve/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,39 @@ def test_custom_error_handlers(self):

challenge = lambda code: self.assertTrue(code in handlers) # noqa
map(challenge, codes)

def test_mongodb_settings(self):
# Create custom app with mongodb settings.
settings = {
'DOMAIN': {'contacts': {}},
'MONGO_OPTIONS': {
'connect': False
}
}
app = Eve(settings=settings)
# Check if settings are set.
self.assertEqual(
app.config['MONGO_OPTIONS']['connect'],
app.config['MONGO_CONNECT']
)
# Prepare a specific schema with mongo specific settings.
settings = {
'schema': {
'name': {'type': 'string'},
},
'MONGO_OPTIONS': {
'connect': False
}
}
self.app.register_resource('mongodb_settings', settings)
# check that settings are set.
resource_settings = self.app.config['DOMAIN']['mongodb_settings']
self.assertEqual(
resource_settings['MONGO_OPTIONS'],
settings['MONGO_OPTIONS']
)
# check that settings are set.
self.assertEqual(
resource_settings['MONGO_OPTIONS']['connect'],
settings['MONGO_OPTIONS']['connect']
)

0 comments on commit c580f55

Please sign in to comment.