Previously, we were running the test suite for several combinations of MongoDB, Python, and PyMongo: - PyPy, MongoDB v2.6, PyMongo v3.x (which really means v3.6.1 at the moment) - Python v2.7, MongoDB v2.6, PyMongo v3.x - Python v3.5, MongoDB v2.6, PyMongo v3.x - Python v3.6, MongoDB v2.6, PyMongo v3.x - Python v2.7, MongoDB v3.0, PyMongo v3.5.0 - Python v3.6, MongoDB v3.0, PyMongo v3.5.0 - Python v3.5, MongoDB v3.2, PyMongo v3.x - Python v3.6, MongoDB v3.2, PyMongo v3.x - Python v3.6, MongoDB v3.4, PyMongo v3.x - Python v3.6, MongoDB v3.6, PyMongo v3.x There were a couple issues with this setup: 1. MongoDB v2.6 – v3.2 have reached their End of Life already (v2.6 almost 3 years ago!). See the "MongoDB Server" section on https://www.mongodb.com/support-policy. 2. We were only testing two recent-ish PyMongo versions (v3.5.0 & v3.6.1). We were not testing the oldest actively supported MongoDB/PyMongo/Python setup. This PR updates the test matrix so that these problems are solved. For the sake of simplicity, it does not yet attempt to cover MongoDB v4.0: - PyPy, MongoDB v3.4, PyMongo v3.x (aka v3.6.1 at the moment) - Python v2.7, MongoDB v3.4, PyMongo v3.x - Python v3.5, MongoDB v3.4, PyMongo v3.x - Python v3.6, MongoDB v3.4, PyMongo v3.x - Python v2.7, MongoDB v3.4, PyMongo v3.4 - Python v3.6, MongoDB v3.6, PyMongo v3.x
		
			
				
	
	
		
			130 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import unittest
 | |
| 
 | |
| from mongoengine import connect, Document, IntField, StringField, ListField
 | |
| 
 | |
| __all__ = ("FindAndModifyTest",)
 | |
| 
 | |
| 
 | |
| class Doc(Document):
 | |
|     id = IntField(primary_key=True)
 | |
|     value = IntField()
 | |
| 
 | |
| 
 | |
| class FindAndModifyTest(unittest.TestCase):
 | |
| 
 | |
|     def setUp(self):
 | |
|         connect(db="mongoenginetest")
 | |
|         Doc.drop_collection()
 | |
| 
 | |
|     def assertDbEqual(self, docs):
 | |
|         self.assertEqual(list(Doc._collection.find().sort("id")), docs)
 | |
| 
 | |
|     def test_modify(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         doc = Doc(id=1, value=1).save()
 | |
| 
 | |
|         old_doc = Doc.objects(id=1).modify(set__value=-1)
 | |
|         self.assertEqual(old_doc.to_json(), doc.to_json())
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": -1}])
 | |
| 
 | |
|     def test_modify_with_new(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         doc = Doc(id=1, value=1).save()
 | |
| 
 | |
|         new_doc = Doc.objects(id=1).modify(set__value=-1, new=True)
 | |
|         doc.value = -1
 | |
|         self.assertEqual(new_doc.to_json(), doc.to_json())
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": -1}])
 | |
| 
 | |
|     def test_modify_not_existing(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         self.assertEqual(Doc.objects(id=1).modify(set__value=-1), None)
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}])
 | |
| 
 | |
|     def test_modify_with_upsert(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         old_doc = Doc.objects(id=1).modify(set__value=1, upsert=True)
 | |
|         self.assertEqual(old_doc, None)
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": 1}])
 | |
| 
 | |
|     def test_modify_with_upsert_existing(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         doc = Doc(id=1, value=1).save()
 | |
| 
 | |
|         old_doc = Doc.objects(id=1).modify(set__value=-1, upsert=True)
 | |
|         self.assertEqual(old_doc.to_json(), doc.to_json())
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": -1}])
 | |
| 
 | |
|     def test_modify_with_upsert_with_new(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         new_doc = Doc.objects(id=1).modify(upsert=True, new=True, set__value=1)
 | |
|         self.assertEqual(new_doc.to_mongo(), {"_id": 1, "value": 1})
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": 1}])
 | |
| 
 | |
|     def test_modify_with_remove(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         doc = Doc(id=1, value=1).save()
 | |
| 
 | |
|         old_doc = Doc.objects(id=1).modify(remove=True)
 | |
|         self.assertEqual(old_doc.to_json(), doc.to_json())
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}])
 | |
| 
 | |
|     def test_find_and_modify_with_remove_not_existing(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         self.assertEqual(Doc.objects(id=1).modify(remove=True), None)
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}])
 | |
| 
 | |
|     def test_modify_with_order_by(self):
 | |
|         Doc(id=0, value=3).save()
 | |
|         Doc(id=1, value=2).save()
 | |
|         Doc(id=2, value=1).save()
 | |
|         doc = Doc(id=3, value=0).save()
 | |
| 
 | |
|         old_doc = Doc.objects().order_by("-id").modify(set__value=-1)
 | |
|         self.assertEqual(old_doc.to_json(), doc.to_json())
 | |
|         self.assertDbEqual([
 | |
|             {"_id": 0, "value": 3}, {"_id": 1, "value": 2},
 | |
|             {"_id": 2, "value": 1}, {"_id": 3, "value": -1}])
 | |
| 
 | |
|     def test_modify_with_fields(self):
 | |
|         Doc(id=0, value=0).save()
 | |
|         Doc(id=1, value=1).save()
 | |
| 
 | |
|         old_doc = Doc.objects(id=1).only("id").modify(set__value=-1)
 | |
|         self.assertEqual(old_doc.to_mongo(), {"_id": 1})
 | |
|         self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": -1}])
 | |
| 
 | |
|     def test_modify_with_push(self):
 | |
|         class BlogPost(Document):
 | |
|             tags = ListField(StringField())
 | |
| 
 | |
|         BlogPost.drop_collection()
 | |
| 
 | |
|         blog = BlogPost.objects.create()
 | |
| 
 | |
|         # Push a new tag via modify with new=False (default).
 | |
|         BlogPost(id=blog.id).modify(push__tags='code')
 | |
|         self.assertEqual(blog.tags, [])
 | |
|         blog.reload()
 | |
|         self.assertEqual(blog.tags, ['code'])
 | |
| 
 | |
|         # Push a new tag via modify with new=True.
 | |
|         blog = BlogPost.objects(id=blog.id).modify(push__tags='java', new=True)
 | |
|         self.assertEqual(blog.tags, ['code', 'java'])
 | |
| 
 | |
|         # Push a new tag with a positional argument.
 | |
|         blog = BlogPost.objects(id=blog.id).modify(
 | |
|             push__tags__0='python',
 | |
|             new=True)
 | |
|         self.assertEqual(blog.tags, ['python', 'code', 'java'])
 | |
| 
 | |
|         # Push multiple new tags with a positional argument.
 | |
|         blog = BlogPost.objects(id=blog.id).modify(
 | |
|             push__tags__1=['go', 'rust'],
 | |
|             new=True)
 | |
|         self.assertEqual(blog.tags, ['python', 'go', 'rust', 'code', 'java'])
 | |
| 
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     unittest.main()
 |