Search code examples
pythonpython-3.xunit-testingpython-unittestpython-unittest.mock

Python unittest case expected not matching with the actual


I am trying to mock the secrets manager client. Earlier the variables weren't in the class so I was able to mock the client directly using a patch like below:

@patch('my_repo.rc.client')

and now since I am using an instance method, I need to mock the instance method.

rc.py

import boto3
import json
from services.provisioner_logger import get_provisioner_logger
from services.exceptions import UnableToRetrieveDetails


class MyRepo(object):
    def __init__(self, region):
        self.client = self.__get_client(region)

    def id_lookup(self, category):
        logger = get_provisioner_logger()
        try:
            response = self.client.get_secret_value(SecretId=category)
            result = json.loads(response['SecretString'])
            logger.info("Got value for secret %s.", category)
            return result
        except Exception as e:
            logger.error("unable to retrieve secret details due to ", str(e))
            raise Exception("unable to retrieve secret details due to ", str(e))

    def __get_client(self, region):
        return boto3.session.Session().client(
            service_name='secretsmanager',
            region_name=region
        )

test_secrt.py

from unittest import TestCase
from unittest.mock import patch, MagicMock
from my_repo.rc import MyRepo
import my_repo


class TestSecretManagerMethod(TestCase):
    def test_get_secret_value(self):
        with patch.object(my_repo.rc.MyRepo, "id_lookup") as fake_bar_mock:
            fake_bar_mock.get_secret_value.return_value = {
                "SecretString": '{"secret": "gotsomecreds"}',
            }
            actual = MyRepo("eu-west-1").id_lookup("any-name")

            self.assertEqual(actual, {"secret": "gotsomecreds"})

Now, I tried a SO post to implement the same but the end result isn't matching. It gives results like below:

self.assertEqual(actual, {"secret": "gotsomecreds"})
AssertionError: <MagicMock name='id_lookup()' id='4589498032'> != {'secret': 'gotsomecreds'}  

I think I am close but unable to find out what exactly am I missing here.


Solution

  • OK, we want a Mock, we don't need a magic mock. In fact, we want 3.

    First, the session

    mock_session_object = Mock()
    

    Then the client,

    mock_client = Mock()
    

    This mock client will return you response:

    mock_client.get_secret_value.return_value = {
                "SecretString": '{"secret": "gotsomecreds"}',
            }
    

    The session client will return this:

    mock_session_object.client.return_value = mock_client
    

    OK. That was a lot, but we have clients inside sessions. Pulling it togther, we have

    from unittest import TestCase
    from unittest.mock import patch, Mock
    from credentials_repo.retrieve_credentials import CredentialsRepository
    import credentials_repo
    
    
    class TestSecretManagerMethod(TestCase):
        @patch("boto3.session.Session")
        def test_get_secret_value(self, mock_session_class):
            mock_session_object = Mock()
            mock_client = Mock()
            mock_client.get_secret_value.return_value = {
                    "SecretString": '{"secret": "gotsomecreds"}',
                }
            mock_session_object.client.return_value = mock_client
            mock_session_class.return_value = mock_session_object
            actual = CredentialsRepository("eu-west-1").find_by_id("db-creds")
    
            self.assertEqual(actual, {"secret": "gotsomecreds"})
    

    (The @path at the top is the same as a with inside, right?)