TestSetIFDNEWithDigest validates the SetIFDNE command works with digests
(t *testing.T)
| 138 | |
| 139 | // TestSetIFDNEWithDigest validates the SetIFDNE command works with digests |
| 140 | func TestSetIFDNEWithDigest(t *testing.T) { |
| 141 | skipIfRedisBelow84(t) |
| 142 | |
| 143 | ctx := context.Background() |
| 144 | client := redis.NewClient(&redis.Options{ |
| 145 | Addr: "localhost:6379", |
| 146 | }) |
| 147 | defer client.Close() |
| 148 | |
| 149 | if err := client.Ping(ctx).Err(); err != nil { |
| 150 | t.Skipf("Redis not available: %v", err) |
| 151 | } |
| 152 | |
| 153 | client.Del(ctx, "cad-test-key") |
| 154 | |
| 155 | // Set initial value |
| 156 | initialValue := "initial-value" |
| 157 | err := client.Set(ctx, "cad-test-key", initialValue, 0).Err() |
| 158 | if err != nil { |
| 159 | t.Fatalf("Failed to set initial value: %v", err) |
| 160 | } |
| 161 | |
| 162 | // Use an arbitrary different digest |
| 163 | wrongDigest := uint64(99999) // arbitrary different digest |
| 164 | |
| 165 | // Test 1: SetIFDNE with different digest should succeed |
| 166 | result := client.SetIFDNE(ctx, "cad-test-key", "new-value", wrongDigest, 0) |
| 167 | if err := result.Err(); err != nil { |
| 168 | t.Errorf("SetIFDNE with different digest failed: %v", err) |
| 169 | } else { |
| 170 | t.Logf("✓ SetIFDNE with different digest succeeded") |
| 171 | } |
| 172 | |
| 173 | // Verify value was updated |
| 174 | val, err := client.Get(ctx, "cad-test-key").Result() |
| 175 | if err != nil { |
| 176 | t.Fatalf("Failed to get value: %v", err) |
| 177 | } |
| 178 | if val != "new-value" { |
| 179 | t.Errorf("Value not updated: got %q, want %q", val, "new-value") |
| 180 | } |
| 181 | |
| 182 | // Test 2: SetIFDNE with matching digest should fail |
| 183 | newDigest := client.Digest(ctx, "cad-test-key").Val() |
| 184 | result = client.SetIFDNE(ctx, "cad-test-key", "another-value", newDigest, 0) |
| 185 | if result.Err() != redis.Nil { |
| 186 | t.Errorf("SetIFDNE with matching digest should return redis.Nil, got: %v", result.Err()) |
| 187 | } else { |
| 188 | t.Logf("✓ SetIFDNE with matching digest correctly failed") |
| 189 | } |
| 190 | |
| 191 | // Verify value was NOT updated |
| 192 | val, err = client.Get(ctx, "cad-test-key").Result() |
| 193 | if err != nil { |
| 194 | t.Fatalf("Failed to get value: %v", err) |
| 195 | } |
| 196 | if val != "new-value" { |
| 197 | t.Errorf("Value should not have changed: got %q, want %q", val, "new-value") |