Oct-11-2019, 08:23 PM
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:
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:
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!
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:
1 2 3 4 5 6 7 |
def test_login(): """Test login """ soql = SoqlQuery() response = soql.login(USERNAME, PASSWORD, TOKEN, PROXIES) assert response.domain = = "login" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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 |
Any help would be greatly appreciated!