Merge pull request #1165 from hughiwnl/add-utils-tests
feat: Add tests for any_to_str and str_to_dict utilitiespull/1166/head^2
commit
bb92993748
@ -0,0 +1,124 @@
|
||||
import pytest
|
||||
|
||||
from loguru import logger
|
||||
from swarms.utils.any_to_str import any_to_str
|
||||
|
||||
|
||||
class TestAnyToStr:
|
||||
"""Test cases for the any_to_str function."""
|
||||
|
||||
def test_dictionary(self):
|
||||
"""Test converting a dictionary to string."""
|
||||
try:
|
||||
result = any_to_str({"a": 1, "b": 2})
|
||||
assert result is not None, "Result should not be None"
|
||||
assert "a: 1" in result
|
||||
assert "b: 2" in result
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_dictionary: {e}")
|
||||
pytest.fail(f"test_dictionary failed with error: {e}")
|
||||
|
||||
def test_list(self):
|
||||
"""Test converting a list to string."""
|
||||
try:
|
||||
result = any_to_str([1, 2, 3])
|
||||
assert result is not None, "Result should not be None"
|
||||
assert "1" in result
|
||||
assert "2" in result
|
||||
assert "3" in result
|
||||
assert "[" in result or "," in result
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_list: {e}")
|
||||
pytest.fail(f"test_list failed with error: {e}")
|
||||
|
||||
def test_none_value(self):
|
||||
"""Test converting None to string."""
|
||||
try:
|
||||
result = any_to_str(None)
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == "None"
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_none_value: {e}")
|
||||
pytest.fail(f"test_none_value failed with error: {e}")
|
||||
|
||||
def test_nested_dictionary(self):
|
||||
"""Test converting a nested dictionary."""
|
||||
try:
|
||||
data = {
|
||||
"user": {
|
||||
"id": 123,
|
||||
"details": {"city": "New York", "active": True},
|
||||
},
|
||||
"data": [1, 2, 3],
|
||||
}
|
||||
result = any_to_str(data)
|
||||
assert result is not None, "Result should not be None"
|
||||
assert "user:" in result
|
||||
assert "data:" in result
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_nested_dictionary: {e}")
|
||||
pytest.fail(f"test_nested_dictionary failed with error: {e}")
|
||||
|
||||
def test_tuple(self):
|
||||
"""Test converting a tuple to string."""
|
||||
try:
|
||||
result = any_to_str((True, False, None))
|
||||
assert result is not None, "Result should not be None"
|
||||
assert "True" in result or "true" in result.lower()
|
||||
assert "(" in result or "," in result
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_tuple: {e}")
|
||||
pytest.fail(f"test_tuple failed with error: {e}")
|
||||
|
||||
def test_empty_list(self):
|
||||
"""Test converting an empty list."""
|
||||
try:
|
||||
result = any_to_str([])
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == "[]"
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_empty_list: {e}")
|
||||
pytest.fail(f"test_empty_list failed with error: {e}")
|
||||
|
||||
def test_empty_dict(self):
|
||||
"""Test converting an empty dictionary."""
|
||||
try:
|
||||
result = any_to_str({})
|
||||
assert result is not None, "Result should not be None"
|
||||
# Empty dict might return empty string or other representation
|
||||
assert isinstance(result, str)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_empty_dict: {e}")
|
||||
pytest.fail(f"test_empty_dict failed with error: {e}")
|
||||
|
||||
def test_string_with_quotes(self):
|
||||
"""Test converting a string - should add quotes."""
|
||||
try:
|
||||
result = any_to_str("hello")
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == '"hello"'
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_string_with_quotes: {e}")
|
||||
pytest.fail(f"test_string_with_quotes failed with error: {e}")
|
||||
|
||||
def test_integer(self):
|
||||
"""Test converting an integer."""
|
||||
try:
|
||||
result = any_to_str(42)
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == "42"
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_integer: {e}")
|
||||
pytest.fail(f"test_integer failed with error: {e}")
|
||||
|
||||
def test_mixed_types_in_list(self):
|
||||
"""Test converting a list with mixed types."""
|
||||
try:
|
||||
result = any_to_str([1, "text", None, 2.5])
|
||||
assert result is not None, "Result should not be None"
|
||||
assert "1" in result
|
||||
assert "text" in result or '"text"' in result
|
||||
assert "None" in result
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_mixed_types_in_list: {e}")
|
||||
pytest.fail(f"test_mixed_types_in_list failed with error: {e}")
|
||||
@ -0,0 +1,134 @@
|
||||
import pytest
|
||||
import json
|
||||
|
||||
from loguru import logger
|
||||
from swarms.utils.str_to_dict import str_to_dict
|
||||
|
||||
|
||||
class TestStrToDict:
|
||||
"""Test cases for the str_to_dict function."""
|
||||
|
||||
def test_valid_json_string(self):
|
||||
"""Test converting a valid JSON string to dictionary."""
|
||||
try:
|
||||
result = str_to_dict('{"key": "value"}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"key": "value"}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_valid_json_string: {e}")
|
||||
pytest.fail(f"test_valid_json_string failed with error: {e}")
|
||||
|
||||
def test_nested_json_string(self):
|
||||
"""Test converting a nested JSON string."""
|
||||
try:
|
||||
result = str_to_dict('{"a": 1, "b": {"c": 2}}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"a": 1, "b": {"c": 2}}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_nested_json_string: {e}")
|
||||
pytest.fail(f"test_nested_json_string failed with error: {e}")
|
||||
|
||||
def test_list_in_json_string(self):
|
||||
"""Test converting JSON string containing a list."""
|
||||
try:
|
||||
result = str_to_dict('{"items": [1, 2, 3]}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"items": [1, 2, 3]}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_list_in_json_string: {e}")
|
||||
pytest.fail(f"test_list_in_json_string failed with error: {e}")
|
||||
|
||||
def test_empty_json_object(self):
|
||||
"""Test converting an empty JSON object."""
|
||||
try:
|
||||
result = str_to_dict("{}")
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_empty_json_object: {e}")
|
||||
pytest.fail(f"test_empty_json_object failed with error: {e}")
|
||||
|
||||
def test_json_with_numbers(self):
|
||||
"""Test converting JSON string with various number types."""
|
||||
try:
|
||||
result = str_to_dict('{"int": 42, "float": 3.14, "negative": -5}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"int": 42, "float": 3.14, "negative": -5}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_json_with_numbers: {e}")
|
||||
pytest.fail(f"test_json_with_numbers failed with error: {e}")
|
||||
|
||||
def test_json_with_booleans(self):
|
||||
"""Test converting JSON string with boolean values."""
|
||||
try:
|
||||
result = str_to_dict('{"true_val": true, "false_val": false}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"true_val": True, "false_val": False}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_json_with_booleans: {e}")
|
||||
pytest.fail(f"test_json_with_booleans failed with error: {e}")
|
||||
|
||||
def test_json_with_null(self):
|
||||
"""Test converting JSON string with null value."""
|
||||
try:
|
||||
result = str_to_dict('{"value": null}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"value": None}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_json_with_null: {e}")
|
||||
pytest.fail(f"test_json_with_null failed with error: {e}")
|
||||
|
||||
def test_invalid_json_raises_error(self):
|
||||
"""Test that invalid JSON raises JSONDecodeError."""
|
||||
try:
|
||||
with pytest.raises(json.JSONDecodeError):
|
||||
str_to_dict('{"invalid": json}') # Invalid JSON
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_invalid_json_raises_error: {e}")
|
||||
pytest.fail(f"test_invalid_json_raises_error failed with error: {e}")
|
||||
|
||||
def test_complex_nested_structure(self):
|
||||
"""Test converting a complex nested JSON structure."""
|
||||
try:
|
||||
json_str = '''
|
||||
{
|
||||
"user": {
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
"active": true
|
||||
},
|
||||
"tags": ["python", "testing"],
|
||||
"metadata": null
|
||||
}
|
||||
'''
|
||||
result = str_to_dict(json_str)
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result["user"]["name"] == "John"
|
||||
assert result["user"]["age"] == 30
|
||||
assert result["tags"] == ["python", "testing"]
|
||||
assert result["metadata"] is None
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_complex_nested_structure: {e}")
|
||||
pytest.fail(f"test_complex_nested_structure failed with error: {e}")
|
||||
|
||||
def test_retries_parameter(self):
|
||||
"""Test that retries parameter works correctly."""
|
||||
try:
|
||||
# This should succeed on first try
|
||||
result = str_to_dict('{"test": 1}', retries=1)
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result == {"test": 1}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_retries_parameter: {e}")
|
||||
pytest.fail(f"test_retries_parameter failed with error: {e}")
|
||||
|
||||
def test_json_with_unicode_characters(self):
|
||||
"""Test converting JSON string with unicode characters."""
|
||||
try:
|
||||
result = str_to_dict('{"emoji": "🐍", "text": "你好"}')
|
||||
assert result is not None, "Result should not be None"
|
||||
assert result["emoji"] == "🐍"
|
||||
assert result["text"] == "你好"
|
||||
except Exception as e:
|
||||
logger.error(f"Error in test_json_with_unicode_characters: {e}")
|
||||
pytest.fail(f"test_json_with_unicode_characters failed with error: {e}")
|
||||
Loading…
Reference in new issue