Skip to main content

No project description provided

Project description

A mechanism for running processes which might have side effects (e.g.: calling an external API etc.) in a way that is safe by default during testing and provides useful introspection functionality.

PyPI version

It will: Run a list of tasks. If possible, it will run them asyncronously

In TEST_MODE:

  • It will not run the code
  • It will store information about the calls made in

Usage

Install and configure

pip install dj-saferunner

Config options

Django settings

SAFERUNNER_USE_SENTRY - set to True to swallow errors and log to Sentry

In your code:

before

from myapp.tasks import ping_google
ping_google(url='http://foo.com')

after

from myapp.tasks import ping_google
from saferunner.process_runner import safely_run_processes
task_list = [
    # task, args, async
    (ping_google, {"url", "http://foo.com"}, False)
]
safely_run_processes(
    run_id='identifier giving some context about where this is run',
    task_list=task_list
)

In your tests

When in test mode, by default the saferunner will not execute your code. Instead it will log your calls using django's cache

example test

def test_run_some_tasks(self):
    clear_run_cache()
    run_id = 'this_is_a_test'
    task_list = [
        (hello, {"msg": "hi"} , False),
        (do_something_slowly, {"sleep": 10}, True),
    ]
    safely_run_processes(
        run_id = run_id,
        task_list = task_list
    )
    res = get_process_run(run_id)

The results of get_process_run would look something like this:

[{
	'task_name': 'hello',
	'task_args': "OrderedDict([('msg', 'hi')])",
	'is_async': False
}, {
	'task_name': 'do_something_slowly',
	'task_args': "OrderedDict([('sleep', 10)])",
	'is_async': True
}]

Explicitly running side effect code

In your tests, You can force saferunner to execute safely run code.

You might want to do this, for example, if you want to test code inside your function from a higher level. Ideally you should not really need to do this too often. We would recommend that it is preferred to test your functions by calling them directly (as opposed to checking for side effects in a higher level function)

Run all side effect code:

Will essentially turn saferunner off for this test

@override_settings(FORCE_RUN_SIDE_EFFECTS='__all__')

You can also specify a specific task to pass through:

This will force saferunner to explicitly run this exact function/method

@override_settings(FORCE_RUN_SIDE_EFFECTS=task.name)
@override_settings(FORCE_RUN_SIDE_EFFECTS=task.__name__)

FAQ:

Q: Why not just use mocks? A: The use-case for this is for code that might get executed often (for example code in signals), and which might have unwanted side-effects if accidentally run durung tests (e.g.: send SMSes or making API calls). We want to err on the side of caution here and mock by default during testing and be explicit about when we want to run this code

PS: DHH would probably call this test induced damage!

TODO

  • Make it work Class methods

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

dj-saferunner-0.0.2.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file dj-saferunner-0.0.2.tar.gz.

File metadata

  • Download URL: dj-saferunner-0.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for dj-saferunner-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f2fddae12798efc26333dce8615a7e834fd368e52494e6a9d8af4e7019e91fe9
MD5 a895b30adffa42d18f29629e3a901564
BLAKE2b-256 904a2fa25c4b15c308ad4dbf141ba2d5e32d807c1934b924705613ff98ea1d1b

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