Skip to content

Instantly share code, notes, and snippets.

@mattrobenolt
Created July 15, 2012 21:01
Show Gist options
  • Save mattrobenolt/3118611 to your computer and use it in GitHub Desktop.
Save mattrobenolt/3118611 to your computer and use it in GitHub Desktop.
from __future__ import with_statement
import factory
from functools import wraps
class override_strategy(object):
def __init__(self, strategy):
self.strategy = strategy
def __call__(self, func):
@wraps(func)
def wrap(*args, **kwargs):
with self:
return func(*args, **kwargs)
return wrap
def __enter__(self):
self._original_strategy = factory.Factory.default_strategy
factory.Factory.default_strategy = self.strategy
def __exit__(self, exc_type, exc_value, traceback):
factory.Factory.default_strategy = self._original_strategy
factory.Factory.default_strategy = 'create'
with override_strategy('test'):
assert 'test' == factory.Factory.default_strategy
assert 'create' == factory.Factory.default_strategy
@override_strategy('test')
def foo():
assert 'test' == factory.Factory.default_strategy
foo()
assert 'create' == factory.Factory.default_strategy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment