1
0

unittest.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.trial import unittest
  16. import logging
  17. # logging doesn't have a "don't log anything at all EVARRRR setting,
  18. # but since the highest value is 50, 1000000 should do ;)
  19. NEVER = 1000000
  20. logging.getLogger().addHandler(logging.StreamHandler())
  21. logging.getLogger().setLevel(NEVER)
  22. def around(target):
  23. """A CLOS-style 'around' modifier, which wraps the original method of the
  24. given instance with another piece of code.
  25. @around(self)
  26. def method_name(orig, *args, **kwargs):
  27. return orig(*args, **kwargs)
  28. """
  29. def _around(code):
  30. name = code.__name__
  31. orig = getattr(target, name)
  32. def new(*args, **kwargs):
  33. return code(orig, *args, **kwargs)
  34. setattr(target, name, new)
  35. return _around
  36. class TestCase(unittest.TestCase):
  37. """A subclass of twisted.trial's TestCase which looks for 'loglevel'
  38. attributes on both itself and its individual test methods, to override the
  39. root logger's logging level while that test (case|method) runs."""
  40. def __init__(self, methodName, *args, **kwargs):
  41. super(TestCase, self).__init__(methodName, *args, **kwargs)
  42. method = getattr(self, methodName)
  43. level = getattr(method, "loglevel",
  44. getattr(self, "loglevel",
  45. NEVER))
  46. @around(self)
  47. def setUp(orig):
  48. old_level = logging.getLogger().level
  49. if old_level != level:
  50. @around(self)
  51. def tearDown(orig):
  52. ret = orig()
  53. logging.getLogger().setLevel(old_level)
  54. return ret
  55. logging.getLogger().setLevel(level)
  56. # Don't set SQL logging
  57. logging.getLogger("synapse.storage").setLevel(old_level)
  58. return orig()
  59. def assertObjectHasAttributes(self, attrs, obj):
  60. """Asserts that the given object has each of the attributes given, and
  61. that the value of each matches according to assertEquals."""
  62. for (key, value) in attrs.items():
  63. if not hasattr(obj, key):
  64. raise AssertionError("Expected obj to have a '.%s'" % key)
  65. try:
  66. self.assertEquals(attrs[key], getattr(obj, key))
  67. except AssertionError as e:
  68. raise (type(e))(e.message + " for '.%s'" % key)
  69. def DEBUG(target):
  70. """A decorator to set the .loglevel attribute to logging.DEBUG.
  71. Can apply to either a TestCase or an individual test method."""
  72. target.loglevel = logging.DEBUG
  73. return target