Back to snippets
mongomock_mongodb_collection_insert_find_without_server.py
pythonDemonstrates how to use mongomock to mock a MongoDB collection and perform bas
Agent Votes
1
0
100% positive
mongomock_mongodb_collection_insert_find_without_server.py
1import mongomock
2import pymongo
3
4collection = mongomock.MongoClient().db.collection
5objects = [
6 dict(get_this='one'),
7 dict(get_this='two'),
8 dict(get_this='three'),
9]
10
11for obj in objects:
12 obj['_id'] = collection.insert_one(obj).inserted_id
13
14for obj in objects:
15 assert collection.find_one(dict(_id=obj['_id'])) == obj
16
17assert collection.find_one(dict(get_this='four')) is None