Skip to main content

Isolate state on the Braintree sandbox during testing.

Project description

https://travis-ci.org/venmo/btnamespace.svg?branch=master

A Braintree namespace isolates state on the Braintree gateway:

import braintree
import btnamespace

with btnamespace.Namespace():
    customer = braintree.Customer.create({"id": "123"})
    assert customer.id == "123"
    braintree.Customer.find("123")  # success

braintree.Customer.find("123")  # NotFound exception

This is primarily useful during integration tests:

def setUp(self):
    self.namespace = btnamespace.Namespace()
    self.namespace.__enter__()

def test_some_sandbox_integration(self):
    #...

def tearDown(self):
    self.namespace.__exit__()

Compared to calling eg braintree.Customer.delete during tearDown, this has a number of advantages:

  • it’s faster, since no teardown is needed

  • it’s simpler, since it doesn’t require any bookkeeping

  • it’s robust, since tests can be written without any state assumptions

You can install it with $ pip install btnamespace.

What’s supported

  • Customer create, update, find, delete

  • CreditCard create, update, find, delete

  • Transaction create, find

All operations involving subresources - eg creating a CreditCard and Customer in one call - work as expected.

Adding support for other operations is easy; we just haven’t needed them yet. Contributions welcome!

How it Works

Under the hood, a Namespace globally patches the braintree client library.

During create operations, any provided ids are removed. This forces the gateway to respond with unique ids, which are later mapped back to the originally-provided ids. Here’s an example:

  • on a call to braintree.Customer.create({'id': '123', ...}), '123' is stored as a Customer id and the call becomes braintree.Customer.create({...}).

  • then, the server returns a unique id 'abcde' for the Customer. '123' is mapped to 'abcde', and the resulting Customer object’s id is set to '123'.

  • later, a call to braintree.Customer.find('123') becomes braintree.Customer.find('abcde').

Contributing

Inside your vitualenv:

$ cd btnamespace
$ pip install -e .
$ pip install -r requirements.txt

To run the tests, first add your sandbox credentials:

$ export BT_MERCHANT_ID=merchant-id
$ export BT_PUBLIC_KEY=public-id
$ export BT_PRIVATE_KEY=private-key

Then run $ python tests/test_integration.py.

History

1.0.0

released 2014-04-07

  • initial release

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

btnamespace-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

File details

Details for the file btnamespace-1.0.0.tar.gz.

File metadata

File hashes

Hashes for btnamespace-1.0.0.tar.gz
Algorithm Hash digest
SHA256 35143b4d352df7645410b3d4c8450fb00c9201fe1938e6082fb00fab5760b1e2
MD5 6ef3eaa73fe78fa4cda3301e8c062cb7
BLAKE2b-256 8088deb723dbff08dde220b6b46dbe6eb29c8f3e0c9299c3ec24bac08f5343e3

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