Skip to main content

A companion package for simple-salesforce that enables the testing of code that interacts with Salesforce's API

Project description

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():
    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 ..

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple-mockforce-0.3.2.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

simple_mockforce-0.3.2-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file simple-mockforce-0.3.2.tar.gz.

File metadata

  • Download URL: simple-mockforce-0.3.2.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.12 Linux/5.4.0-1074-azure

File hashes

Hashes for simple-mockforce-0.3.2.tar.gz
Algorithm Hash digest
SHA256 c1abd2dd35761fc8846d140950f7a14410b176e303b2a16ad01503aab7fbcf9f
MD5 4f8803e0c5b3bf48ea2a696d5447ae66
BLAKE2b-256 4e7f6f5293ccf2084f7da3c6d617b48b00092fe8e740a956f4c3b9d2dd47cf55

See more details on using hashes here.

File details

Details for the file simple_mockforce-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: simple_mockforce-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.12 Linux/5.4.0-1074-azure

File hashes

Hashes for simple_mockforce-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53ee2d0aea83c88002192cb61052839a8092838461ac13029a378a39e2bc7a16
MD5 f92ec0d3fc342eae1037bab2fb62ea25
BLAKE2b-256 2b292794a09cf8f2a255decc1e6845fe67c5304b6ecf05a05349606fc3c9d7ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page