def test_recursively_convert_to_str_with_dict(self): test_var_1_in_unicode = python_utils.UNICODE('test_var_1') test_var_2_in_unicode = python_utils.UNICODE('test_var_2') test_var_3_in_bytes = test_var_1_in_unicode.encode('utf-8') test_var_4_in_bytes = test_var_2_in_unicode.encode('utf-8') test_dict = { test_var_1_in_unicode: test_var_3_in_bytes, test_var_2_in_unicode: test_var_4_in_bytes } self.assertEqual(test_dict, { 'test_var_1': b'test_var_1', 'test_var_2': b'test_var_2' }) for key, val in test_dict.items(): self.assertEqual(type(key), future.types.newstr) self.assertEqual(type(val), future.types.newbytes) dict_in_str = python_utils._recursively_convert_to_str(test_dict) # pylint: disable=protected-access self.assertEqual(dict_in_str, { 'test_var_1': 'test_var_1', 'test_var_2': 'test_var_2' }) for key, val in dict_in_str.items(): self.assertEqual(type(key), unicode) self.assertEqual(type(val), bytes)
def test_recursively_convert_to_str_with_nested_structure(self): test_var_1_in_unicode = python_utils.UNICODE('test_var_1') test_list_1 = [ test_var_1_in_unicode, test_var_1_in_unicode.encode('utf-8'), 'test_var_2', b'test_var_3', { 'test_var_4': b'test_var_5' } ] test_dict = {test_var_1_in_unicode: test_list_1} self.assertEqual( test_dict, { 'test_var_1': [ 'test_var_1', b'test_var_1', 'test_var_2', b'test_var_3', { 'test_var_4': b'test_var_5' } ] }) dict_in_str = python_utils._recursively_convert_to_str(test_dict) # pylint: disable=protected-access self.assertEqual( dict_in_str, { 'test_var_1': [ 'test_var_1', b'test_var_1', 'test_var_2', 'test_var_3', { 'test_var_4': 'test_var_5' } ] }) for key, value in dict_in_str.items(): self.assertNotEqual(type(key), future.types.newstr) self.assertNotEqual(type(key), future.types.newbytes) self.assertTrue(isinstance(key, unicode)) for item in value: self.assertNotEqual(type(item), future.types.newstr) self.assertNotEqual(type(item), future.types.newbytes) self.assertTrue(isinstance(item, (unicode, bytes, dict))) for k, v in value[-1].items(): self.assertNotEqual(type(k), future.types.newstr) self.assertNotEqual(type(k), future.types.newbytes) self.assertNotEqual(type(v), future.types.newstr) self.assertNotEqual(type(v), future.types.newbytes) self.assertEqual(type(k), unicode) self.assertEqual(type(v), bytes)