From 51d51769868c62b07ba8155a04ad42070d0e28b4 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 15:58:35 -0400 Subject: [PATCH] UT: Improve MongoNodeRepository test coverage Setting the MagicMock members to lambda's did not work as expected. A look at the code coverage report revealed that not all branches were covered. Setting them to new MagicMock objects with the correct return type added coverage to branches that were previously skipped. --- .../cc/repository/test_mongo_node_repository.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_node_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_node_repository.py index b056cb575..9d1ee6279 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_node_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_node_repository.py @@ -158,7 +158,7 @@ def test_upsert_communication__find_one_fails(error_raising_node_repository): def test_upsert_communication__replace_one_fails( error_raising_mock_mongo_client, error_raising_node_repository ): - error_raising_mock_mongo_client.monkey_island.nodes.find_one = lambda _: None + error_raising_mock_mongo_client.monkey_island.nodes.find_one = MagicMock(return_value=None) with pytest.raises(StorageError): error_raising_node_repository.upsert_communication(1, 2, CommunicationType.SCANNED) @@ -169,8 +169,10 @@ def test_upsert_communication__replace_one_matched_without_modify( mock_result = MagicMock() mock_result.matched_count = 1 mock_result.modified_count = 0 - error_raising_mock_mongo_client.monkey_island.nodes.find_one = lambda _: None - error_raising_mock_mongo_client.monkey_island.nodes.replace_one = lambda *_, **__: mock_result + error_raising_mock_mongo_client.monkey_island.nodes.find_one = MagicMock(return_value=None) + error_raising_mock_mongo_client.monkey_island.nodes.replace_one = MagicMock( + return_value=mock_result + ) with pytest.raises(StorageError): error_raising_node_repository.upsert_communication(1, 2, CommunicationType.SCANNED) @@ -182,8 +184,10 @@ def test_upsert_communication__replace_one_insert_fails( mock_result = MagicMock() mock_result.matched_count = 0 mock_result.upserted_id = None - error_raising_mock_mongo_client.monkey_island.nodes.find_one = lambda _: None - error_raising_mock_mongo_client.monkey_island.nodes.replace_one = lambda *_, **__: mock_result + error_raising_mock_mongo_client.monkey_island.nodes.find_one = MagicMock(return_value=None) + error_raising_mock_mongo_client.monkey_island.nodes.replace_one = MagicMock( + return_value=mock_result + ) with pytest.raises(StorageError): error_raising_node_repository.upsert_communication(1, 2, CommunicationType.SCANNED)