add testcase and changelog for pull:#1020 'improve _created status when switch collection and db'

This commit is contained in:
Breeze.kay 2015-06-19 15:40:05 +08:00
parent 0bbbbdde80
commit dfc7f35ef1
4 changed files with 84 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -503,6 +503,10 @@ class Document(BaseDocument):
: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
@ -531,6 +535,10 @@ class Document(BaseDocument):
: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

View File

@ -279,5 +279,78 @@ class SignalTests(unittest.TestCase):
# 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()