Package: simple-mockforce
IntroductionThis library was inspired bymotoand mimics some of its design. Mainly,
nosimple-salesforcecode is patched; instead, the HTTP calls it makes are intercepted, and state is
stored in an in-memory, virtual Salesforce organization, which is just a globally instantiated class that
is created at the run-time of a test-suite.Installationpip install simple-mockforceor, with poetrypoetry add simple-mockforceUsageTo patch calls to the Salesforce API and instead interact with the "virtual"
Salesforce organization provided by this library, add the following:importosfromsimple_mockforceimportmock_salesforcefromsimple_salesforceimportSalesforce@mock_salesforcedeftest_api():# The username, password, and security token are ignored - any value will work.salesforce=Salesforce(username=os.getenv("SFDC_USERNAME"),password=os.getenv("SFDC_PASSWORD"),security_token=os.getenv("SFDC_SECURITY_TOKEN"))response=salesforce.Account.create({"Name":"Test Account"})account_id=response["id"]account=salesforce.Account.get(account_id)assertaccount["Name"]=="Test Account"And that's about it!CaveatsCase sensitivityUnlike a real Salesforce organization, the virtual organization will not handle case-insensitive
dependent code for you. You must remain consistent with your casing of object and field
names in all aspects of the code.Missing endpointsThe following features are currently not supported:the describe APIbulk queriesSOSL searchesQueriesSOQL is only partially supported as of now. Please refer to the README
forpython-soql-parserto see what's not yet implemented.You should only expect this library to be able to mock the most basic of queries.
While there are plans to, mocking query calls which traverse object relationships
or that use SOQL-specific where-clause tokens are not yet supported.Notable mentions:be explicit with direction inORDER BYclauses, i.e., always supplyDESCorASCattributes of parent objects can be specified in theselectclause (but not in thewhereclause)Error handlingError handling is only mocked to a degree, and for some calls it isn't at all.
This is because the virtual Salesforce organization does not yet enforce any of
the server-side validation you might encounter when working with the real API.This means that the virtual organization is much more permissive and loose than a
real Salesforce organization would be.There are plans to read the XML consumed by the meta API in order to enforce
more rigidity inside the virtual organization, but this is not yet implemented.All HTTP traffic is blockedWhen using@mock_salesforce, do note that therequestslibrary is being
patched withresponses, so any calls you make to any other APIs will fail
unless you patch them yourself, or patch the code which invokes said calls.RelationsRelations are the weakest part of this library, and some features are just
plain not supported yet.If you have a relational field that points to an object whose name cannot be
inferred from the field name (e.g., fromAccount__rit can be inferred
that this is pointing to anAccountobject), you can create a file calledrelations.jsonthat translates a relational field name to your intended
Salesforce object's name. Seerelations.jsonin the test folder for an
example.To specify the location ofrelations.json, set an environment variable
calledMOCKFORCE_RELATIONS_ROOTwhich points to the parent folder ofrelations.json. Note, this defaults to the current directory..