Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

decorators.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import mock
  2. def dataProvider(fn_data_provider):
  3. """
  4. Data provider decorator, allows another callable to provide the data for
  5. the test. This is a nice feature from PHPUnit which is very useful. Am
  6. sticking with the JUnit style naming as unittest does this already.
  7. Implementation based on:
  8. http://melp.nl/2011/02/phpunit-style-dataprovider-in-python-unit-test/#more-525
  9. """
  10. def test_decorator(test_method):
  11. def execute_test_method_with_each_data_set(self):
  12. for data in fn_data_provider():
  13. if (len(data) == 2 and isinstance(data[0], tuple) and
  14. isinstance(data[1], dict)):
  15. # Both args and kwargs being provided
  16. args, kwargs = data[:]
  17. else:
  18. args, kwargs = data, {}
  19. try:
  20. test_method(self, *args, **kwargs)
  21. except AssertionError, e:
  22. self.fail("%s (Provided data: %s, %s)" % (e, args, kwargs))
  23. return execute_test_method_with_each_data_set
  24. return test_decorator
  25. # This will be in Oscar 0.6 - it should be functools though!
  26. def compose(*functions):
  27. """
  28. Compose functions
  29. This is useful for combining decorators.
  30. """
  31. def _composed(*args):
  32. for fn in functions:
  33. try:
  34. args = fn(*args)
  35. except TypeError:
  36. # args must be scalar so we don't try to expand it
  37. args = fn(args)
  38. return args
  39. return _composed
  40. no_database = mock.patch(
  41. 'django.db.backends.util.CursorWrapper', mock.Mock(
  42. side_effect=RuntimeError("Using the database is not permitted!")))
  43. no_filesystem = mock.patch('__builtin__.open', mock.Mock(
  44. side_effect=RuntimeError("Using the filesystem is not permitted!")))
  45. no_sockets = mock.patch('socket.getaddrinfo', mock.Mock(
  46. side_effect=RuntimeError("Using sockets is not permitted!")))
  47. no_externals = no_diggity = compose(
  48. no_database, no_filesystem, no_sockets) # = no doubt