Python Forum
Help mocking simple-salesforce
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help mocking simple-salesforce
#1
Hello Everyone,

I'm new to mocking API calls in Python and can't find any documentation that shows any straightforward examples of how to mock calls to an API that uses Requests.

In particular, I need to mock calls to simple-salesforce. I've tried using Reponses and requests-mock, but I always hit some exception or another in the simples-salesforce library.

Can anyone help me with a simple mock of this call? If I can get one to work, I think i can probably get on well from there.

Here's the actual call from the test module:

def test_login():
    """Test login
    """
    soql = SoqlQuery()
    response = soql.login(USERNAME, PASSWORD, TOKEN, PROXIES)

    assert response.domain == "login"
SoqlQuery() is a wrapper I wrote over simple-salesforce that allows python object like lists to be submitted for a query call without having to parse out query strings all the time. The relevant part to logging in looks like this:

from simple_salesforce import Salesforce


class SoqlQuery:
    """
    Open a Salesforce instance.
    """

    def __init__(self, instance_url=None, sandbox=False):
        """
        :param instance_url: Use if you want to log into an
                         instance other than your default
        :param sandbox: Production or not
        """
        self.instance_url = instance_url

        if sandbox:
            self.sandbox = "test"

        else:
            self.sandbox = None

        self.salesforce = None

    def login(self, username, password, token, proxies):
        """
        Log into Salesforce
        :param username: Username
        :param password: Password
        :param token: Dev token
        :param proxies: Proxies to use
        :return: result
        """
        result = self.salesforce = Salesforce(
            username=username,
            password=password,
            security_token=token,
            instance_url=self.instance_url,
            domain=self.sandbox,
            proxies=proxies,
        )
        return result
There are also query and find methods, but I can probably figure that out if I can just mock a login correctly.

Any help would be greatly appreciated!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020