-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:weargoggles/django-coreapi
- Loading branch information
Showing
3 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import functools | ||
import coreapi | ||
import django_coreapi.client | ||
|
||
|
||
_responses = [] | ||
|
||
|
||
def get_match(keys): | ||
""" | ||
Find the first matching response in the current set | ||
:param keys: the key path to be matched | ||
:return: the matching response or None | ||
""" | ||
for response in _responses: | ||
if response[0] == keys: | ||
return response[1] | ||
|
||
|
||
class Mock(object): | ||
""" | ||
Can be used as a context manager. Takes handler functions as arguments, which are evaluated in order in place of | ||
""" | ||
def __init__(self): | ||
super(Mock, self).__init__() | ||
|
||
def __enter__(self): | ||
# prepare and inject the mock methods to coreapi's session | ||
self._real_action = coreapi.Client.action | ||
self._real_django_client_action = django_coreapi.client.DjangoCoreAPIClient.action | ||
|
||
def fake_action(client, document, keys, *args, **kwargs): | ||
res = get_match(keys) | ||
if res is not None: | ||
return res | ||
raise | ||
|
||
coreapi.Client.action = fake_action | ||
django_coreapi.client.DjangoCoreAPIClient.action = fake_action | ||
|
||
def __exit__(self, exc_type, value, tb): | ||
global _responses | ||
# replace the real methods | ||
coreapi.Client.action = self._real_action | ||
django_coreapi.client.DjangoCoreAPIClient.action = self._real_django_client_action | ||
# clear out the match list | ||
_responses = [] | ||
|
||
|
||
def activate(f): | ||
""" | ||
A decorator which mocks the coreapi and django_coreapi clients, allowing use of `add` | ||
:param f: the function to be wrapped | ||
:return: the wrapped function | ||
""" | ||
@functools.wraps(f) | ||
def decorated(*args, **kwargs): | ||
with Mock(): | ||
return f(*args, **kwargs) | ||
|
||
return decorated | ||
|
||
|
||
def add(keys, response): | ||
global _responses | ||
_responses.append((keys, response)) |
This file contains 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