I am using Firestore with python to write Cloud Functions. I need to test a function with following query:
from google.cloud import firestore
MY_COLLECTION_GROUP = firestore.Client(project="my-project").collection_group("my_collection")
def function_to_test(data):
my_query = (
MY_COLLECTION_GROUP.where("eventDateTime", "==", data["dateTime"])
.where("user.id", "==", data["user_id"])
.where("user.age", ">=", data["valid_age"])
)
# fetch results from query and proceed further
To test the above function, I tried to mock MY_COLLECTION_GROUP
's where function to test the queries. Like this:
from mock import call, patch
@patch("mymodule.MY_COLLECTION_GROUP.where")
def test_function_to_test(mocked_where_function):
# called the function with some data
function_to_test(data)
# tried asserting the mock calls like this
mocked_where_function.assert_has_calls([
call("eventDateTime", "==", data["dateTime"]),
call("user.id", "==", data["user_id"]),
call("user.age", ">=", data["valid_age"]),
])
But it seems like it was only mocking the first where
function and not all, I got the following error while running test:
E AssertionError: Calls not found.
E Expected: [call('eventDateTime', '==', '2021, 09, 15'),
E call('user.id', '==', '12'),
E call('user.age', '>=', 18)]
E Actual: [call('eventDateTime', '==', '2021, 09, 15'),
E call().where('user.id', '==', '12'),
E call().where().where('user.age', '>=', 18))]
So, is there any way to mock all where functions and not just the first one?
What am I doing wrong here?