Testing differen mock

This commit is contained in:
Alan Saul 2013-01-31 12:55:30 +00:00
parent c6339116a0
commit d5bafc9bb9

View file

@ -11,34 +11,108 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os, exceptions
import sys, os
#Mocking uninstalled modules: https://read-the-docs.readthedocs.org/en/latest/faq.html
##############################################################################
##
## Mock out imports with C dependencies because ReadTheDocs can't build them.
class Mock(object):
__all__ = []
def __init__(self, *args, **kwargs):
pass
for key, value in kwargs.iteritems():
setattr(self, key, value)
def __call__(self, *args, **kwargs):
return Mock()
@classmethod
def __getattr__(cls, name):
__add__ = __mul__ = __getitem__ = __setitem__ = \
__delitem__ = __sub__ = __floordiv__ = __mod__ = __divmod__ = \
__pow__ = __lshift__ = __rshift__ = __and__ = __xor__ = __or__ = \
__rmul__ = __rsub__ = __rfloordiv__ = __rmod__ = __rdivmod__ = \
__rpow__ = __rlshift__ = __rrshift__ = __rand__ = __rxor__ = __ror__ = \
__imul__ = __isub__ = __ifloordiv__ = __imod__ = __idivmod__ = \
__ipow__ = __ilshift__ = __irshift__ = __iand__ = __ixor__ = __ior__ = \
__neg__ = __pos__ = __abs__ = __invert__ = __call__
def __getattr__(self, name):
if name in ('__file__', '__path__'):
return '/dev/null'
elif name[0] == name[0].upper():
mockType = type(name, (), {})
mockType.__module__ = __name__
return mockType
if name == 'sqrt':
return math.sqrt
elif name[0] != '_' and name[0] == name[0].upper():
return type(name, (), {})
else:
return Mock()
return Mock(**vars(self))
MOCK_MODULES = ['matplotlib', 'matplotlib.color', 'matplotlib.pyplot', 'pylab' ]
def __lt__(self, *args, **kwargs):
return True
__nonzero__ = __le__ = __eq__ = __ne__ = __gt__ = __ge__ = __contains__ = \
__lt__
def __repr__(self):
# Use _mock_repr to fake the __repr__ call
res = getattr(self, "_mock_repr")
return res if isinstance(res, str) else "Mock"
def __hash__(self):
return 1
__len__ = __int__ = __long__ = __index__ = __hash__
def __oct__(self):
return '01'
def __hex__(self):
return '0x1'
def __float__(self):
return 0.1
def __complex__(self):
return 1j
MOCK_MODULES = [
'pylab', 'scipy', 'matplotlib', 'matplotlib.pyplot', 'pyfits',
'scipy.constants.constants', 'matplotlib.cm',
'matplotlib.image', 'matplotlib.colors', 'sunpy.cm',
'pandas', 'pandas.io', 'pandas.io.parsers',
'suds', 'matplotlib.ticker', 'matplotlib.colorbar',
'matplotlib.dates', 'scipy.optimize', 'scipy.ndimage',
'matplotlib.figure', 'scipy.ndimage.interpolation', 'bs4']
for mod_name in MOCK_MODULES:
sys.modules[mod_name] = Mock()
sys.modules[mod_name] = Mock(pi=math.pi, G=6.67364e-11)
sys.modules['numpy'] = Mock(pi=math.pi, G=6.67364e-11,
ndarray=type('ndarray', (), {}),
dtype=lambda _: Mock(_mock_repr='np.dtype(\'float32\')'))
sys.modules['scipy.constants'] = Mock(pi=math.pi, G=6.67364e-11)
##############################################################################
##
## Mock out imports with C dependencies because ReadTheDocs can't build them.
#class Mock(object):
#def __init__(self, *args, **kwargs):
#pass
#def __call__(self, *args, **kwargs):
#return Mock()
#@classmethod
#def __getattr__(cls, name):
#if name in ('__file__', '__path__'):
#return '/dev/null'
#elif name[0] == name[0].upper():
#mockType = type(name, (), {})
#mockType.__module__ = __name__
#return mockType
#else:
#return Mock()
#MOCK_MODULES = ['pylab']#'matplotlib', 'matplotlib.color', 'matplotlib.pyplot', 'pylab' ]
#for mod_name in MOCK_MODULES:
#sys.modules[mod_name] = Mock()
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the