Skip to main content

Introduction

This library was inspired by moto and mimics some of its design. Mainly, no simple-salesforce code 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.

Installation

pip install simple-mockforce

or, with poetry

poetry add simple-mockforce

Usage

To patch calls to the Salesforce API and instead interact with the "virtual" Salesforce organization provided by this library, add the following:

import os

from simple_mockforce import mock_salesforce

from simple_salesforce import Salesforce


@mock_salesforce
def test_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)

    assert account["Name"] == "Test Account"

And that's about it!

Caveats

Case sensitivity

Unlike 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 endpoints

The following features are currently not supported:

  • the describe API
  • bulk queries
  • SOSL searches

Queries

SOQL is only partially supported as of now. Please refer to the README for python-soql-parser to 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 in ORDER BY clauses, i.e., always supply DESC or ASC
  • attributes of parent objects can be specified in the select clause (but not in the where clause)

Error handling

Error 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 blocked

When using @mock_salesforce, do note that the requests library is being patched with responses, so any calls you make to any other APIs will fail unless you patch them yourself, or patch the code which invokes said calls.

Relations

Relations 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., from Account__r it can be inferred that this is pointing to an Account object), you can create a file called relations.json that translates a relational field name to your intended Salesforce object's name. See relations.json in the test folder for an example.

To specify the location of relations.json, set an environment variable called MOCKFORCE_RELATIONS_ROOT which points to the parent folder of relations.json. Note, this defaults to the current directory ..

Metadata

Release files for simple-mockforce 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-mockforce 0.8.1
File Size Uploaded Provenance Actions
simple-mockforce-0.8.1.tar.gz 14.8 kB Without provenance Details for simple-mockforce-0.8.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-mockforce 0.8.1
File Interpreter ABI Platform Actions
simple_mockforce-0.8.1-py3-none-any.whl Python 3 none any Details for simple_mockforce-0.8.1-py3-none-any.whl

Total release size: 30.5 kB

Release files / simple-mockforce-0.8.1.tar.gz

Download URL simple-mockforce-0.8.1.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3500cd9c4aa0e2d169d6a5458a210e941dfd11461ba6200aca4d35963b61a4e5
BLAKE2b-256 checksum
How to use checksums
39df5296300a0f2e15a5bb02c1c47302b9ed410ff6e3672c21167589500be5d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.8.18 Linux/6.5.0-1018-azure

Release files / simple_mockforce-0.8.1-py3-none-any.whl

Download URL simple_mockforce-0.8.1-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c902439684a1367fd2ccc7f3d326d568ebe52dae6b566654130c66e06ae1c2ab
BLAKE2b-256 checksum
How to use checksums
e4a4a14c7ebbf122fe37e2fd75d79cd3b0e8d27d405ac2c131e3b560ea9e71ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.8.18 Linux/6.5.0-1018-azure

Security for simple-mockforce 0.8.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page