jeudi 16 janvier 2020

Pytest how do I mock side-effects for a function call that depends on its parent call parameter

This is my current test:

My setup:

class CheckTestParentMocked:
    @pytest.fixture(autouse=True)
    def run_around_tests(self, mocker):
        self.profile = 'myprofile'
        self.region = 'eu-west-1'
        mocked_boto = mocker.patch(self.boto_client_path) #mypackage.module.boto3
        mocked_session = mocked_boto.Session()
        self.mocked_client = mocked_session.client()

My actual est:

def test_gets_non_authorizer_api(self):
    def side_effect(*args, **kwargs):
        if args or kwargs:
            # these are resources
            return [{
                'items': [
                    {
                        'id': 'resource-id-foo',
                        'path': '/',
                        'resourceMethods': ['GET']
                    }
                ]
            }]
        else:
            # these are apis
            return [{'items': [
                {
                    'id': 'foo',
                    'name': 'foo-name'
                }
            ]
            }]

    self.paginator.paginate.side_effect = side_effect

    self.mocked_client.get_method.return_value = {
        'authorizationType': 'NONE'
    }
    assertion = {
        'API Name': 'foo-name', 'API Methods': 'GET', 'Resource Path': '/', 'Region': self.region,
        'Authorization Type': 'NONE'
    }
    self.mocked_client.get_paginator('get_rest_apis').paginate()
    self.mocked_client.get_paginator('get_resources').paginate(restApiId='someid')

paginate()'s result depends on the parameter passed to get_paginator. Right now I'm lucky that I can use paginate's params to determine what the behavior should be, but how can I define a mock so that paginate() returns me specific values based on the get_paginator params?

Aucun commentaire:

Enregistrer un commentaire