unittest.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 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. handler = logging.StreamHandler()
  21. handler.setFormatter(logging.Formatter(
  22. "%(levelname)s:%(name)s:%(message)s [%(pathname)s:%(lineno)d]"
  23. ))
  24. logging.getLogger().addHandler(handler)
  25. logging.getLogger().setLevel(NEVER)
  26. logging.getLogger("synapse.storage.SQL").setLevel(NEVER)
  27. logging.getLogger("synapse.storage.txn").setLevel(NEVER)
  28. def around(target):
  29. """A CLOS-style 'around' modifier, which wraps the original method of the
  30. given instance with another piece of code.
  31. @around(self)
  32. def method_name(orig, *args, **kwargs):
  33. return orig(*args, **kwargs)
  34. """
  35. def _around(code):
  36. name = code.__name__
  37. orig = getattr(target, name)
  38. def new(*args, **kwargs):
  39. return code(orig, *args, **kwargs)
  40. setattr(target, name, new)
  41. return _around
  42. class TestCase(unittest.TestCase):
  43. """A subclass of twisted.trial's TestCase which looks for 'loglevel'
  44. attributes on both itself and its individual test methods, to override the
  45. root logger's logging level while that test (case|method) runs."""
  46. def __init__(self, methodName, *args, **kwargs):
  47. super(TestCase, self).__init__(methodName, *args, **kwargs)
  48. method = getattr(self, methodName)
  49. level = getattr(method, "loglevel", getattr(self, "loglevel", NEVER))
  50. @around(self)
  51. def setUp(orig):
  52. old_level = logging.getLogger().level
  53. if old_level != level:
  54. @around(self)
  55. def tearDown(orig):
  56. ret = orig()
  57. logging.getLogger().setLevel(old_level)
  58. return ret
  59. logging.getLogger().setLevel(level)
  60. return orig()
  61. def assertObjectHasAttributes(self, attrs, obj):
  62. """Asserts that the given object has each of the attributes given, and
  63. that the value of each matches according to assertEquals."""
  64. for (key, value) in attrs.items():
  65. if not hasattr(obj, key):
  66. raise AssertionError("Expected obj to have a '.%s'" % key)
  67. try:
  68. self.assertEquals(attrs[key], getattr(obj, key))
  69. except AssertionError as e:
  70. raise (type(e))(e.message + " for '.%s'" % key)
  71. def DEBUG(target):
  72. """A decorator to set the .loglevel attribute to logging.DEBUG.
  73. Can apply to either a TestCase or an individual test method."""
  74. target.loglevel = logging.DEBUG
  75. return target