[英]Python mock aggregate function of MongoDB in Pymongo
我想為下面的代碼模擬PyMongo
提供的aggregate
函數:
client = MongoClient(host="localhost", port=27017,username="Harsha", password="Harsha", authSource="admin")
db_obj = client["DB name"]
mongo_result = db_obj[collection_name].aggregate(pipeline)
我想模擬aggregate
函數。
誰能幫我模擬集合函數嗎?
我嘗試了以下代碼段來模擬聚合函數:
試用1:
from pymongo import collection
collection_obj = collection.Collection(client["DB name"], "collection_name")
def mock_get(self, *args):
return "Result I want"
@mock.patch(collection_obj.Collection.aggregate, side_effect=mock_get)
def test_demo(self):
.
.
.
.
這不起作用,因為@mock.patch
需要字符串完整路徑參數。
所以我也試圖給出完整的aggregation
函數路徑
試用2:
class BasicTest(unittest.TestCase):
def mock_get(self, *args):
return "Result I want"
@mock.patch('pymongo.collection.Collection.aggregate', side_effect=mock_get)
def test_demo(self):
.
.
.
.
這給了我:
TypeError: test_demo() takes 1 positional argument but 2 were given
patch將模擬對象作為附加參數傳遞給修飾的函數,以便您可以對其進行斷言。 像這樣更改代碼:
@mock.patch('pymongo.collection.Collection.aggregate', side_effect=mock_get)
def test_demo(self, mock_object):
.
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.