Abhas P
09/22/2021, 1:10 AMKevin Kho
Abhas P
09/22/2021, 9:32 PM@task
def mongo_connect():
db = get_db() // wrapper on top of pymongoclient to connect to the specified db
collection = get_collection(db). // wrapper on top of pymongoclient to get a specified collection form the db
docs = collection.find()
return docs
@task
def transform(docs):
//performs some data manipulation
with Flow("flow1") as flow:
docs = mongo_connect()
result = transform(docs)
I want to mock this task to return a certain set of documents, so I can test my entire flow run and assert the expected output.Ben Muller
09/22/2021, 10:37 PMtasks.py
and another file functions.py.
In your example you could have
#functions.py
def mongo_connect():
db = get_db() // wrapper on top of pymongoclient to connect to the specified db
collection = get_collection(db). // wrapper on top of pymongoclient to get a specified collection form the db
docs = collection.find()
return docs
#tasks.py
from prefect import task
from functions import mongo_connect
@task
def mongo_connect_task():
return mongo_connect()
now when you do a mock in your tests you can simply do something like
@mock.patch("tasks.mongo_connect")
and this will mock the functionality of the taskAbhas P
09/22/2021, 10:42 PMBen Muller
09/22/2021, 10:44 PMAbhas P
09/22/2021, 10:48 PMMary Clair Thompson
10/06/2021, 12:42 PM