I have Fast API with a Mongo Database
I try to realize my tests for some functions which need mongo connection
Here my collection users and its fields:
from mongoengine import Document, EmailField, StringField, EnumField
class User(Document):
meta = {"collection": "users"}
email = EmailField(unique=True)
hashed_password = StringField()
role = EnumField(UserRole)
def username(self):
return self.email.split('@')[0]
def __str__(self):
return f'{self.email} ({self.role})
Here my function
class UserService:
@staticmethod
def existing_user(email: str) -> Optional[User]:
try:
user = User.objects(email=email).first()
except ServerSelectionTimeoutError as e:
raise e
return user
What is the best way to test this function please (using pytest)? I would like to mock my MongoDB but I didn’t manage with mongomock or directly with monkeypatch
Thank you very much
My test file
from domain.entities.user import UserRole, User
from mongomock import MongoClient
class TestUserServiceMethods:
email="[email protected]"
# User example
user = User(email=email,
role=UserRole.admin,
hashed_password="hashed_pwd")
add_user = {
"email": email,
"role": UserRole.admin,
"hashed_password": "hashed_pwd"
}
user_service = UserService()
client = MongoClient()
mock_db = client['ml-db']
user_collection = mock_db['users']
user_collection.insert_one(add_user)
def test_existing_user(self, monkeypatch):
with monkeypatch.context() as m:
m.setattr('pymongo.MongoClient', lambda *args, **kwargs: self.client)
result = self.user_service.existing_user(self.email)
assert result == self.user
It seems the mock didn’t sucess because I got as error :
mongoengine.connection.ConnectionFailure: You have not defined a default connection
Should I use directly Fast app with test_client?
2
Answers
UPDATE As Ozs said, the best way is to create a Fake database and to drop it at the end. Here an example which resolves my problem
You need to run connect method from mogoengine with db_name (default alias is "default") and connection_string before any action you do.
for example :
the client in the code would be from monfoengine and in tests it would be from mongomock