I have this function which makes a call to database using SQLalchemy ORM.
def get_cust_id(self,_session_id):
cust_id: str = self.sqlalchemy_orm.get_customer_id_sqlalchemy(_session_id)
if cust_id is None:
return _session_id
return cust_id
def get_customer_id_sqlalchemy(self, _session_id: int) -> str:
try:
if self.session is None:
self.session = self._connection.get_session()
_res_id = self.session.query(Users).with_entities(Users.cust_id).filter(
Users.age > 20).order_by(Users.id.desc()).one_or_none()
if _res_id is None:
return _res_id
else:
return __res_id[0]
finally:
if not self.persistent_connection:
self.session.close()
I want to unit test the get_cust_id()and mock database calls, how should I do it using pytest and mock?