Created
July 15, 2012 21:01
-
-
Save mattrobenolt/3118611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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