Skip to content

Commit

Permalink
add testcase and changelog for pull:#1020 'improve _created status wh…
Browse files Browse the repository at this point in the history
…en switch collection and db'
  • Loading branch information
9nix00 committed Jun 19, 2015
1 parent 0bbbbdd commit dfc7f35
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Expand Up @@ -224,3 +224,5 @@ that much better:
* Matthieu Rigal (https://github.com/MRigal)
* Charanpal Dhanjal (https://github.com/charanpald)
* Emmanuel Leblond (https://github.com/touilleMan)
* Breeze.Kay (https://github.com/9nix00)

1 change: 1 addition & 0 deletions docs/changelog.rst
Expand Up @@ -5,6 +5,7 @@ Changelog

Changes in 0.9.X - DEV
======================
- improve Document._created status when switch collection and db #1020
- Queryset update doesn't go through field validation #453
- Added support for specifying authentication source as option `authSource` in URI. #967
- Fixed mark_as_changed to handle higher/lower level fields changed. #927
Expand Down
8 changes: 8 additions & 0 deletions mongoengine/document.py
Expand Up @@ -503,6 +503,10 @@ def switch_db(self, db_alias, keep_created=True):
:param str db_alias: The database alias to use for saving the document
:param bool keep_created: keep self._created value after call `swith_db()` when True,
else will always set self._created value to True
.. seealso::
Use :class:`~mongoengine.context_managers.switch_collection`
if you need to read from another collection
Expand Down Expand Up @@ -531,6 +535,10 @@ def switch_collection(self, collection_name, keep_created=True):
:param str collection_name: The database alias to use for saving the
document
:param bool keep_created: keep self._created value after call `swith_db()` when True,
else will always set self._created value to True
.. seealso::
Use :class:`~mongoengine.context_managers.switch_db`
if you need to read from another database
Expand Down
73 changes: 73 additions & 0 deletions tests/test_signals.py
Expand Up @@ -279,5 +279,78 @@ def test_signals_with_explicit_doc_ids(self):
# second time, it must be an update
self.assertEqual(self.get_signal_output(ei.save), ['Is updated'])

def test_signals_with_switch_collection(self):
ei = self.ExplicitId(id=123)
ei.switch_collection("explicit__1")
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])
ei.switch_collection("explicit__1")
self.assertEqual(self.get_signal_output(ei.save), ['Is updated'])

ei.switch_collection("explicit__1", keep_created=False)
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])
ei.switch_collection("explicit__1", keep_created=False)
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])

def test_signals_with_switch_db(self):
connect('mongoenginetest')
register_connection('testdb-1', 'mongoenginetest2')

ei = self.ExplicitId(id=123)
ei.switch_db("testdb-1")
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])
ei.switch_db("testdb-1")
self.assertEqual(self.get_signal_output(ei.save), ['Is updated'])

ei.switch_db("testdb-1", keep_created=False)
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])
ei.switch_db("testdb-1", keep_created=False)
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])

def test_signals_with_switch_sharding_db(self):

import pymongo
from mongoengine.connection import get_connection

connect('mongoenginetest', alias='testdb1')
expected_connection = get_connection('testdb1')

connect('mongoenginetest', alias='testdb2')
actual_connection = get_connection('testdb2')

if pymongo.version_tuple[0] < 3:
IS_PYMONGO_3 = False
else:
IS_PYMONGO_3 = True

ei = self.ExplicitId(id=123)
ei.switch_db("testdb1")
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])
ei.switch_db("testdb1")
self.assertEqual(self.get_signal_output(ei.save), ['Is updated'])

ei.switch_db("testdb2", keep_created=False)
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])
ei.switch_db("testdb2", keep_created=False)
self.assertEqual(self.get_signal_output(ei.save), ['Is created'])

# Handle PyMongo 3+ Async Connection
if IS_PYMONGO_3:
# Ensure we are connected, throws ServerSelectionTimeoutError otherwise.
# Purposely not catching exception to fail test if thrown.
expected_connection.server_info()

self.assertEqual(expected_connection, actual_connection)












if __name__ == '__main__':
unittest.main()

0 comments on commit dfc7f35

Please sign in to comment.