Skip to content

Commit

Permalink
Fixed issue in test_nested_context_managers_with_session_created_befo…
Browse files Browse the repository at this point in the history
…re_first_nesting. by using a single class and patching cassette on that class. Not a great solution :\
  • Loading branch information
colonelpanic8 committed Sep 19, 2014
1 parent 58fcb2b commit 2bf23b2
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions vcr/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,22 @@ class PatcherBuilder(object):

def __init__(self, cassette):
self._cassette = cassette
self._class_to_cassette_subclass = {}

def build_patchers(self):
patcher_args = itertools.chain(self._httplib(), self._requests(), self._urllib3(),
self._httplib2(), self._boto())
for args in patcher_args:
patcher = self._build_patcher(*args)
for obj, patched_attribute, replacement_class in patcher_args:
patcher = self._build_patcher(obj, patched_attribute, replacement_class)
if patcher:
yield patcher
if hasattr(replacement_class, 'cassette'):
yield mock.patch.object(replacement_class, 'cassette', self._cassette)

def _build_patcher(self, obj, patched_attribute, replacement_class):
if not hasattr(obj, patched_attribute):
return

if isinstance(replacement_class, dict):
for key in replacement_class:
replacement_class[key] = self._get_cassette_subclass(replacement_class[key])
else:
replacement_class = self._get_cassette_subclass(replacement_class)
return mock.patch.object(obj, patched_attribute, replacement_class)

def _get_cassette_subclass(self, klass):
if klass.cassette is not None:
return klass
if klass not in self._class_to_cassette_subclass:
self._class_to_cassette_subclass[klass] = self._build_cassette_subclass(klass)
return self._class_to_cassette_subclass[klass]

def _build_cassette_subclass(self, base_class):
bases = (base_class,)
if not issubclass(base_class, object): # Check for old style class
bases += (object,)
return type('{0}{1}'.format(base_class.__name__, self._cassette._path),
bases, dict(cassette=self._cassette))

def _httplib(self):
yield httplib, 'HTTPConnection', VCRHTTPConnection
yield httplib, 'HTTPSConnection', VCRHTTPSConnection
Expand Down

0 comments on commit 2bf23b2

Please sign in to comment.