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 instance, 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 instance 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"]
    )

    salesforce.Account.create({"Name": "Test Account"})

To reset state, you can call create_new_virtual_instance, ensuring there's no pollution between tests

from simple_mockforce.virtual import virtual_salesforce


@mock_salesforce
def test_api_again():
    # This will wipe away the account created in the above step
    virtual_salesforce.create_new_virtual_instance()

And that's about it!

Caveats

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 READ ME 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.

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 instance does not yet enforce any of the server-side validation when working with a real API.

This means that the virtual instance is much more permissive and loose than a real Salesforce instance would be.

There are plans to read the XML consumed by the meta API in order to enforce more rigidity inside the virtual instance, 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.

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.1.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

simple_mockforce-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple-mockforce-0.1.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Windows/10

File hashes

Hashes for simple-mockforce-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a8eb4f86db4e706654dfa276589874248b63345091051a7aa9d23d2ea5d033ab
MD5 f115681b15428028579ad0a92bc14d49
BLAKE2b-256 77d1a2750afd447c9a30af84a4d2badf6a809ce6b648aa8a8b69a1814730724c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for simple_mockforce-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 276c8f25d08668792deb28400c3d48ea7e9e700516dd37bb89175ac6f0244c97
MD5 a964e32dd132c81fe99d89ca146fd104
BLAKE2b-256 48e4f5de9120a43f4e03282abd4871d9f9d7a18e0fc52cc5c3114169b1488e9b

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