Skip to content

Instantly share code, notes, and snippets.

@frnsys
Created March 6, 2014 20:02
Show Gist options
  • Save frnsys/9398211 to your computer and use it in GitHub Desktop.
Save frnsys/9398211 to your computer and use it in GitHub Desktop.
dynamic patcher
from unittest.mock import patch
class Patcher():
"""
This class provides a central place for managing
commonly used patches for tests.
Example::
p = Patcher([
'argos.core.brain.knowledge.knowledge_for',
'argos.core.brain.knowledge.uri_for_name'
])
This will look for the methods `faux_knowledge_for` and
`faux_uri_for_name` and patch those methods with these fake versions.
When you're done, simply call::
p.stop()
"""
def __init__(self, patch_names):
self.patchers = []
for patch_name in patch_names:
func_name = 'faux_{0}'.format(patch_name.split('.')[-1])
patcher = patch(patch_name)
mock_method = patcher.start()
func = globals().get(func_name) # kinda sketched out by this, but works ok
if func:
mock_method.side_effect = func
else:
raise Exception('No mock function has been defined for this name.')
self.patchers.append(patcher)
def stop(self):
for patcher in self.patchers:
patcher.stop()
# Patch these methods so
# tests don't require the Fuseki server
# or a network connection to Wikipedia.
def faux_knowledge_for(name=None, uri=None, fallback=None):
if uri:
return {
'summary': 'this is fake summary for uri {0}'.format(uri),
'image': 'http://www.argos.la/image.jpg'
}
if name:
return {
'summary': 'this is fake summary for name {0}'.format(name),
'image': 'http://www.argos.la/image.jpg'
}
return None
def faux_uri_for_name(name):
return "http://fauxpedia.org/resource/{0}".format(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment