I am trying to write a test case test_is_user_present() which calls another function execute_redshift_sql() from redshift_util.py script
I set the expected return value from the function execute_redshift_sql() to 1 . But I never get this value returned from result after the function is called ! I also printed some values for debugging purpose
You can take a look at test case below
from mock import patch, Mock, MagicMock
from cia_admin_operations.redshift_util import execute_redshift_sql
@patch('cia_admin_operations.redshift_util.execute_redshift_sql')
def test_is_user_present(mock_execute_redshift_sql):
ldap_user = "dummy_user"
mock_out = Mock()
user_check_sql = "SELECT COUNT(1) FROM pg_user WHERE usename = '{}';".format(ldap_user)
mock_execute_redshift_sql.return_value = 1
print(mock_execute_redshift_sql())
result = execute_redshift_sql(mock_out, user_check_sql)
print(result)
print(result())
> assert result() == 1
E AssertionError: assert <Mock name='m...749067684720'> == 1
E -<Mock name='mock.query().getresult()()' id='139749067684720'>
E +1
test/test_cia_admin_operations.py:51: AssertionError
----------------------------- Captured stdout call -----------------------------
1
<Mock name='mock.query().getresult()' id='139749067684776'>
<Mock name='mock.query().getresult()()' id='139749067684720'>
redshift_util.py
def execute_redshift_sql(connection, sqlQuery):
"""Executes redshift query"""
logger.info("Executing following SQL query :\n %s" % sqlQuery)
try:
result = connection.query(sqlQuery)
logger.info("Redshift query is successfully executed.")
except Exception as e:
logger.error("Query not executed : %s" % e)
return None
# return only if the result has some data
if result:
logger.info("Query result :\n %s" % result)
return result.getresult()
else:
return 0
cia_admin_operations.redshift_util.execute_redshift_sql) is mocked thats why you see this printedname='mock.query().getresult()'. What I am expecting is the function should return the return value I set for testing purpose.mock_outin the unmocked function. The used function seems not to be the same as the mocked one, thus I asked to show how it is imported.