|
| 1 | +"""Test for variadic arguments (*args) handling in cachier.""" |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from cachier import cachier |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.pickle |
| 11 | +def test_varargs_different_cache_keys(): |
| 12 | + """Test that functions with *args get unique cache keys for different arguments.""" |
| 13 | + call_count = 0 |
| 14 | + |
| 15 | + @cachier(stale_after=timedelta(seconds=500)) |
| 16 | + def get_data(*args): |
| 17 | + """Test function that accepts variadic arguments.""" |
| 18 | + nonlocal call_count |
| 19 | + call_count += 1 |
| 20 | + return f"Result for args: {args}, call #{call_count}" |
| 21 | + |
| 22 | + # Clear any existing cache |
| 23 | + get_data.clear_cache() |
| 24 | + call_count = 0 |
| 25 | + |
| 26 | + # Test 1: Call with different arguments should produce different cache entries |
| 27 | + result1 = get_data("print", "domains") |
| 28 | + assert call_count == 1 |
| 29 | + assert "('print', 'domains')" in result1 |
| 30 | + |
| 31 | + result2 = get_data("print", "users", "allfields") |
| 32 | + assert call_count == 2, "Function should be called again with different args" |
| 33 | + assert "('print', 'users', 'allfields')" in result2 |
| 34 | + assert result1 != result2, "Different args should produce different results" |
| 35 | + |
| 36 | + # Test 2: Calling with the same arguments should use cache |
| 37 | + result3 = get_data("print", "domains") |
| 38 | + assert call_count == 2, "Function should not be called again (cache hit)" |
| 39 | + assert result3 == result1 |
| 40 | + |
| 41 | + result4 = get_data("print", "users", "allfields") |
| 42 | + assert call_count == 2, "Function should not be called again (cache hit)" |
| 43 | + assert result4 == result2 |
| 44 | + |
| 45 | + get_data.clear_cache() |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.pickle |
| 49 | +def test_varargs_empty(): |
| 50 | + """Test that functions with *args work with no arguments.""" |
| 51 | + call_count = 0 |
| 52 | + |
| 53 | + @cachier(stale_after=timedelta(seconds=500)) |
| 54 | + def get_data(*args): |
| 55 | + """Test function that accepts variadic arguments.""" |
| 56 | + nonlocal call_count |
| 57 | + call_count += 1 |
| 58 | + return f"Result for args: {args}, call #{call_count}" |
| 59 | + |
| 60 | + get_data.clear_cache() |
| 61 | + call_count = 0 |
| 62 | + |
| 63 | + # Call with no arguments |
| 64 | + result1 = get_data() |
| 65 | + assert call_count == 1 |
| 66 | + assert "()" in result1 |
| 67 | + |
| 68 | + # Second call should use cache |
| 69 | + result2 = get_data() |
| 70 | + assert call_count == 1 |
| 71 | + assert result2 == result1 |
| 72 | + |
| 73 | + get_data.clear_cache() |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.pickle |
| 77 | +def test_varargs_with_regular_args(): |
| 78 | + """Test that functions with both regular and variadic arguments work correctly.""" |
| 79 | + call_count = 0 |
| 80 | + |
| 81 | + @cachier(stale_after=timedelta(seconds=500)) |
| 82 | + def get_data(command, *args): |
| 83 | + """Test function with regular and variadic arguments.""" |
| 84 | + nonlocal call_count |
| 85 | + call_count += 1 |
| 86 | + return f"Command: {command}, args: {args}, call #{call_count}" |
| 87 | + |
| 88 | + get_data.clear_cache() |
| 89 | + call_count = 0 |
| 90 | + |
| 91 | + # Test different calls |
| 92 | + result1 = get_data("print", "domains") |
| 93 | + assert call_count == 1 |
| 94 | + |
| 95 | + result2 = get_data("print", "users", "allfields") |
| 96 | + assert call_count == 2 |
| 97 | + assert result1 != result2 |
| 98 | + |
| 99 | + result3 = get_data("list") |
| 100 | + assert call_count == 3 |
| 101 | + assert result3 != result1 |
| 102 | + assert result3 != result2 |
| 103 | + |
| 104 | + # Test cache hits |
| 105 | + result4 = get_data("print", "domains") |
| 106 | + assert call_count == 3 |
| 107 | + assert result4 == result1 |
| 108 | + |
| 109 | + get_data.clear_cache() |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.pickle |
| 113 | +def test_varkwargs_different_cache_keys(): |
| 114 | + """Test that functions with **kwargs get unique cache keys for different arguments.""" |
| 115 | + call_count = 0 |
| 116 | + |
| 117 | + @cachier(stale_after=timedelta(seconds=500)) |
| 118 | + def get_data(**kwargs): |
| 119 | + """Test function that accepts keyword variadic arguments.""" |
| 120 | + nonlocal call_count |
| 121 | + call_count += 1 |
| 122 | + return f"Result for kwargs: {kwargs}, call #{call_count}" |
| 123 | + |
| 124 | + get_data.clear_cache() |
| 125 | + call_count = 0 |
| 126 | + |
| 127 | + # Test with different kwargs |
| 128 | + result1 = get_data(type="domains", action="print") |
| 129 | + assert call_count == 1 |
| 130 | + |
| 131 | + result2 = get_data(type="users", action="print", fields="allfields") |
| 132 | + assert call_count == 2 |
| 133 | + assert result1 != result2 |
| 134 | + |
| 135 | + # Test cache hits |
| 136 | + result3 = get_data(type="domains", action="print") |
| 137 | + assert call_count == 2 |
| 138 | + assert result3 == result1 |
| 139 | + |
| 140 | + get_data.clear_cache() |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.pickle |
| 144 | +def test_varargs_and_varkwargs(): |
| 145 | + """Test that functions with both *args and **kwargs work correctly.""" |
| 146 | + call_count = 0 |
| 147 | + |
| 148 | + @cachier(stale_after=timedelta(seconds=500)) |
| 149 | + def get_data(*args, **kwargs): |
| 150 | + """Test function with both variadic arguments.""" |
| 151 | + nonlocal call_count |
| 152 | + call_count += 1 |
| 153 | + return f"args: {args}, kwargs: {kwargs}, call #{call_count}" |
| 154 | + |
| 155 | + get_data.clear_cache() |
| 156 | + call_count = 0 |
| 157 | + |
| 158 | + # Test different combinations |
| 159 | + result1 = get_data("print", "domains") |
| 160 | + assert call_count == 1 |
| 161 | + |
| 162 | + result2 = get_data("print", "users", action="list") |
| 163 | + assert call_count == 2 |
| 164 | + assert result1 != result2 |
| 165 | + |
| 166 | + result3 = get_data(action="list", resource="domains") |
| 167 | + assert call_count == 3 |
| 168 | + assert result3 != result1 |
| 169 | + assert result3 != result2 |
| 170 | + |
| 171 | + # Test cache hits |
| 172 | + result4 = get_data("print", "domains") |
| 173 | + assert call_count == 3 |
| 174 | + assert result4 == result1 |
| 175 | + |
| 176 | + get_data.clear_cache() |
| 177 | + |
| 178 | + |
| 179 | +@pytest.mark.memory |
| 180 | +def test_varargs_memory_backend(): |
| 181 | + """Test that variadic arguments work with memory backend.""" |
| 182 | + call_count = 0 |
| 183 | + |
| 184 | + @cachier(backend="memory", stale_after=timedelta(seconds=500)) |
| 185 | + def get_data(*args): |
| 186 | + """Test function that accepts variadic arguments.""" |
| 187 | + nonlocal call_count |
| 188 | + call_count += 1 |
| 189 | + return f"Result: {args}, call #{call_count}" |
| 190 | + |
| 191 | + get_data.clear_cache() |
| 192 | + call_count = 0 |
| 193 | + |
| 194 | + result1 = get_data("a", "b") |
| 195 | + assert call_count == 1 |
| 196 | + |
| 197 | + result2 = get_data("a", "b", "c") |
| 198 | + assert call_count == 2 |
| 199 | + assert result1 != result2 |
| 200 | + |
| 201 | + result3 = get_data("a", "b") |
| 202 | + assert call_count == 2 |
| 203 | + assert result3 == result1 |
| 204 | + |
| 205 | + get_data.clear_cache() |
0 commit comments