From 8929080e7a9a32d1bf748a5feb0e166c191641c3 Mon Sep 17 00:00:00 2001 From: Bernie Hackett Date: Mon, 12 Feb 2018 19:13:44 -0800 Subject: [PATCH] PYTHON-1445 PYTHON-1446 PYTHON-1448 - More examples --- test/test_examples.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/test_examples.py b/test/test_examples.py index 2a4376169..a3341d409 100644 --- a/test/test_examples.py +++ b/test/test_examples.py @@ -656,12 +656,14 @@ class TestSampleShellCommands(unittest.TestCase): def insert_docs(): while not done: - db.inventory.insert_one({}) + db.inventory.insert_one({"username": "alice"}) + db.inventory.delete_one({"username": "alice"}) t = threading.Thread(target=insert_docs) t.start() try: + # 1. The database for reactive, real-time applications # Start Changestream Example 1 cursor = db.inventory.watch() document = next(cursor) @@ -677,10 +679,47 @@ class TestSampleShellCommands(unittest.TestCase): cursor = db.inventory.watch(resume_after=resume_token) document = next(cursor) # End Changestream Example 3 + + # Start Changestream Example 4 + pipeline = [ + {"$match": + {"$or": [ + {"fullDocument.username": "alice"}, + {"operationType": {"$in": ["delete"]}}] + } + } + ] + cursor = db.inventory.watch(pipeline=pipeline) + document = next(cursor) + # End Changestream Example 4 finally: done = True t.join() + @client_context.require_version_min(3, 6, 0) + @client_context.require_replica_set + def test_misc(self): + # Marketing examples + client = client_context.client + self.addCleanup(client.drop_database, "test") + self.addCleanup(client.drop_database, "my_database") + + # 2. Tunable consistency controls + collection = client.my_database.my_collection + with client.start_session() as session: + collection.insert_one({'_id': 1}, session=session) + collection.update_one( + {'_id': 1}, {"$set": {"a": 1}}, session=session) + for doc in collection.find({}, session=session): + pass + + # 3. Exploiting the power of arrays + collection = client.test.array_updates_test + collection.update_one( + {'_id': 1}, + {"$set": {"a.$[i].b": 2}}, + array_filters=[{"i.b": 0}]) + if __name__ == "__main__": unittest.main()