def test_json_string(): assert utils.json_string(None, 'nan') == 'nan' assert utils.json_string(u"\u25B2") is not None builtin_pkg = '__builtin__' if PY3: builtin_pkg = 'builtins' with ExitStack() as stack: stack.enter_context( mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=Exception))) mock_logger = stack.enter_context( mock.patch('dtale.utils.logger', mock.Mock())) assert utils.json_string('blah', 'nan') == 'nan' mock_logger.exception.assert_called_once() class MockStr(object): def __init__(self, string=''): if PY3: raise UnicodeEncodeError('', '', 0, 0, '') else: raise UnicodeEncodeError('', u'', 0, 0, '') class TestStr(object): def encode(self, encoding=None, errors=None): return 'blah' with ExitStack() as stack: stack.enter_context( mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=MockStr))) stack.enter_context(mock.patch('dtale.utils.logger', mock.Mock())) assert utils.json_string(TestStr(), 'nan') == 'blah'
def test_json_string(builtin_pkg): assert utils.json_string(None, nan_display='nan') == 'nan' assert utils.json_string(u"\u25B2") is not None class MockStr(object): def __init__(self, string=''): raise Exception('test') class MockLogger(object): def exception(self, v): pass with ExitStack() as stack: stack.enter_context(mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=MockStr))) stack.enter_context(mock.patch('dtale.utils.logger', MockLogger())) assert utils.json_string('blah', nan_display='nan') == 'nan'
def test_json_string(builtin_pkg): assert utils.json_string(None, nan_display="nan") == "nan" assert utils.json_string(u"\u25B2") is not None class MockStr(object): def __init__(self, string=""): raise Exception("test") class MockLogger(object): def exception(self, v): pass with ExitStack() as stack: stack.enter_context( mock.patch("{}.str".format(builtin_pkg), mock.Mock(side_effect=MockStr))) stack.enter_context(mock.patch("dtale.utils.logger", MockLogger())) assert utils.json_string("blah", nan_display="nan") == "nan"
def test_json_string2(builtin_pkg): class MockLogger(object): def exception(self, v): pass class MockStr(object): def __init__(self, string=""): raise UnicodeEncodeError("", "", 0, 0, "") class TestStr(object): def encode(self, encoding=None, errors=None): pass with ExitStack() as stack: stack.enter_context( mock.patch("{}.str".format(builtin_pkg), mock.Mock(side_effect=MockStr))) stack.enter_context(mock.patch("dtale.utils.logger", MockLogger())) utils.json_string(TestStr(), nan_display="nan")
def test_json_string2(builtin_pkg): class MockLogger(object): def exception(self, v): pass class MockStr(object): def __init__(self, string=''): if PY3: raise UnicodeEncodeError('', '', 0, 0, '') else: raise UnicodeEncodeError('', u'', 0, 0, '') class TestStr(object): def encode(self, encoding=None, errors=None): pass with ExitStack() as stack: stack.enter_context(mock.patch('{}.str'.format(builtin_pkg), mock.Mock(side_effect=MockStr))) stack.enter_context(mock.patch('dtale.utils.logger', MockLogger())) utils.json_string(TestStr(), nan_display='nan')