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.
This commit is contained in:
Mike Salvatore 2022-09-14 15:58:35 -04:00
parent 4f29326da9
commit 51d5176986
1 changed files with 9 additions and 5 deletions

View File

@ -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)