From 19949c72c73a11080aaf1eaa3a23c5ef304fc241 Mon Sep 17 00:00:00 2001 From: Hugh <155223694+hughiwnl@users.noreply.github.com> Date: Mon, 27 Oct 2025 18:29:25 -0700 Subject: [PATCH] feat: Add tests for any_to_str and str_to_dict utilities --- tests/utils/test_any_to_str.py | 73 ++++++++++++++++++++++++++++++ tests/utils/test_str_to_dict.py | 80 +++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 tests/utils/test_any_to_str.py create mode 100644 tests/utils/test_str_to_dict.py diff --git a/tests/utils/test_any_to_str.py b/tests/utils/test_any_to_str.py new file mode 100644 index 00000000..2097eaf5 --- /dev/null +++ b/tests/utils/test_any_to_str.py @@ -0,0 +1,73 @@ +import pytest + +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.""" + result = any_to_str({"a": 1, "b": 2}) + assert "a: 1" in result + assert "b: 2" in result + + def test_list(self): + """Test converting a list to string.""" + result = any_to_str([1, 2, 3]) + assert "1" in result + assert "2" in result + assert "3" in result + assert "[" in result or "," in result + + def test_none_value(self): + """Test converting None to string.""" + result = any_to_str(None) + assert result == "None" + + def test_nested_dictionary(self): + """Test converting a nested dictionary.""" + data = { + "user": { + "id": 123, + "details": {"city": "New York", "active": True}, + }, + "data": [1, 2, 3], + } + result = any_to_str(data) + assert "user:" in result + assert "data:" in result + + def test_tuple(self): + """Test converting a tuple to string.""" + result = any_to_str((True, False, None)) + assert "True" in result or "true" in result.lower() + assert "(" in result or "," in result + + def test_empty_list(self): + """Test converting an empty list.""" + result = any_to_str([]) + assert result == "[]" + + def test_empty_dict(self): + """Test converting an empty dictionary.""" + result = any_to_str({}) + assert result == "" or "None" in result or len(result.strip()) == 0 + + def test_string_with_quotes(self): + """Test converting a string - should add quotes.""" + result = any_to_str("hello") + assert result == '"hello"' + + def test_integer(self): + """Test converting an integer.""" + result = any_to_str(42) + assert result == "42" + + def test_mixed_types_in_list(self): + """Test converting a list with mixed types.""" + result = any_to_str([1, "text", None, 2.5]) + assert "1" in result + assert "text" in result or '"text"' in result + assert "None" in result + diff --git a/tests/utils/test_str_to_dict.py b/tests/utils/test_str_to_dict.py new file mode 100644 index 00000000..6e157ebf --- /dev/null +++ b/tests/utils/test_str_to_dict.py @@ -0,0 +1,80 @@ +import pytest +import json + +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.""" + result = str_to_dict('{"key": "value"}') + assert result == {"key": "value"} + + def test_nested_json_string(self): + """Test converting a nested JSON string.""" + result = str_to_dict('{"a": 1, "b": {"c": 2}}') + assert result == {"a": 1, "b": {"c": 2}} + + def test_list_in_json_string(self): + """Test converting JSON string containing a list.""" + result = str_to_dict('{"items": [1, 2, 3]}') + assert result == {"items": [1, 2, 3]} + + def test_empty_json_object(self): + """Test converting an empty JSON object.""" + result = str_to_dict("{}") + assert result == {} + + def test_json_with_numbers(self): + """Test converting JSON string with various number types.""" + result = str_to_dict('{"int": 42, "float": 3.14, "negative": -5}') + assert result == {"int": 42, "float": 3.14, "negative": -5} + + def test_json_with_booleans(self): + """Test converting JSON string with boolean values.""" + result = str_to_dict('{"true_val": true, "false_val": false}') + assert result == {"true_val": True, "false_val": False} + + def test_json_with_null(self): + """Test converting JSON string with null value.""" + result = str_to_dict('{"value": null}') + assert result == {"value": None} + + def test_invalid_json_raises_error(self): + """Test that invalid JSON raises JSONDecodeError.""" + with pytest.raises(json.JSONDecodeError): + str_to_dict('{"invalid": json}') # Invalid JSON + + def test_complex_nested_structure(self): + """Test converting a complex nested JSON structure.""" + json_str = ''' + { + "user": { + "name": "John", + "age": 30, + "active": true + }, + "tags": ["python", "testing"], + "metadata": null + } + ''' + result = str_to_dict(json_str) + assert result["user"]["name"] == "John" + assert result["user"]["age"] == 30 + assert result["tags"] == ["python", "testing"] + assert result["metadata"] is None + + def test_retries_parameter(self): + """Test that retries parameter works correctly.""" + # This should succeed on first try + result = str_to_dict('{"test": 1}', retries=1) + assert result == {"test": 1} + + def test_json_with_unicode_characters(self): + """Test converting JSON string with unicode characters.""" + result = str_to_dict('{"emoji": "🐍", "text": "你好"}') + assert result["emoji"] == "🐍" + assert result["text"] == "你好" +