Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

case.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import absolute_import
  2. import sys
  3. from functools import wraps
  4. from io import StringIO
  5. import mock
  6. from nose import SkipTest # noqa
  7. try:
  8. import unittest
  9. unittest.skip
  10. except AttributeError:
  11. import unittest2 as unittest # noqa
  12. PY3 = sys.version_info[0] == 3
  13. patch = mock.patch
  14. call = mock.call
  15. class Case(unittest.TestCase):
  16. def assertItemsEqual(self, a, b, *args, **kwargs):
  17. return self.assertEqual(sorted(a), sorted(b), *args, **kwargs)
  18. assertSameElements = assertItemsEqual
  19. class Mock(mock.Mock):
  20. def __init__(self, *args, **kwargs):
  21. attrs = kwargs.pop('attrs', None) or {}
  22. super(Mock, self).__init__(*args, **kwargs)
  23. for attr_name, attr_value in attrs.items():
  24. setattr(self, attr_name, attr_value)
  25. class _ContextMock(Mock):
  26. """Dummy class implementing __enter__ and __exit__
  27. as the with statement requires these to be implemented
  28. in the class, not just the instance."""
  29. def __enter__(self):
  30. pass
  31. def __exit__(self, *exc_info):
  32. pass
  33. def ContextMock(*args, **kwargs):
  34. obj = _ContextMock(*args, **kwargs)
  35. obj.attach_mock(Mock(), '__enter__')
  36. obj.attach_mock(Mock(), '__exit__')
  37. obj.__enter__.return_value = obj
  38. # if __exit__ return a value the exception is ignored,
  39. # so it must return None here.
  40. obj.__exit__.return_value = None
  41. return obj
  42. class MockPool(object):
  43. def __init__(self, value=None):
  44. self.value = value or ContextMock()
  45. def acquire(self, **kwargs):
  46. return self.value
  47. def redirect_stdouts(fun):
  48. @wraps(fun)
  49. def _inner(*args, **kwargs):
  50. sys.stdout = StringIO()
  51. sys.stderr = StringIO()
  52. try:
  53. return fun(*args, **dict(kwargs,
  54. stdout=sys.stdout, stderr=sys.stderr))
  55. finally:
  56. sys.stdout = sys.__stdout__
  57. sys.stderr = sys.__stderr__
  58. return _inner