def test_text(self): self.assertEqual( decorators.pretty_repr('Unicode text'), "u'''Unicode text'''" ) self.assertEqual( decorators.pretty_repr(b'bytes text\x01'), "b'''bytes text\x01'''" )
def test_renamed_args_kwargs(self, logger): arg = 'arg' targs = ['string1', 'string2'] tkwargs = {'key': 'tkwargs'} @decorators.logwrap def func(arg, *positional, **named): return arg, tuple(positional), named result = func(arg, *targs, **tkwargs) self.assertEqual(result, (arg, tuple(targs), tkwargs)) # raise ValueError(logger.mock_calls) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(" "\n 'arg'={arg}," "\n 'positional'={args}," "\n 'named'={kwargs},\n)".format( arg=decorators.pretty_repr(arg, indent=8, no_indent_start=True), args=decorators.pretty_repr(tuple(targs), indent=8, no_indent_start=True), kwargs=decorators.pretty_repr(tkwargs, indent=8, no_indent_start=True))), mock.call.log(level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result))), ))
def test_args_complex(self, logger): string = 'string' dictionary = {'key': 'dictionary'} @decorators.logwrap def func(param_string, param_dictionary): return param_string, param_dictionary result = func(string, dictionary) self.assertEqual(result, (string, dictionary)) # raise ValueError(logger.mock_calls) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(" "\n 'param_string'={string}," "\n 'param_dictionary'={dictionary},\n)".format( string=decorators.pretty_repr(string, indent=8, no_indent_start=True), dictionary=decorators.pretty_repr(dictionary, indent=8, no_indent_start=True))), mock.call.log(level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result))), ))
def test_args_complex(self, logger): string = 'string' dictionary = {'key': 'dictionary'} @decorators.logwrap def func(param_string, param_dictionary): return param_string, param_dictionary result = func(string, dictionary) self.assertEqual(result, (string, dictionary)) # raise ValueError(logger.mock_calls) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(" "\n 'param_string'={string}," "\n 'param_dictionary'={dictionary},\n)".format( string=decorators.pretty_repr( string, indent=8, no_indent_start=True), dictionary=decorators.pretty_repr( dictionary, indent=8, no_indent_start=True) ) ), mock.call.log( level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result)) ), ))
def test_iterable(self): self.assertEqual(decorators.pretty_repr([1, 2, 3]), '\n[{nl:<5}1,{nl:<5}2,{nl:<5}3,\n]'.format(nl='\n')) self.assertEqual(decorators.pretty_repr((1, 2, 3)), '\n({nl:<5}1,{nl:<5}2,{nl:<5}3,\n)'.format(nl='\n')) res = decorators.pretty_repr({1, 2, 3}) self.assertTrue(res.startswith('\n{') and res.endswith('\n}'))
def test_args_kwargs(self, logger): targs = ['string1', 'string2'] tkwargs = {'key': 'tkwargs'} @decorators.logwrap def func(*args, **kwargs): return tuple(args), kwargs result = func(*targs, **tkwargs) self.assertEqual(result, (tuple(targs), tkwargs)) # raise ValueError(logger.mock_calls) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(" "\n 'args'={args}," "\n 'kwargs'={kwargs},\n)".format( args=decorators.pretty_repr( tuple(targs), indent=8, no_indent_start=True), kwargs=decorators.pretty_repr( tkwargs, indent=8, no_indent_start=True) ) ), mock.call.log( level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result)) ), ))
def test_args_kwargs(self, logger): targs = ['string1', 'string2'] tkwargs = {'key': 'tkwargs'} @decorators.logwrap def func(*args, **kwargs): return tuple(args), kwargs result = func(*targs, **tkwargs) self.assertEqual(result, (tuple(targs), tkwargs)) # raise ValueError(logger.mock_calls) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(" "\n 'args'={args}," "\n 'kwargs'={kwargs},\n)".format( args=decorators.pretty_repr(tuple(targs), indent=8, no_indent_start=True), kwargs=decorators.pretty_repr(tkwargs, indent=8, no_indent_start=True))), mock.call.log(level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result))), ))
def test_iterable(self): self.assertEqual( decorators.pretty_repr([1, 2, 3]), '\n[{nl:<5}1,{nl:<5}2,{nl:<5}3,\n]'.format(nl='\n') ) self.assertEqual( decorators.pretty_repr((1, 2, 3)), '\n({nl:<5}1,{nl:<5}2,{nl:<5}3,\n)'.format(nl='\n') ) res = decorators.pretty_repr({1, 2, 3}) self.assertTrue( res.startswith('\n{') and res.endswith('\n}') )
def test_nested_dict(self): test_obj = [{ 1: { 2: 3 }, 4: { 5: 6 }, }, { 7: 8, 9: (10, 11) }, ( 12, 13, ), { 14: { 15: { 16: { 17: { 18: { 19: [20] } } } } } }] exp_repr = ('\n[' '\n {' '\n 1: ' '\n {' '\n 2: 3,' '\n },' '\n 4: ' '\n {' '\n 5: 6,' '\n },' '\n },' '\n {' '\n 9: ' '\n (' '\n 10,' '\n 11,' '\n ),' '\n 7: 8,' '\n },' '\n (' '\n 12,' '\n 13,' '\n ),' '\n {' '\n 14: ' '\n {' '\n 15: {16: {17: {18: {19: [20]}}}},' '\n },' '\n },' '\n]') self.assertEqual(decorators.pretty_repr(test_obj), exp_repr)
def test_args_defaults(self, logger): arg = 'test arg' @decorators.logwrap def func(tst=arg): return tst result = func() self.assertEqual(result, arg) logger.assert_has_calls(( mock.call.log(level=logging.DEBUG, msg="Calling: \n'func'(\n 'tst'={},\n)".format( decorators.pretty_repr(arg, indent=8, no_indent_start=True))), mock.call.log(level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result))), ))
def test_nested_dict(self): test_obj = [ { 1: { 2: 3 }, 4: { 5: 6 }, }, { 7: 8, 9: (10, 11) }, ( 12, 13, ), {14: {15: {16: {17: {18: {19: [20]}}}}}} ] exp_repr = ( '\n[' '\n {' '\n 1: ' '\n {' '\n 2: 3,' '\n },' '\n 4: ' '\n {' '\n 5: 6,' '\n },' '\n },' '\n {' '\n 9: ' '\n (' '\n 10,' '\n 11,' '\n ),' '\n 7: 8,' '\n },' '\n (' '\n 12,' '\n 13,' '\n ),' '\n {' '\n 14: ' '\n {' '\n 15: {16: {17: {18: {19: [20]}}}},' '\n },' '\n },' '\n]' ) self.assertEqual(decorators.pretty_repr(test_obj), exp_repr)
def test_no_args(self, logger): @decorators.logwrap def func(): return 'No args' result = func() self.assertEqual(result, 'No args') logger.assert_has_calls(( mock.call.log(level=logging.DEBUG, msg="Calling: \n'func'()"), mock.call.log(level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result))), ))
def test_args_defaults(self, logger): arg = 'test arg' @decorators.logwrap def func(tst=arg): return tst result = func() self.assertEqual(result, arg) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(\n 'tst'={},\n)".format( decorators.pretty_repr(arg, indent=8, no_indent_start=True)) ), mock.call.log( level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result)) ), ))
def test_renamed_args_kwargs(self, logger): arg = 'arg' targs = ['string1', 'string2'] tkwargs = {'key': 'tkwargs'} @decorators.logwrap def func(arg, *positional, **named): return arg, tuple(positional), named result = func(arg, *targs, **tkwargs) self.assertEqual(result, (arg, tuple(targs), tkwargs)) # raise ValueError(logger.mock_calls) logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'(" "\n 'arg'={arg}," "\n 'positional'={args}," "\n 'named'={kwargs},\n)".format( arg=decorators.pretty_repr( arg, indent=8, no_indent_start=True), args=decorators.pretty_repr( tuple(targs), indent=8, no_indent_start=True), kwargs=decorators.pretty_repr( tkwargs, indent=8, no_indent_start=True) ) ), mock.call.log( level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result)) ), ))
def test_no_args(self, logger): @decorators.logwrap def func(): return 'No args' result = func() self.assertEqual(result, 'No args') logger.assert_has_calls(( mock.call.log( level=logging.DEBUG, msg="Calling: \n'func'()" ), mock.call.log( level=logging.DEBUG, msg="Done: 'func' with result:\n{}".format( decorators.pretty_repr(result)) ), ))
def test_dict(self): self.assertEqual(decorators.pretty_repr({ 1: 1, 2: 2, 33: 33 }), '\n{\n 1 : 1,\n 2 : 2,\n 33: 33,\n}')
def test_dict(self): self.assertEqual( decorators.pretty_repr({1: 1, 2: 2, 33: 33}), '\n{\n 1 : 1,\n 2 : 2,\n 33: 33,\n}' )
def test_simple(self): self.assertEqual( decorators.pretty_repr(True), repr(True) )
def test_simple(self): self.assertEqual(decorators.pretty_repr(True), repr(True))