mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 17:15:35 +00:00
Revert "Add global toggle to use_cassette."
This reverts commit 366e2b75bb.
Conflicts:
tests/unit/test_cassettes.py
This commit is contained in:
@@ -1,12 +1,8 @@
|
|||||||
from six.moves import http_client as httplib
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
from vcr.cassette import Cassette
|
from vcr.cassette import Cassette
|
||||||
from vcr.errors import UnhandledHTTPRequestError
|
from vcr.errors import UnhandledHTTPRequestError
|
||||||
from vcr import global_toggle
|
|
||||||
|
|
||||||
|
|
||||||
def test_cassette_load(tmpdir):
|
def test_cassette_load(tmpdir):
|
||||||
@@ -73,11 +69,11 @@ def test_cassette_cant_read_same_request_twice():
|
|||||||
|
|
||||||
|
|
||||||
@mock.patch('vcr.cassette.requests_match', return_value=True)
|
@mock.patch('vcr.cassette.requests_match', return_value=True)
|
||||||
@mock.patch('vcr.cassette.load_cassette',
|
@mock.patch('vcr.cassette.load_cassette', lambda *args, **kwargs: (('foo',), (mock.MagicMock(),)))
|
||||||
lambda *args, **kwargs: (('foo',), (mock.MagicMock(),)))
|
|
||||||
@mock.patch('vcr.cassette.Cassette.can_play_response_for', return_value=True)
|
@mock.patch('vcr.cassette.Cassette.can_play_response_for', return_value=True)
|
||||||
@mock.patch('vcr.stubs.VCRHTTPResponse')
|
@mock.patch('vcr.stubs.VCRHTTPResponse')
|
||||||
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*args):
|
def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*args):
|
||||||
|
from six.moves import http_client as httplib
|
||||||
@Cassette.use_cassette('test')
|
@Cassette.use_cassette('test')
|
||||||
def decorated_function():
|
def decorated_function():
|
||||||
conn = httplib.HTTPConnection("www.python.org")
|
conn = httplib.HTTPConnection("www.python.org")
|
||||||
@@ -101,24 +97,6 @@ def test_cassette_all_played():
|
|||||||
a.play_response('foo')
|
a.play_response('foo')
|
||||||
assert a.all_played
|
assert a.all_played
|
||||||
|
|
||||||
@mock.patch('vcr.cassette.install')
|
|
||||||
@mock.patch('vcr.cassette.reset')
|
|
||||||
def test_global_toggle(mock_reset, mock_install):
|
|
||||||
@Cassette.use_cassette('test')
|
|
||||||
def function():
|
|
||||||
pass
|
|
||||||
|
|
||||||
global_toggle(enabled=False)
|
|
||||||
|
|
||||||
function()
|
|
||||||
assert mock_install.call_count == 0
|
|
||||||
assert mock_reset.call_count == 0
|
|
||||||
|
|
||||||
global_toggle(enabled=True)
|
|
||||||
function()
|
|
||||||
mock_install.assert_called_once_with(mock.ANY)
|
|
||||||
mock_reset.assert_called_once_with()
|
|
||||||
|
|
||||||
|
|
||||||
def test_before_record_response():
|
def test_before_record_response():
|
||||||
before_record_response = mock.Mock(return_value='mutated')
|
before_record_response = mock.Mock(return_value='mutated')
|
||||||
@@ -127,4 +105,3 @@ def test_before_record_response():
|
|||||||
|
|
||||||
before_record_response.assert_called_once_with('res')
|
before_record_response.assert_called_once_with('res')
|
||||||
assert cassette.responses[0] == 'mutated'
|
assert cassette.responses[0] == 'mutated'
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from .config import VCR
|
from .config import VCR
|
||||||
from . import cassette
|
|
||||||
|
|
||||||
# Set default logging handler to avoid "No handler found" warnings.
|
# Set default logging handler to avoid "No handler found" warnings.
|
||||||
try: # Python 2.7+
|
try: # Python 2.7+
|
||||||
@@ -10,9 +9,6 @@ except ImportError:
|
|||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def global_toggle(enabled=True):
|
|
||||||
cassette.use_cassette._enabled = enabled
|
|
||||||
|
|
||||||
|
|
||||||
logging.getLogger(__name__).addHandler(NullHandler())
|
logging.getLogger(__name__).addHandler(NullHandler())
|
||||||
|
|
||||||
|
|||||||
@@ -14,33 +14,15 @@ from .matchers import requests_match, uri, method
|
|||||||
from .errors import UnhandledHTTPRequestError
|
from .errors import UnhandledHTTPRequestError
|
||||||
|
|
||||||
|
|
||||||
class NullContextDecorator(object):
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __enter__(self, *args):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, *args):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __call__(self, function):
|
|
||||||
return function
|
|
||||||
|
|
||||||
|
|
||||||
class use_cassette(object):
|
class use_cassette(object):
|
||||||
|
|
||||||
_enabled = True
|
|
||||||
|
|
||||||
def __init__(self, cls, *args, **kwargs):
|
def __init__(self, cls, *args, **kwargs):
|
||||||
self.args = args
|
self.args = args
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.cls = cls
|
self.cls = cls
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._cassette = self.cls.load(*self.args, **self.kwargs) if self._enabled \
|
self._cassette = self.cls.load(*self.args, **self.kwargs)
|
||||||
else NullContextDecorator()
|
|
||||||
return self._cassette.__enter__()
|
return self._cassette.__enter__()
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
|
|||||||
Reference in New Issue
Block a user