python unittest: mock cursor.fetchall() to return a dummy value inside a function

huangapple go评论91阅读模式
英文:

python unittest : mock cursor.fetchall() to return a dummy value inside a function

问题

  1. #file utils.py
  2. def update_configuration(configuration, mysql_client):
  3. query = "SELECT * from some database"
  4. cursor = mysql_client.execute_query(query)
  5. function_mapping = cursor.fetchall()
  6. cursor.close()
  7. configuration["mapping"] = {}
  8. for (display_name, formula_name) in function_mapping:
  9. configuration["formula_mapping"][formula_name] = display_name
  10. #main.py
  11. def main_func(configuration):
  12. mysql_client = get_mysql_connection()
  13. ut.update_configuration(configuration, mysql_client) #ut means utils.py
  14. #test.py
  15. @mock.patch("src.utils.cursor.fetchall", return_value=[1, 2, 3, 4])
  16. @mock.patch("src.main.get_sql_connection", return_value=mock.Mock())
  17. def test_initiate_calc(self, dummy1):
  18. # perform integration testing on "main_func"

The project structure is as shown below

  1. --project
  2. --src
  3. --tests
  4. --test_main.py
  5. --main.py
  6. --utils.py

When I try to mock the cursor.fetchall() to return some value, I get an error saying "ModuleNotFoundError: No module named 'src.utils.cursor'; 'src.utils' is not a package."

Need help in finding a way to get the function_mapping = cursor.fetchall() value to return some value.

英文:
  1. #file utils.py
  2. def update_configuration(configuration, mysql_client):
  3. query = "SELECT * from some database"
  4. cursor = mysql_client.execute_query(query)
  5. function_mapping = cursor.fetchall()
  6. cursor.close()
  7. configuration["mapping"] = {}
  8. for (display_name, formula_name) in function_mapping:
  9. configuration["formula_mapping"][formula_name] = display_name
  10. #main.py
  11. def main_func(configuration):
  12. mysql_client = get_mysql_connection()
  13. ut.update_configuration(configuration, mysql_client) #ut means utils.py
  14. #test.py
  15. @mock.patch("src.utils.cursor.fetchall",return_value = [1,2,3,4])
  16. @mock.patch("src.main.get_sql_connection", return_value = mock.Mock())
  17. def test_initiate_calc(self, dummy1):
  18. # perform integration testing on "main_func"

The project structure is as shown below

  1. --project
  2. --src
  3. --tests
  4. --test_main.py
  5. --main.py
  6. --utils.py

When I try to mock the cursor.fetchall() to return some value I get an error saying "ModuleNotFoundError: No module named 'src.utils.cursor'; 'src.utils' is not a package"

Need help in finding a way to get the function_mapping = cursor.fetchall() value some return value

答案1

得分: 1

你也可以尝试 cursor.fetchall.side_effect = "some-value"

类似这样的:

  1. @patch('connector')
  2. def test_create_table(self, connector):
  3. connection = Mock()
  4. cursor = Mock()
  5. connector.connect.return_value = connection
  6. connection.cursor.return_value = cursor
  7. cursor.fetchall.side_effect = "List of tuple"
  8. connector.connect.assert_called_with("creds")
  9. cursor.execute.assert_called_with("statements - you - are - trying - to - execute")

对于多个语句,可以使用 assert_has_calls

英文:

You can also try cursor.fetchall.side_effect = "some-value"

Something like this:

  1. @patch('connector')
  2. def test_create_table(self, connector):
  3. connection = Mock()
  4. cursor = Mock()
  5. connector.connect.return_value = connection
  6. connection.cursor.return_value = cursor
  7. cursor.fetchall.side_effect = "List of tuple"
  8. connector.connect.assert_called_with("creds")
  9. cursor.execute.assert_called_with("statements - you - are - trying - to - execute")

For multiple statements use assert_has_calls.

答案2

得分: 0

无法修补本地变量cursor,也不需要这样做。正确配置get_mysql_connection模拟函数。

  1. @mock.patch("src.main.get_sql_connection")
  2. def test_initiate_calc(self, mock_conn):
  3. mock_conn.return_value.execute_query.return_value.fetchall.return_value = [1,2,3,4]
  4. # 在"main_func"上执行集成测试
英文:

You can't patch the local variable cursor, nor do you need to. Configure the get_mysql_connection mock properly instead.

  1. @mock.patch("src.main.get_sql_connection")
  2. def test_initiate_calc(self, mock_conn):
  3. mock_conn.return_value.execute_query.return_value.fetchall.return_value = [1,2,3,4]
  4. # perform integration testing on "main_func"

huangapple
  • 本文由 发表于 2023年2月19日 23:32:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定