Skip to content

Commit

Permalink
Add tests for recursive_unicode function
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Jan 23, 2024
1 parent 15c6c16 commit 9cae450
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/tribler/core/utilities/tests/test_unicode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest

from tribler.core.utilities.unicode import recursive_unicode


def test_recursive_unicode_empty():
# Test that recursive_unicode works on empty items
assert recursive_unicode({}) == {}
assert recursive_unicode([]) == []
assert recursive_unicode(b'') == ''
assert recursive_unicode('') == ''
assert recursive_unicode(None) is None


def test_recursive_unicode_unicode_decode_error():
# Test that recursive_unicode raises an exception on invalid bytes
with pytest.raises(UnicodeDecodeError):
recursive_unicode(b'\x80')


def test_recursive_unicode_unicode_decode_error_ignore_errors():
# Test that recursive_unicode ignores errors on invalid bytes and returns the converted bytes by using chr()
assert recursive_unicode(b'\x80', ignore_errors=True) == '\x80'


def test_recursive_unicode_complex_object():
# Test that recursive_unicode works on a complex object
obj = {
'list': [
b'binary',
{}
],
'sub dict': {
'sub list': [
1,
b'binary',
{
'': b''
},
]
}
}

expected = {
'list': [
'binary',
{}
],
'sub dict': {
'sub list': [
1,
'binary',
{
'': ''
},
]
}
}
assert recursive_unicode(obj) == expected

0 comments on commit 9cae450

Please sign in to comment.