Skip to content

Commit b719df3

Browse files
committed
Unit tests and bugs
1 parent 89dbc86 commit b719df3

File tree

4 files changed

+198
-20
lines changed

4 files changed

+198
-20
lines changed

logwrap/_alogwrap.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,7 @@ def wrapper(*args, **kwargs):
8181
except BaseException as e:
8282
if isinstance(e, tuple(self.blacklisted_exceptions)):
8383
raise
84-
arguments = args_repr if self.log_call_args_on_exc else ''
85-
self._logger.log(
86-
level=self.exc_level,
87-
msg="Failed: \n{name!r}({arguments})".format(
88-
name=func.__name__,
89-
arguments=arguments,
90-
),
91-
exc_info=True
92-
)
84+
self._make_exc_record(name=func.__name__, arguments=args_repr)
9385
raise
9486
return result
9587

logwrap/_log_wrap.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,7 @@ def wrapper(*args, **kwargs):
8888
except BaseException as e:
8989
if isinstance(e, tuple(self.blacklisted_exceptions)):
9090
raise
91-
arguments = args_repr if self.log_call_args_on_exc else ''
92-
self._logger.log(
93-
level=self.exc_level,
94-
msg="Failed: \n{name!r}({arguments})".format(
95-
name=func.__name__,
96-
arguments=arguments,
97-
),
98-
exc_info=True
99-
)
91+
self._make_exc_record(name=func.__name__, arguments=args_repr)
10092
raise
10193
return result
10294

logwrap/_log_wrap_shared.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def max_indent(self):
200200
@_check_type(int)
201201
def max_indent(self, val):
202202
"""Maximum indentation."""
203-
self.__exc_level = val
203+
self.__max_indent = val
204204

205205
@property
206206
def blacklisted_names(self):
@@ -240,7 +240,7 @@ def log_result_obj(self):
240240
return self.__log_result_obj
241241

242242
@log_result_obj.setter
243-
@_check_type
243+
@_check_type(bool)
244244
def log_result_obj(self, val):
245245
"""Flag: log result object."""
246246
self.__log_result_obj = val
@@ -338,6 +338,16 @@ def _make_calling_record(self, name, arguments, method='Calling'):
338338
)
339339
)
340340

341+
def _make_exc_record(self, name, arguments):
342+
self._logger.log(
343+
level=self.exc_level,
344+
msg="Failed: \n{name!r}({arguments})".format(
345+
name=name,
346+
arguments=arguments if self.log_call_args_on_exc else '',
347+
),
348+
exc_info=True
349+
)
350+
341351
@abc.abstractmethod
342352
def _get_function_wrapper(self, func):
343353
"""Here should be constructed and returned real decorator.

test/test_log_wrap.py

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,190 @@ def func():
500500
]
501501
)
502502

503+
def test_disable_args(self, logger):
504+
new_logger = mock.Mock(spec=logging.Logger, name='logger')
505+
log = mock.Mock(name='log')
506+
new_logger.attach_mock(log, 'log')
507+
508+
arg1 = 'test arg 1'
509+
arg2 = 'test arg 2'
510+
511+
@logwrap.logwrap(log=new_logger, log_call_args=False)
512+
def func(test_arg1, test_arg2):
513+
return test_arg1, test_arg2
514+
515+
result = func(arg1, arg2)
516+
self.assertEqual(result, (arg1, arg2))
517+
log.assert_has_calls((
518+
mock.call(
519+
level=logging.DEBUG,
520+
msg="Calling: \n"
521+
"'func'()"
522+
),
523+
mock.call(
524+
level=logging.DEBUG,
525+
msg="Done: 'func' with result:\n{}".format(
526+
logwrap.pretty_repr(result))
527+
),
528+
))
529+
530+
def test_disable_args_exc(self, logger):
531+
new_logger = mock.Mock(spec=logging.Logger, name='logger')
532+
log = mock.Mock(name='log')
533+
new_logger.attach_mock(log, 'log')
534+
535+
arg1 = 'test arg 1'
536+
arg2 = 'test arg 2'
537+
538+
@logwrap.logwrap(log=new_logger, log_call_args_on_exc=False)
539+
def func(test_arg1, test_arg2):
540+
raise TypeError('Blacklisted')
541+
542+
with self.assertRaises(TypeError):
543+
func(arg1, arg2)
544+
545+
self.assertEqual(len(logger.mock_calls), 0)
546+
log.assert_has_calls((
547+
mock.call(
548+
level=logging.DEBUG,
549+
msg="Calling: \n"
550+
"'func'(\n"
551+
" # POSITIONAL_OR_KEYWORD:\n"
552+
" 'test_arg1'={},\n"
553+
" 'test_arg2'={},\n"
554+
")".format(
555+
logwrap.pretty_repr(
556+
arg1,
557+
indent=8,
558+
no_indent_start=True
559+
),
560+
logwrap.pretty_repr(
561+
arg2,
562+
indent=8,
563+
no_indent_start=True
564+
),
565+
)
566+
),
567+
mock.call(
568+
level=logging.ERROR,
569+
msg="Failed: \n'func'()",
570+
exc_info=True
571+
),
572+
))
573+
574+
def test_disable_all_args(self, logger):
575+
new_logger = mock.Mock(spec=logging.Logger, name='logger')
576+
log = mock.Mock(name='log')
577+
new_logger.attach_mock(log, 'log')
578+
579+
arg1 = 'test arg 1'
580+
arg2 = 'test arg 2'
581+
582+
@logwrap.logwrap(
583+
log=new_logger,
584+
log_call_args=False,
585+
log_call_args_on_exc=False
586+
)
587+
def func(test_arg1, test_arg2):
588+
raise TypeError('Blacklisted')
589+
590+
with self.assertRaises(TypeError):
591+
func(arg1, arg2)
592+
593+
self.assertEqual(len(logger.mock_calls), 0)
594+
log.assert_has_calls((
595+
mock.call(
596+
level=logging.DEBUG,
597+
msg="Calling: \n"
598+
"'func'()"
599+
),
600+
mock.call(
601+
level=logging.ERROR,
602+
msg="Failed: \n'func'()",
603+
exc_info=True
604+
),
605+
))
606+
607+
def test_disable_result(self, logger):
608+
new_logger = mock.Mock(spec=logging.Logger, name='logger')
609+
log = mock.Mock(name='log')
610+
new_logger.attach_mock(log, 'log')
611+
612+
@logwrap.logwrap(log=new_logger, log_result_obj=False)
613+
def func():
614+
return 'not logged'
615+
616+
func()
617+
618+
self.assertEqual(len(logger.mock_calls), 0)
619+
self.assertEqual(
620+
log.mock_calls,
621+
[
622+
mock.call(
623+
level=logging.DEBUG,
624+
msg="Calling: \n'func'()"
625+
),
626+
mock.call(
627+
level=logging.DEBUG,
628+
msg="Done: 'func'"
629+
),
630+
]
631+
)
632+
633+
634+
class TestObject(unittest.TestCase):
635+
def test_basic(self):
636+
log_call = logwrap.LogWrap()
637+
self.assertEqual(log_call.log_level, logging.DEBUG)
638+
self.assertEqual(log_call.exc_level, logging.ERROR)
639+
self.assertEqual(log_call.max_indent, 20)
640+
self.assertEqual(log_call.blacklisted_names, [])
641+
self.assertEqual(log_call.blacklisted_exceptions, [])
642+
self.assertTrue(log_call.log_call_args)
643+
self.assertTrue(log_call.log_call_args_on_exc)
644+
self.assertTrue(log_call.log_result_obj)
645+
646+
log_call.log_level = logging.INFO
647+
log_call.exc_level = logging.CRITICAL
648+
log_call.max_indent = 40
649+
log_call.blacklisted_names.append('password')
650+
log_call.blacklisted_exceptions.append(IOError)
651+
log_call.log_call_args = False
652+
log_call.log_call_args_on_exc = False
653+
log_call.log_result_obj = False
654+
655+
self.assertEqual(log_call.log_level, logging.INFO)
656+
self.assertEqual(log_call.exc_level, logging.CRITICAL)
657+
self.assertEqual(log_call.max_indent, 40)
658+
self.assertEqual(log_call.blacklisted_names, ['password'])
659+
self.assertEqual(log_call.blacklisted_exceptions, [IOError])
660+
self.assertFalse(log_call.log_call_args)
661+
self.assertFalse(log_call.log_call_args_on_exc)
662+
self.assertFalse(log_call.log_result_obj)
663+
664+
with self.assertRaises(TypeError):
665+
log_call.log_level = 'WARNING'
666+
667+
self.assertEqual(
668+
'{cls}('
669+
'log={logger}, '
670+
'log_level={obj.log_level}, '
671+
'exc_level={obj.exc_level}, '
672+
'max_indent={obj.max_indent}, '
673+
'spec=None, '
674+
'blacklisted_names={obj.blacklisted_names}, '
675+
'blacklisted_exceptions={obj.blacklisted_exceptions}, '
676+
'log_call_args={obj.log_call_args}, '
677+
'log_call_args_on_exc={obj.log_call_args_on_exc}, '
678+
'log_result_obj={obj.log_result_obj}, '
679+
')'.format(
680+
cls=log_call.__class__.__name__,
681+
logger=log_call._logger,
682+
obj=log_call
683+
),
684+
repr(log_call),
685+
)
686+
503687

504688
@mock.patch('logwrap._log_wrap_shared.logger', autospec=True)
505689
@unittest.skipUnless(

0 commit comments

Comments
 (0)