Mercurial > libervia-backend
changeset 1909:0681d69cbe0a
test: add helpers methods muteLogging and unmuteLogging
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 11 Mar 2016 16:41:44 +0100 (2016-03-11) |
parents | ffcd7904b655 |
children | 55440ee00905 |
files | src/test/helpers.py src/test/test_memory.py src/test/test_plugin_misc_radiocol.py src/test/test_plugin_misc_room_game.py src/test/test_plugin_xep_0033.py |
diffstat | 5 files changed, 34 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/src/test/helpers.py Fri Mar 11 16:38:23 2016 +0100 +++ b/src/test/helpers.py Fri Mar 11 16:41:44 2016 +0100 @@ -22,7 +22,12 @@ from sat.core import log_config log_config.satConfigure() +import logging +from sat.core.log import getLogger +getLogger().setLevel(logging.WARNING) # put this to DEBUG when needed + from sat.core import exceptions +from sat.tools import config as tools_config from constants import Const as C from wokkel.xmppim import RosterItem from wokkel.generic import parseXml @@ -47,6 +52,19 @@ return u"True" if value else u"False" +def muteLogging(): + """Temporarily set the logging level to CRITICAL to not pollute the output with expected errors.""" + logger = getLogger() + logger.original_level = logger.getEffectiveLevel() + logger.setLevel(logging.CRITICAL) + + +def unmuteLogging(): + """Restore the logging level after it has been temporarily disabled.""" + logger = getLogger() + logger.setLevel(logger.original_level) + + class DifferentArgsException(FailTest): pass @@ -340,7 +358,7 @@ SatRosterProtocol.__init__(self, host) self.parent = parent self._jids = {} - self.addItem(parent.jid) + self.addItem(parent.jid.userhostJID()) def addItem(self, jid, *args, **kwargs): if not args and not kwargs:
--- a/src/test/test_memory.py Fri Mar 11 16:38:23 2016 +0100 +++ b/src/test/test_memory.py Fri Mar 11 16:41:44 2016 +0100 @@ -21,8 +21,6 @@ from sat.test import helpers from twisted.trial import unittest import traceback -from sat.core.log import getLogger -from logging import INFO from constants import Const from xml.dom import minidom @@ -205,11 +203,9 @@ @param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure @param app: name of the frontend registering the parameters """ - logger = getLogger() - level = logger.getEffectiveLevel() - logger.setLevel(INFO) + helpers.muteLogging() self.host.memory.paramsRegisterApp(xml, security_limit, app) - logger.setLevel(level) + helpers.unmuteLogging() # tests with no security level on the parameter (most secure) params = self._getParamXML()
--- a/src/test/test_plugin_misc_radiocol.py Fri Mar 11 16:38:23 2016 +0100 +++ b/src/test/test_plugin_misc_radiocol.py Fri Mar 11 16:41:44 2016 +0100 @@ -21,7 +21,6 @@ """ Tests for the plugin radiocol """ from sat.core import exceptions -from sat.core.log import getLogger from sat.test import helpers, helpers_plugins from sat.plugins import plugin_misc_radiocol as plugin from sat.plugins import plugin_misc_room_game as plugin_room_game
--- a/src/test/test_plugin_misc_room_game.py Fri Mar 11 16:38:23 2016 +0100 +++ b/src/test/test_plugin_misc_room_game.py Fri Mar 11 16:41:44 2016 +0100 @@ -21,7 +21,6 @@ """ Tests for the plugin room game (base class for MUC games) """ from sat.core.i18n import _ -from sat.core.log import getLogger from constants import Const from sat.test import helpers, helpers_plugins from sat.plugins import plugin_misc_room_game as plugin @@ -430,7 +429,9 @@ def test__checkCreateGameAndInit(self): self.reinit() - self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE)) # print internal error + helpers.muteLogging() + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE)) + helpers.unmuteLogging() nick = self.plugin_0045.joinRoom(0, 0) self.assertEqual((True, False), self.plugin._checkCreateGameAndInit(ROOM_JID, PROFILE)) @@ -438,7 +439,9 @@ self.assertFalse(self.plugin._gameExists(ROOM_JID, True)) self.assertTrue(self.plugin.isReferee(ROOM_JID, nick)) - self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, OTHER_PROFILE)) # print internal error + helpers.muteLogging() + self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, OTHER_PROFILE)) + helpers.unmuteLogging() self.plugin_0045.joinRoom(0, 1) self.assertEqual((False, False), self.plugin._checkCreateGameAndInit(ROOM_JID, OTHER_PROFILE))
--- a/src/test/test_plugin_xep_0033.py Fri Mar 11 16:38:23 2016 +0100 +++ b/src/test/test_plugin_xep_0033.py Fri Mar 11 16:41:44 2016 +0100 @@ -24,7 +24,6 @@ from sat.test import helpers from sat.plugins import plugin_xep_0033 as plugin from sat.core.exceptions import CancelError -from sat.core.log import getLogger from twisted.internet import defer from wokkel.generic import parseXml from twisted.words.protocols.jabber.jid import JID @@ -108,7 +107,7 @@ stored = [] d_list = [] - def cb(entities, to_jid, logger, level): + def cb(entities, to_jid): if host in entities: if host not in sent: # send the message to the entity offering the feature sent.append(host) @@ -117,16 +116,14 @@ else: # feature not supported, use normal behavior sent.append(to_jid) stored.append(to_jid) - logger.setLevel(level) + helpers.unmuteLogging() for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC): to_jid = JID(to_s) host = JID(to_jid.host) - logger = getLogger() - level = logger.getEffectiveLevel() - logger.setLevel(ERROR) # remove log.warning pollution - d = self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE) - d.addCallback(cb, to_jid, logger, level) + helpers.muteLogging() + d = self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile=PROFILE) + d.addCallback(cb, to_jid) d_list.append(d) def cb_list(dummy): @@ -144,14 +141,12 @@ addressing information to the stanza. @param data: the data to be processed by self.plugin.sendMessageTrigger """ - logger = getLogger() - level = logger.getEffectiveLevel() - logger.setLevel(ERROR) # remove log.warning pollution pre_treatments = defer.Deferred() post_treatments = defer.Deferred() + helpers.muteLogging() self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE) post_treatments.callback(data) - logger.setLevel(level) + helpers.unmuteLogging() post_treatments.addCallbacks(self._assertAddresses, lambda failure: failure.trap(CancelError)) return post_treatments