follow same test pattern
This commit is contained in:
parent
d438b8f311
commit
5f17a2b6a9
2 changed files with 90 additions and 220 deletions
|
|
@ -42,79 +42,38 @@ class TestStringToBytes:
|
||||||
assert result == input_bytes
|
assert result == input_bytes
|
||||||
assert result is input_bytes # Should be the same object
|
assert result is input_bytes # Should be the same object
|
||||||
|
|
||||||
def test_empty_string(self):
|
|
||||||
"""Test conversion of empty string"""
|
|
||||||
result = string_to_bytes("")
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
assert result == b""
|
|
||||||
assert len(result) == 0
|
|
||||||
|
|
||||||
def test_unicode_characters(self):
|
|
||||||
"""Test conversion of Unicode characters"""
|
|
||||||
input_string = "Hello 世界 🌍"
|
|
||||||
result = string_to_bytes(input_string)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
# Verify it can be decoded back
|
|
||||||
assert result.decode("utf-8") == input_string
|
|
||||||
|
|
||||||
def test_special_characters(self):
|
|
||||||
"""Test conversion of special characters"""
|
|
||||||
input_string = "Hello, world! @#$%^&*()"
|
|
||||||
result = string_to_bytes(input_string)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
assert result.decode("utf-8") == input_string
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("input_val,expected", [
|
@pytest.mark.parametrize("input_val,expected", [
|
||||||
("test", b"test"),
|
("test", b"test"),
|
||||||
("", b""),
|
("", b""),
|
||||||
("123", b"123"),
|
("123", b"123"),
|
||||||
("Hello World", b"Hello World"),
|
("Hello World", b"Hello World"),
|
||||||
|
("Hello 世界 🌍", "Hello 世界 🌍".encode("utf-8")),
|
||||||
|
("Hello, world! @#$%^&*()", b"Hello, world! @#$%^&*()"),
|
||||||
|
("Newline\nTab\tQuote\"", b"Newline\nTab\tQuote\""),
|
||||||
])
|
])
|
||||||
def test_various_string_inputs(self, input_val, expected):
|
def test_various_string_inputs(self, input_val, expected):
|
||||||
"""Test various string inputs"""
|
"""Test conversion of various string inputs including unicode and special characters"""
|
||||||
result = string_to_bytes(input_val)
|
result = string_to_bytes(input_val)
|
||||||
|
assert isinstance(result, bytes)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
class TestBytesToString:
|
class TestBytesToString:
|
||||||
"""Test cases for bytes_to_string function"""
|
"""Test cases for bytes_to_string function"""
|
||||||
|
|
||||||
def test_bytes_input_returns_string(self):
|
|
||||||
"""Test that bytes input is converted to string"""
|
|
||||||
input_bytes = b"hello world"
|
|
||||||
result = bytes_to_string(input_bytes)
|
|
||||||
|
|
||||||
assert isinstance(result, str)
|
|
||||||
assert result == "hello world"
|
|
||||||
|
|
||||||
def test_empty_bytes(self):
|
|
||||||
"""Test conversion of empty bytes"""
|
|
||||||
result = bytes_to_string(b"")
|
|
||||||
|
|
||||||
assert isinstance(result, str)
|
|
||||||
assert result == ""
|
|
||||||
assert len(result) == 0
|
|
||||||
|
|
||||||
def test_unicode_bytes(self):
|
|
||||||
"""Test conversion of Unicode bytes"""
|
|
||||||
input_bytes = "Hello 世界 🌍".encode("utf-8")
|
|
||||||
result = bytes_to_string(input_bytes)
|
|
||||||
|
|
||||||
assert isinstance(result, str)
|
|
||||||
assert result == "Hello 世界 🌍"
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("input_bytes,expected", [
|
@pytest.mark.parametrize("input_bytes,expected", [
|
||||||
|
(b"hello world", "hello world"),
|
||||||
(b"test", "test"),
|
(b"test", "test"),
|
||||||
(b"", ""),
|
(b"", ""),
|
||||||
(b"123", "123"),
|
(b"123", "123"),
|
||||||
(b"Hello World", "Hello World"),
|
(b"Hello World", "Hello World"),
|
||||||
|
("Hello 世界 🌍".encode("utf-8"), "Hello 世界 🌍"),
|
||||||
|
(b"Special: @#$%^&*()", "Special: @#$%^&*()"),
|
||||||
])
|
])
|
||||||
def test_various_bytes_inputs(self, input_bytes, expected):
|
def test_various_bytes_inputs(self, input_bytes, expected):
|
||||||
"""Test various bytes inputs"""
|
"""Test conversion of various bytes inputs including unicode"""
|
||||||
result = bytes_to_string(input_bytes)
|
result = bytes_to_string(input_bytes)
|
||||||
|
assert isinstance(result, str)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
def test_invalid_utf8_raises_error(self):
|
def test_invalid_utf8_raises_error(self):
|
||||||
|
|
@ -129,35 +88,32 @@ class TestBytesToString:
|
||||||
class TestRoundtripConversion:
|
class TestRoundtripConversion:
|
||||||
"""Test roundtrip conversions between string and bytes"""
|
"""Test roundtrip conversions between string and bytes"""
|
||||||
|
|
||||||
def test_string_to_bytes_to_string(self):
|
|
||||||
"""Test converting string to bytes and back"""
|
|
||||||
original = "Hello, World! 世界"
|
|
||||||
|
|
||||||
as_bytes = string_to_bytes(original)
|
|
||||||
back_to_string = bytes_to_string(as_bytes)
|
|
||||||
|
|
||||||
assert back_to_string == original
|
|
||||||
|
|
||||||
def test_bytes_to_string_to_bytes(self):
|
|
||||||
"""Test converting bytes to string and back"""
|
|
||||||
original = b"Hello, World!"
|
|
||||||
|
|
||||||
as_string = bytes_to_string(original)
|
|
||||||
back_to_bytes = string_to_bytes(as_string)
|
|
||||||
|
|
||||||
assert back_to_bytes == original
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("test_string", [
|
@pytest.mark.parametrize("test_string", [
|
||||||
"Simple text",
|
"Simple text",
|
||||||
|
"Hello, World! 世界",
|
||||||
"Unicode: 你好世界 🌍",
|
"Unicode: 你好世界 🌍",
|
||||||
"Special: !@#$%^&*()",
|
"Special: !@#$%^&*()",
|
||||||
"Multiline\nWith\tTabs",
|
"Multiline\nWith\tTabs",
|
||||||
"",
|
"",
|
||||||
])
|
])
|
||||||
def test_roundtrip_various_strings(self, test_string):
|
def test_string_to_bytes_to_string(self, test_string):
|
||||||
"""Test roundtrip conversion for various strings"""
|
"""Test converting string to bytes and back for various inputs"""
|
||||||
result = bytes_to_string(string_to_bytes(test_string))
|
as_bytes = string_to_bytes(test_string)
|
||||||
assert result == test_string
|
back_to_string = bytes_to_string(as_bytes)
|
||||||
|
assert back_to_string == test_string
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("test_bytes", [
|
||||||
|
b"Simple text",
|
||||||
|
b"Hello, World!",
|
||||||
|
"Unicode: 你好世界 🌍".encode("utf-8"),
|
||||||
|
b"Special: !@#$%^&*()",
|
||||||
|
b"",
|
||||||
|
])
|
||||||
|
def test_bytes_to_string_to_bytes(self, test_bytes):
|
||||||
|
"""Test converting bytes to string and back for various inputs"""
|
||||||
|
as_string = bytes_to_string(test_bytes)
|
||||||
|
back_to_bytes = string_to_bytes(as_string)
|
||||||
|
assert back_to_bytes == test_bytes
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -34,30 +34,31 @@ from api.utils.configs import (
|
||||||
class TestSerializeB64:
|
class TestSerializeB64:
|
||||||
"""Test cases for serialize_b64 function"""
|
"""Test cases for serialize_b64 function"""
|
||||||
|
|
||||||
def test_serialize_dict(self):
|
@pytest.mark.parametrize("test_data", [
|
||||||
"""Test serialization of a dictionary"""
|
{"key": "value", "number": 42},
|
||||||
test_dict = {"key": "value", "number": 42}
|
[1, 2, 3, "test", {"nested": "dict"}],
|
||||||
result = serialize_b64(test_dict)
|
"Hello, World!",
|
||||||
|
12345,
|
||||||
|
{
|
||||||
|
"list": [1, 2, 3],
|
||||||
|
"dict": {"nested": {"deep": "value"}},
|
||||||
|
"tuple": (1, 2, 3),
|
||||||
|
"string": "test",
|
||||||
|
"number": 42.5
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
{},
|
||||||
|
[],
|
||||||
|
])
|
||||||
|
def test_serialize_returns_bytes(self, test_data):
|
||||||
|
"""Test serialization of various data types returns bytes"""
|
||||||
|
result = serialize_b64(test_data, to_str=False)
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
assert isinstance(result, bytes)
|
||||||
# Should be valid base64
|
# Should be valid base64
|
||||||
decoded = base64.b64decode(result)
|
decoded = base64.b64decode(result)
|
||||||
assert isinstance(decoded, bytes)
|
assert isinstance(decoded, bytes)
|
||||||
|
|
||||||
def test_serialize_list(self):
|
|
||||||
"""Test serialization of a list"""
|
|
||||||
test_list = [1, 2, 3, "test", {"nested": "dict"}]
|
|
||||||
result = serialize_b64(test_list)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_with_to_str_false(self):
|
|
||||||
"""Test serialization with to_str=False returns bytes"""
|
|
||||||
test_data = {"test": "data"}
|
|
||||||
result = serialize_b64(test_data, to_str=False)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_with_to_str_true(self):
|
def test_serialize_with_to_str_true(self):
|
||||||
"""Test serialization with to_str=True returns string"""
|
"""Test serialization with to_str=True returns string"""
|
||||||
test_data = {"test": "data"}
|
test_data = {"test": "data"}
|
||||||
|
|
@ -67,68 +68,15 @@ class TestSerializeB64:
|
||||||
# Should be valid base64 string
|
# Should be valid base64 string
|
||||||
base64.b64decode(result) # Should not raise
|
base64.b64decode(result) # Should not raise
|
||||||
|
|
||||||
def test_serialize_string(self):
|
|
||||||
"""Test serialization of a string"""
|
|
||||||
test_string = "Hello, World!"
|
|
||||||
result = serialize_b64(test_string)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_number(self):
|
|
||||||
"""Test serialization of numbers"""
|
|
||||||
test_int = 12345
|
|
||||||
result = serialize_b64(test_int)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_complex_nested_structure(self):
|
|
||||||
"""Test serialization of complex nested structures"""
|
|
||||||
test_data = {
|
|
||||||
"list": [1, 2, 3],
|
|
||||||
"dict": {"nested": {"deep": "value"}},
|
|
||||||
"tuple": (1, 2, 3),
|
|
||||||
"string": "test",
|
|
||||||
"number": 42.5
|
|
||||||
}
|
|
||||||
result = serialize_b64(test_data)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_none(self):
|
|
||||||
"""Test serialization of None"""
|
|
||||||
result = serialize_b64(None)
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_empty_dict(self):
|
|
||||||
"""Test serialization of empty dictionary"""
|
|
||||||
result = serialize_b64({})
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
def test_serialize_empty_list(self):
|
|
||||||
"""Test serialization of empty list"""
|
|
||||||
result = serialize_b64([])
|
|
||||||
|
|
||||||
assert isinstance(result, bytes)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDeserializeB64:
|
class TestDeserializeB64:
|
||||||
"""Test cases for deserialize_b64 function"""
|
"""Test cases for deserialize_b64 function"""
|
||||||
|
|
||||||
def test_deserialize_string_input(self):
|
@pytest.mark.parametrize("to_str", [True, False])
|
||||||
"""Test deserialization with string input"""
|
def test_deserialize_string_and_bytes_input(self, to_str):
|
||||||
|
"""Test deserialization with both string and bytes input"""
|
||||||
test_data = {"key": "value"}
|
test_data = {"key": "value"}
|
||||||
serialized = serialize_b64(test_data, to_str=True)
|
serialized = serialize_b64(test_data, to_str=to_str)
|
||||||
|
|
||||||
result = deserialize_b64(serialized)
|
|
||||||
|
|
||||||
assert result == test_data
|
|
||||||
|
|
||||||
def test_deserialize_bytes_input(self):
|
|
||||||
"""Test deserialization with bytes input"""
|
|
||||||
test_data = {"key": "value"}
|
|
||||||
serialized = serialize_b64(test_data, to_str=False)
|
|
||||||
|
|
||||||
result = deserialize_b64(serialized)
|
result = deserialize_b64(serialized)
|
||||||
|
|
||||||
|
|
@ -160,22 +108,14 @@ class TestDeserializeB64:
|
||||||
|
|
||||||
assert result == test_data
|
assert result == test_data
|
||||||
|
|
||||||
def test_roundtrip_serialization(self):
|
@pytest.mark.parametrize("test_data", [
|
||||||
"""Test complete roundtrip serialization and deserialization"""
|
{"key": "value"},
|
||||||
test_data = {
|
{
|
||||||
"string": "test",
|
"string": "test",
|
||||||
"number": 123,
|
"number": 123,
|
||||||
"list": [1, 2, 3],
|
"list": [1, 2, 3],
|
||||||
"nested": {"key": "value"}
|
"nested": {"key": "value"}
|
||||||
}
|
},
|
||||||
|
|
||||||
serialized = serialize_b64(test_data, to_str=True)
|
|
||||||
deserialized = deserialize_b64(serialized)
|
|
||||||
|
|
||||||
assert deserialized == test_data
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("test_data", [
|
|
||||||
{"key": "value"},
|
|
||||||
[1, 2, 3, 4, 5],
|
[1, 2, 3, 4, 5],
|
||||||
"simple string",
|
"simple string",
|
||||||
42,
|
42,
|
||||||
|
|
@ -184,7 +124,7 @@ class TestDeserializeB64:
|
||||||
{"nested": {"deep": {"structure": "value"}}},
|
{"nested": {"deep": {"structure": "value"}}},
|
||||||
])
|
])
|
||||||
def test_roundtrip_various_data_types(self, test_data):
|
def test_roundtrip_various_data_types(self, test_data):
|
||||||
"""Test roundtrip for various data types"""
|
"""Test roundtrip serialization and deserialization for various data types"""
|
||||||
serialized = serialize_b64(test_data)
|
serialized = serialize_b64(test_data)
|
||||||
deserialized = deserialize_b64(serialized)
|
deserialized = deserialize_b64(serialized)
|
||||||
|
|
||||||
|
|
@ -194,14 +134,19 @@ class TestDeserializeB64:
|
||||||
class TestRestrictedUnpickler:
|
class TestRestrictedUnpickler:
|
||||||
"""Test cases for RestrictedUnpickler class"""
|
"""Test cases for RestrictedUnpickler class"""
|
||||||
|
|
||||||
def test_allows_safe_modules(self):
|
@pytest.mark.parametrize("test_data", [
|
||||||
"""Test that safe modules are allowed"""
|
{"test": "data"},
|
||||||
# Create a simple object that would be in a safe module context
|
[1, 2, 3, "test", {"key": "value"}],
|
||||||
test_data = {"test": "data"}
|
{"nested": {"deep": "structure"}},
|
||||||
|
[1, 2, 3],
|
||||||
|
"simple string",
|
||||||
|
])
|
||||||
|
def test_restricted_loads_with_safe_data(self, test_data):
|
||||||
|
"""Test restricted_loads with various safe data types"""
|
||||||
pickled = pickle.dumps(test_data)
|
pickled = pickle.dumps(test_data)
|
||||||
|
|
||||||
# This should work without raising
|
|
||||||
result = restricted_loads(pickled)
|
result = restricted_loads(pickled)
|
||||||
|
|
||||||
assert result == test_data
|
assert result == test_data
|
||||||
|
|
||||||
@patch('api.utils.configs.get_base_config')
|
@patch('api.utils.configs.get_base_config')
|
||||||
|
|
@ -231,48 +176,42 @@ class TestRestrictedUnpickler:
|
||||||
assert 'numpy' in safe_module
|
assert 'numpy' in safe_module
|
||||||
assert 'rag_flow' in safe_module
|
assert 'rag_flow' in safe_module
|
||||||
|
|
||||||
def test_restricted_loads_with_safe_data(self):
|
|
||||||
"""Test restricted_loads with safe data"""
|
|
||||||
test_data = [1, 2, 3, "test", {"key": "value"}]
|
|
||||||
pickled = pickle.dumps(test_data)
|
|
||||||
|
|
||||||
result = restricted_loads(pickled)
|
|
||||||
|
|
||||||
assert result == test_data
|
|
||||||
|
|
||||||
|
|
||||||
class TestIntegrationScenarios:
|
class TestIntegrationScenarios:
|
||||||
"""Integration tests for serialization/deserialization workflows"""
|
"""Integration tests for serialization/deserialization workflows"""
|
||||||
|
|
||||||
def test_serialize_deserialize_workflow(self):
|
@pytest.mark.parametrize("to_str,original_data", [
|
||||||
"""Test complete workflow of serialize and deserialize"""
|
(True, {
|
||||||
original_data = {
|
|
||||||
"user": "test_user",
|
"user": "test_user",
|
||||||
"settings": {
|
"settings": {
|
||||||
"theme": "dark",
|
"theme": "dark",
|
||||||
"notifications": True
|
"notifications": True
|
||||||
},
|
},
|
||||||
"items": [1, 2, 3, 4, 5]
|
"items": [1, 2, 3, 4, 5]
|
||||||
}
|
}),
|
||||||
|
(False, {"test": "data", "number": 42}),
|
||||||
|
(True, {}),
|
||||||
|
(True, {
|
||||||
|
f"key_{i}": {
|
||||||
|
"value": i,
|
||||||
|
"list": list(range(10)),
|
||||||
|
"nested": {"deep": f"value_{i}"}
|
||||||
|
}
|
||||||
|
for i in range(100)
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
def test_serialize_deserialize_workflow(self, to_str, original_data):
|
||||||
|
"""Test complete workflow of serialize and deserialize with various data"""
|
||||||
|
# Serialize
|
||||||
|
serialized = serialize_b64(original_data, to_str=to_str)
|
||||||
|
|
||||||
# Serialize to string
|
if to_str:
|
||||||
serialized_str = serialize_b64(original_data, to_str=True)
|
assert isinstance(serialized, str)
|
||||||
assert isinstance(serialized_str, str)
|
else:
|
||||||
|
assert isinstance(serialized, bytes)
|
||||||
|
|
||||||
# Deserialize back
|
# Deserialize back
|
||||||
deserialized = deserialize_b64(serialized_str)
|
deserialized = deserialize_b64(serialized)
|
||||||
assert deserialized == original_data
|
|
||||||
|
|
||||||
def test_serialize_deserialize_with_bytes(self):
|
|
||||||
"""Test workflow using bytes format"""
|
|
||||||
original_data = {"test": "data", "number": 42}
|
|
||||||
|
|
||||||
# Serialize to bytes
|
|
||||||
serialized_bytes = serialize_b64(original_data, to_str=False)
|
|
||||||
assert isinstance(serialized_bytes, bytes)
|
|
||||||
|
|
||||||
# Deserialize back
|
|
||||||
deserialized = deserialize_b64(serialized_bytes)
|
|
||||||
assert deserialized == original_data
|
assert deserialized == original_data
|
||||||
|
|
||||||
@patch('api.utils.configs.get_base_config')
|
@patch('api.utils.configs.get_base_config')
|
||||||
|
|
@ -287,31 +226,6 @@ class TestIntegrationScenarios:
|
||||||
|
|
||||||
assert result == test_data
|
assert result == test_data
|
||||||
|
|
||||||
def test_empty_data_workflow(self):
|
|
||||||
"""Test workflow with empty data"""
|
|
||||||
empty_dict = {}
|
|
||||||
|
|
||||||
serialized = serialize_b64(empty_dict, to_str=True)
|
|
||||||
deserialized = deserialize_b64(serialized)
|
|
||||||
|
|
||||||
assert deserialized == empty_dict
|
|
||||||
|
|
||||||
def test_large_data_workflow(self):
|
|
||||||
"""Test workflow with larger data structures"""
|
|
||||||
large_data = {
|
|
||||||
f"key_{i}": {
|
|
||||||
"value": i,
|
|
||||||
"list": list(range(10)),
|
|
||||||
"nested": {"deep": f"value_{i}"}
|
|
||||||
}
|
|
||||||
for i in range(100)
|
|
||||||
}
|
|
||||||
|
|
||||||
serialized = serialize_b64(large_data, to_str=True)
|
|
||||||
deserialized = deserialize_b64(serialized)
|
|
||||||
|
|
||||||
assert deserialized == large_data
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
pytest.main([__file__, "-v"])
|
pytest.main([__file__, "-v"])
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue