1
0

unittest.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. 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", getattr(self, "loglevel", NEVER))
  44. @around(self)
  45. def setUp(orig):
  46. old_level = logging.getLogger().level
  47. if old_level != level:
  48. @around(self)
  49. def tearDown(orig):
  50. ret = orig()
  51. logging.getLogger().setLevel(old_level)
  52. return ret
  53. logging.getLogger().setLevel(level)
  54. # Don't set SQL logging
  55. logging.getLogger("synapse.storage").setLevel(old_level)
  56. return orig()
  57. def assertObjectHasAttributes(self, attrs, obj):
  58. """Asserts that the given object has each of the attributes given, and
  59. that the value of each matches according to assertEquals."""
  60. for (key, value) in attrs.items():
  61. if not hasattr(obj, key):
  62. raise AssertionError("Expected obj to have a '.%s'" % key)
  63. try:
  64. self.assertEquals(attrs[key], getattr(obj, key))
  65. except AssertionError as e:
  66. raise (type(e))(e.message + " for '.%s'" % key)
  67. def DEBUG(target):
  68. """A decorator to set the .loglevel attribute to logging.DEBUG.
  69. Can apply to either a TestCase or an individual test method."""
  70. target.loglevel = logging.DEBUG
  71. return target