Skip to content

Commit 8929080

Browse files
committed
PYTHON-1445 PYTHON-1446 PYTHON-1448 - More examples
1 parent 077fb20 commit 8929080

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

test/test_examples.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,14 @@ def test_change_streams(self):
656656

657657
def insert_docs():
658658
while not done:
659-
db.inventory.insert_one({})
659+
db.inventory.insert_one({"username": "alice"})
660+
db.inventory.delete_one({"username": "alice"})
660661

661662
t = threading.Thread(target=insert_docs)
662663
t.start()
663664

664665
try:
666+
# 1. The database for reactive, real-time applications
665667
# Start Changestream Example 1
666668
cursor = db.inventory.watch()
667669
document = next(cursor)
@@ -677,10 +679,47 @@ def insert_docs():
677679
cursor = db.inventory.watch(resume_after=resume_token)
678680
document = next(cursor)
679681
# End Changestream Example 3
682+
683+
# Start Changestream Example 4
684+
pipeline = [
685+
{"$match":
686+
{"$or": [
687+
{"fullDocument.username": "alice"},
688+
{"operationType": {"$in": ["delete"]}}]
689+
}
690+
}
691+
]
692+
cursor = db.inventory.watch(pipeline=pipeline)
693+
document = next(cursor)
694+
# End Changestream Example 4
680695
finally:
681696
done = True
682697
t.join()
683698

699+
@client_context.require_version_min(3, 6, 0)
700+
@client_context.require_replica_set
701+
def test_misc(self):
702+
# Marketing examples
703+
client = client_context.client
704+
self.addCleanup(client.drop_database, "test")
705+
self.addCleanup(client.drop_database, "my_database")
706+
707+
# 2. Tunable consistency controls
708+
collection = client.my_database.my_collection
709+
with client.start_session() as session:
710+
collection.insert_one({'_id': 1}, session=session)
711+
collection.update_one(
712+
{'_id': 1}, {"$set": {"a": 1}}, session=session)
713+
for doc in collection.find({}, session=session):
714+
pass
715+
716+
# 3. Exploiting the power of arrays
717+
collection = client.test.array_updates_test
718+
collection.update_one(
719+
{'_id': 1},
720+
{"$set": {"a.$[i].b": 2}},
721+
array_filters=[{"i.b": 0}])
722+
684723

685724
if __name__ == "__main__":
686725
unittest.main()

0 commit comments

Comments
 (0)