def test_info_with_kwargs(self, logger): dct = {'a': 1, 'b': 'xyz', 'c': ['world', 3]} logger.info('Hello', kwargs=dct) logged = test_util.get_logged(n_tails=1) assert re.search( "KeyParams: .*(\{'a': 1, 'b': 'xyz', 'c': \['world', 3\]\})", logged[0])
def test_info(self, logger): logger.info('Hello') logged = test_util.get_logged(n_tails=1) pattern = pattern_normal % ('INFO', 'Hello') assert re.search("^" + pattern_time, logged[0]) assert re.search(pattern, logged[0]) assert not re.search(r"Params: ", logged[0]) assert not re.search(r"KeyParams: ", logged[0])
def test_critical(self, logger): try: 1 / 0 except Exception as e: logger.critical('Hello', (), locals(), e, traceback.format_exc()) logged = test_util.get_logged(n_tails=1) pattern_extra = r"/ KeyParams: .+ / Caught Exception: .+ ZeroDivisionError: division by zero\s*" pattern = pattern_except % ('CRITICAL', 'Hello', pattern_extra) assert re.search(pattern, logged[0])
def test_decorator(self): @decorator.trace @decorator.excep def run_test(a=1, b=3, x='x', z='z'): 1 / 0 run_test() logged = test_util.get_logged(n_tails=3) assert re.search(r"INFO .+ Start ", logged[0]) assert re.search(r"ERROR .+ ErrorOccured", logged[1]) assert re.search(r"INFO .+ End ", logged[2])
def test_info_with_params(self, logger): tpl = (1, 2, 3) dct = {'a': 1, 'b': 'xyz', 'c': ['world', 3]} logger.info('Hello', tpl, dct) logged = test_util.get_logged(n_tails=1) assert re.search("Params: .*(\(1, 2, 3\))", logged[0]) assert re.search( "KeyParams: .*(\{'a': 1, 'b': 'xyz', 'c': \['world', 3\]\})", logged[0]) assert re.search("Params: .*KeyParams: ", logged[0])
def test_type_warn(self): @deco.trace @deco.excep() @deco.excep(type_exc=ZeroDivisionError, warn=True) def run_test(a=1, b=3, x='x', z='z'): 1 / 0 run_test() logged = test_util.get_logged(n_tails=3) assert re.search(r"INFO .+ Start ", logged[0]) assert re.search(r"WARNING .+ Warned.+ZeroDivisionError.+", logged[1]) assert re.search(r"INFO .+ End ", logged[2])
def test_type_exc2(self): @deco.trace @deco.excep(type_exc=Exception) @deco.excep(type_exc=IndexError) def run_test(a=1, b=3, x='x', z='z'): 1 / 0 run_test() logged = test_util.get_logged(n_tails=3) assert re.search(r"INFO .+ Start ", logged[0]) assert re.search(r"ERROR .+ ErrorOccured.+ZeroDivisionError.+", logged[1]) assert re.search(r"INFO .+ End ", logged[2])
def test_type_exc(self): @deco.trace @deco.excep() @deco.excep(type_exc=ZeroDivisionError, with_raise=True) def run_test(a=1, b=3, x='x', z='z'): 1 / 0 run_test() logged = test_util.get_logged(n_tails=4) assert re.search(r"INFO .+ Start ", logged[0]) assert re.search(r"ERROR .+ ErrorOccured.+ZeroDivisionError.+", logged[1]) # excep(type_exc=*) assert re.search(r"ERROR .+ ErrorOccured.+ZeroDivisionError.+", logged[2]) # excep() assert re.search(r"INFO .+ End ", logged[3])
def test_warn(self, logger): tpl = (1, 2, 3) lst = list(tpl) dct = {'a': 1, 'b': 'xyz', 'c': ['world', 3]} try: 1 / 0 except Exception as e: logger.warn('Hello', (), locals(), e, traceback.format_exc()) logged = test_util.get_logged(n_tails=1) pattern_extra = r"/ KeyParams: .+ / Caught Exception: .+ ZeroDivisionError: division by zero\s*" pattern = pattern_except % ('WARNING', 'Hello', pattern_extra) assert re.search(pattern, logged[0]) assert re.search(r"'dct': \{'a': 1, 'b': 'xyz', 'c': \['world', 3\]\}", logged[0]) assert re.search(r"'lst': \[1, 2, 3\]", logged[0]) assert re.search(r"'tpl': \(1, 2, 3\)", logged[0])
def test_info_with_args(self, logger): tpl = (1, 2, 3) logger.info('Hello', args=tpl) logged = test_util.get_logged(n_tails=1) assert re.search("Params: .*(\(1, 2, 3\))", logged[0])
def test_debug(self, logger): logger.debug('Hello') logged = test_util.get_logged(n_tails=1) pattern = pattern_normal % ('DEBUG', 'Hello') assert re.search(pattern, logged[0])