-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for recursive_unicode function
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |