mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Change use_cassette to pass a function to CassetteContextDecorator so that changes to the default settings on the vcr properly propogate.
This commit is contained in:
@@ -86,6 +86,32 @@ def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times(*arg
|
|||||||
decorated_function()
|
decorated_function()
|
||||||
|
|
||||||
|
|
||||||
|
def test_arg_getter_functionality():
|
||||||
|
arg_getter = mock.Mock(return_value=('test', {}))
|
||||||
|
context_decorator = Cassette.use_arg_getter(arg_getter)
|
||||||
|
|
||||||
|
with context_decorator as cassette:
|
||||||
|
assert cassette._path == 'test'
|
||||||
|
|
||||||
|
arg_getter.return_value = ('other', {})
|
||||||
|
|
||||||
|
with context_decorator as cassette:
|
||||||
|
assert cassette._path == 'other'
|
||||||
|
|
||||||
|
arg_getter.return_value = ('', {'filter_headers': ('header_name',)})
|
||||||
|
|
||||||
|
@context_decorator
|
||||||
|
def function():
|
||||||
|
pass
|
||||||
|
|
||||||
|
with mock.patch.object(Cassette, '__init__', return_value=None) as cassette_init, \
|
||||||
|
mock.patch.object(Cassette, '_load'), \
|
||||||
|
mock.patch.object(Cassette, '__exit__'):
|
||||||
|
function()
|
||||||
|
cassette_init.assert_called_once_with(arg_getter.return_value[0],
|
||||||
|
**arg_getter.return_value[1])
|
||||||
|
|
||||||
|
|
||||||
def test_cassette_not_all_played():
|
def test_cassette_not_all_played():
|
||||||
a = Cassette('test')
|
a = Cassette('test')
|
||||||
a.append('foo', 'bar')
|
a.append('foo', 'bar')
|
||||||
|
|||||||
@@ -3,14 +3,28 @@ import mock
|
|||||||
from vcr import VCR
|
from vcr import VCR
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_vcr_use_cassette():
|
def test_vcr_use_cassette():
|
||||||
filter_headers = mock.Mock()
|
filter_headers = mock.Mock()
|
||||||
test_vcr = VCR(filter_headers=filter_headers)
|
test_vcr = VCR(filter_headers=filter_headers)
|
||||||
with mock.patch('vcr.config.Cassette') as mock_cassette_class:
|
with mock.patch('vcr.cassette.Cassette.__init__', return_value=None) as mock_cassette_init, \
|
||||||
|
mock.patch('vcr.cassette.Cassette._load'), \
|
||||||
|
mock.patch('vcr.cassette.Cassette.__exit__'):
|
||||||
@test_vcr.use_cassette('test')
|
@test_vcr.use_cassette('test')
|
||||||
def function():
|
def function():
|
||||||
pass
|
pass
|
||||||
mock_cassette_class.call_count == 0
|
assert mock_cassette_init.call_count == 0
|
||||||
function()
|
function()
|
||||||
assert mock_cassette_class.use.call_args[1]['filter_headers'] is filter_headers
|
assert mock_cassette_init.call_args[1]['filter_headers'] is filter_headers
|
||||||
|
|
||||||
|
# Make sure that calls to function now use cassettes with the
|
||||||
|
# new filter_header_settings
|
||||||
|
test_vcr.filter_headers = ('a',)
|
||||||
|
function()
|
||||||
|
assert mock_cassette_init.call_args[1]['filter_headers'] == test_vcr.filter_headers
|
||||||
|
|
||||||
|
# Ensure that explicitly provided arguments still supercede
|
||||||
|
# those on the vcr.
|
||||||
|
new_filter_headers = mock.Mock()
|
||||||
|
|
||||||
|
with test_vcr.use_cassette('test', filter_headers=new_filter_headers) as cassette:
|
||||||
|
assert cassette._filter_headers == new_filter_headers
|
||||||
|
|||||||
@@ -24,13 +24,17 @@ class CassetteContextDecorator(object):
|
|||||||
from interfering with another.
|
from interfering with another.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, cls, *args, **kwargs):
|
@classmethod
|
||||||
self.args = args
|
def from_args(cls, cassette_class, path, **kwargs):
|
||||||
self.kwargs = kwargs
|
return cls(cassette_class, lambda: (path, kwargs))
|
||||||
|
|
||||||
|
def __init__(self, cls, args_getter):
|
||||||
self.cls = cls
|
self.cls = cls
|
||||||
|
self._args_getter = args_getter
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._cassette = self.cls.load(*self.args, **self.kwargs)
|
path, kwargs = self._args_getter()
|
||||||
|
self._cassette = self.cls.load(path, **kwargs)
|
||||||
return self._cassette.__enter__()
|
return self._cassette.__enter__()
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
@@ -55,10 +59,12 @@ class Cassette(object):
|
|||||||
return new_cassette
|
return new_cassette
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def use(cls, *args, **kwargs):
|
def use_arg_getter(cls, arg_getter):
|
||||||
return CassetteContextDecorator(cls, *args, **kwargs)
|
return CassetteContextDecorator(cls, arg_getter)
|
||||||
|
|
||||||
use_cassette = use
|
@classmethod
|
||||||
|
def use(cls, *args, **kwargs):
|
||||||
|
return CassetteContextDecorator.from_args(cls, *args, **kwargs)
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
path,
|
path,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import functools
|
||||||
import os
|
import os
|
||||||
from .cassette import Cassette
|
from .cassette import Cassette
|
||||||
from .serializers import yamlserializer, jsonserializer
|
from .serializers import yamlserializer, jsonserializer
|
||||||
@@ -74,13 +75,16 @@ class VCR(object):
|
|||||||
return matchers
|
return matchers
|
||||||
|
|
||||||
def use_cassette(self, path, **kwargs):
|
def use_cassette(self, path, **kwargs):
|
||||||
|
args_getter = functools.partial(self.get_path_and_merged_config, path, **kwargs)
|
||||||
|
return Cassette.use_arg_getter(args_getter)
|
||||||
|
|
||||||
|
def get_path_and_merged_config(self, path, **kwargs):
|
||||||
serializer_name = kwargs.get('serializer', self.serializer)
|
serializer_name = kwargs.get('serializer', self.serializer)
|
||||||
matcher_names = kwargs.get('match_on', self.match_on)
|
matcher_names = kwargs.get('match_on', self.match_on)
|
||||||
cassette_library_dir = kwargs.get(
|
cassette_library_dir = kwargs.get(
|
||||||
'cassette_library_dir',
|
'cassette_library_dir',
|
||||||
self.cassette_library_dir
|
self.cassette_library_dir
|
||||||
)
|
)
|
||||||
|
|
||||||
if cassette_library_dir:
|
if cassette_library_dir:
|
||||||
path = os.path.join(cassette_library_dir, path)
|
path = os.path.join(cassette_library_dir, path)
|
||||||
|
|
||||||
@@ -107,8 +111,7 @@ class VCR(object):
|
|||||||
'ignore_localhost', self.ignore_localhost
|
'ignore_localhost', self.ignore_localhost
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
return path, merged_config
|
||||||
return Cassette.use(path, **merged_config)
|
|
||||||
|
|
||||||
def register_serializer(self, name, serializer):
|
def register_serializer(self, name, serializer):
|
||||||
self.serializers[name] = serializer
|
self.serializers[name] = serializer
|
||||||
|
|||||||
Reference in New Issue
Block a user