lundi 28 décembre 2015

Mocking Googleads for Unit Tests

I have a service that uses the googleads library in python, I want to unit test these functions but do not know how to go about mocking this aspect. When I used PHP and Zend Framework it was pretty easy to mock clients, as I could tell what was expected to be called, and mock what was returned, but I do not know how to do this here.

Could you point to a good resource to do learn more about it? Here's some example code I'd like to test (get_account_timezone):

from googleads import AdWordsClient
from googleads.oauth2 import GoogleRefreshTokenClient
from dateutil import tz

class AdWords:
    ADWORDS_VERSION = 'v201509'

    def __init__(self, client_id, client_secret, refresh_token, dev_token, customer_id):
        """AdWords __init__ function

        Args:
            client_id (str):        OAuth2 client ID
            client_secret (str):    OAuth2 client secret
            refresh_token (str):    Refresh token
            dev_token (str):        Google AdWords developer token
            customer_id (str):      Google AdWords customer ID
        """
        self.client_id = client_id
        self.client_secret = client_secret
        self.refresh_token = refresh_token
        self.dev_token = dev_token
        self.customer_id = customer_id

        oauth2_client = GoogleRefreshTokenClient(
            self.client_id,
            self.client_secret,
            self.refresh_token
        )

        self.client = AdWordsClient(
            self.dev_token,
            oauth2_client,
            'Analytics',
            self.customer_id
        )

    def get_account_timezone(self):
        """Get timezone that current AdWords account is using

        Returns:
            Timezone
        """
        service = self.client.GetService('ManagedCustomerService', self.ADWORDS_VERSION)

        response = service.get({
            'fields': ['DateTimeZone']
        })

        if 'entries' not in response or len(response.entries) != 1:
            return tz.tzutc()

        account_timezone = response.entries[0].dateTimeZone

        return tz.gettz(account_timezone)

Thank you,

fermin

Aucun commentaire:

Enregistrer un commentaire