|
| 1 | +from replicate import Replicate |
| 2 | +from replicate.lib._files import encode_json, async_encode_json, filter_none_values |
| 3 | + |
| 4 | + |
| 5 | +def test_filter_none_values_simple_dict(): |
| 6 | + """Test that None values are filtered from a simple dictionary.""" |
| 7 | + input_dict = {"prompt": "banana", "seed": None, "width": 512} |
| 8 | + result = filter_none_values(input_dict) |
| 9 | + assert result == {"prompt": "banana", "width": 512} |
| 10 | + assert "seed" not in result |
| 11 | + |
| 12 | + |
| 13 | +def test_filter_none_values_nested_dict(): |
| 14 | + """Test that None values are filtered from nested dictionaries.""" |
| 15 | + input_dict = { |
| 16 | + "prompt": "banana", |
| 17 | + "config": {"seed": None, "temperature": 0.8, "iterations": None}, |
| 18 | + "width": 512, |
| 19 | + } |
| 20 | + result = filter_none_values(input_dict) |
| 21 | + assert result == { |
| 22 | + "prompt": "banana", |
| 23 | + "config": {"temperature": 0.8}, |
| 24 | + "width": 512, |
| 25 | + } |
| 26 | + assert "seed" not in result["config"] |
| 27 | + assert "iterations" not in result["config"] |
| 28 | + |
| 29 | + |
| 30 | +def test_filter_none_values_all_none(): |
| 31 | + """Test that a dict with all None values returns an empty dict.""" |
| 32 | + input_dict = {"seed": None, "temperature": None} |
| 33 | + result = filter_none_values(input_dict) |
| 34 | + assert result == {} |
| 35 | + |
| 36 | + |
| 37 | +def test_filter_none_values_empty_dict(): |
| 38 | + """Test that an empty dict returns an empty dict.""" |
| 39 | + input_dict = {} |
| 40 | + result = filter_none_values(input_dict) |
| 41 | + assert result == {} |
| 42 | + |
| 43 | + |
| 44 | +def test_filter_none_values_with_list(): |
| 45 | + """Test that lists are preserved and None values in dicts within lists are filtered.""" |
| 46 | + input_dict = { |
| 47 | + "prompts": ["banana", "apple"], |
| 48 | + "seeds": [None, 42, None], |
| 49 | + "config": {"value": None}, |
| 50 | + } |
| 51 | + result = filter_none_values(input_dict) |
| 52 | + # None values in lists are preserved |
| 53 | + assert result == { |
| 54 | + "prompts": ["banana", "apple"], |
| 55 | + "seeds": [None, 42, None], |
| 56 | + "config": {}, |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +def test_filter_none_values_with_tuple(): |
| 61 | + """Test that tuples are preserved.""" |
| 62 | + input_dict = {"coords": (1, None, 3)} |
| 63 | + result = filter_none_values(input_dict) |
| 64 | + # Tuples are preserved as-is |
| 65 | + assert result == {"coords": (1, None, 3)} |
| 66 | + |
| 67 | + |
| 68 | +def test_filter_none_values_non_dict(): |
| 69 | + """Test that non-dict values are returned as-is.""" |
| 70 | + assert filter_none_values("string") == "string" |
| 71 | + assert filter_none_values(42) == 42 |
| 72 | + assert filter_none_values(None) is None |
| 73 | + assert filter_none_values([1, 2, 3]) == [1, 2, 3] |
| 74 | + |
| 75 | + |
| 76 | +def test_encode_json_filters_none(client: Replicate): |
| 77 | + """Test that encode_json filters None values from dicts.""" |
| 78 | + input_dict = {"prompt": "banana", "seed": None, "width": 512} |
| 79 | + result = encode_json(input_dict, client) |
| 80 | + assert result == {"prompt": "banana", "width": 512} |
| 81 | + assert "seed" not in result |
| 82 | + |
| 83 | + |
| 84 | +def test_encode_json_nested_none_filtering(client: Replicate): |
| 85 | + """Test that encode_json recursively filters None values.""" |
| 86 | + input_dict = { |
| 87 | + "prompt": "banana", |
| 88 | + "config": {"seed": None, "temperature": 0.8}, |
| 89 | + "metadata": {"user": "test", "session": None}, |
| 90 | + } |
| 91 | + result = encode_json(input_dict, client) |
| 92 | + assert result == { |
| 93 | + "prompt": "banana", |
| 94 | + "config": {"temperature": 0.8}, |
| 95 | + "metadata": {"user": "test"}, |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +async def test_async_encode_json_filters_none(async_client): |
| 100 | + """Test that async_encode_json filters None values from dicts.""" |
| 101 | + input_dict = {"prompt": "banana", "seed": None, "width": 512} |
| 102 | + result = await async_encode_json(input_dict, async_client) |
| 103 | + assert result == {"prompt": "banana", "width": 512} |
| 104 | + assert "seed" not in result |
| 105 | + |
| 106 | + |
| 107 | +async def test_async_encode_json_nested_none_filtering(async_client): |
| 108 | + """Test that async_encode_json recursively filters None values.""" |
| 109 | + input_dict = { |
| 110 | + "prompt": "banana", |
| 111 | + "config": {"seed": None, "temperature": 0.8}, |
| 112 | + "metadata": {"user": "test", "session": None}, |
| 113 | + } |
| 114 | + result = await async_encode_json(input_dict, async_client) |
| 115 | + assert result == { |
| 116 | + "prompt": "banana", |
| 117 | + "config": {"temperature": 0.8}, |
| 118 | + "metadata": {"user": "test"}, |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +def test_encode_json_preserves_false_and_zero(client: Replicate): |
| 123 | + """Test that False and 0 are not filtered out.""" |
| 124 | + input_dict = { |
| 125 | + "prompt": "banana", |
| 126 | + "seed": 0, |
| 127 | + "enable_feature": False, |
| 128 | + "iterations": None, |
| 129 | + } |
| 130 | + result = encode_json(input_dict, client) |
| 131 | + assert result == { |
| 132 | + "prompt": "banana", |
| 133 | + "seed": 0, |
| 134 | + "enable_feature": False, |
| 135 | + } |
| 136 | + assert "iterations" not in result |
0 commit comments