TestSetIFDEQWithDigest validates the SetIFDEQ command works with digests
(t *testing.T)
| 74 | |
| 75 | // TestSetIFDEQWithDigest validates the SetIFDEQ command works with digests |
| 76 | func TestSetIFDEQWithDigest(t *testing.T) { |
| 77 | skipIfRedisBelow84(t) |
| 78 | |
| 79 | ctx := context.Background() |
| 80 | client := redis.NewClient(&redis.Options{ |
| 81 | Addr: "localhost:6379", |
| 82 | }) |
| 83 | defer client.Close() |
| 84 | |
| 85 | if err := client.Ping(ctx).Err(); err != nil { |
| 86 | t.Skipf("Redis not available: %v", err) |
| 87 | } |
| 88 | |
| 89 | client.Del(ctx, "cas-test-key") |
| 90 | |
| 91 | // Set initial value |
| 92 | initialValue := "initial-value" |
| 93 | err := client.Set(ctx, "cas-test-key", initialValue, 0).Err() |
| 94 | if err != nil { |
| 95 | t.Fatalf("Failed to set initial value: %v", err) |
| 96 | } |
| 97 | |
| 98 | // Get current digest |
| 99 | correctDigest := client.Digest(ctx, "cas-test-key").Val() |
| 100 | wrongDigest := uint64(12345) // arbitrary wrong digest |
| 101 | |
| 102 | // Test 1: SetIFDEQ with correct digest should succeed |
| 103 | result := client.SetIFDEQ(ctx, "cas-test-key", "new-value", correctDigest, 0) |
| 104 | if err := result.Err(); err != nil { |
| 105 | t.Errorf("SetIFDEQ with correct digest failed: %v", err) |
| 106 | } else { |
| 107 | t.Logf("✓ SetIFDEQ with correct digest succeeded") |
| 108 | } |
| 109 | |
| 110 | // Verify value was updated |
| 111 | val, err := client.Get(ctx, "cas-test-key").Result() |
| 112 | if err != nil { |
| 113 | t.Fatalf("Failed to get value: %v", err) |
| 114 | } |
| 115 | if val != "new-value" { |
| 116 | t.Errorf("Value not updated: got %q, want %q", val, "new-value") |
| 117 | } |
| 118 | |
| 119 | // Test 2: SetIFDEQ with wrong digest should fail |
| 120 | result = client.SetIFDEQ(ctx, "cas-test-key", "another-value", wrongDigest, 0) |
| 121 | if result.Err() != redis.Nil { |
| 122 | t.Errorf("SetIFDEQ with wrong digest should return redis.Nil, got: %v", result.Err()) |
| 123 | } else { |
| 124 | t.Logf("✓ SetIFDEQ with wrong digest correctly failed") |
| 125 | } |
| 126 | |
| 127 | // Verify value was NOT updated |
| 128 | val, err = client.Get(ctx, "cas-test-key").Result() |
| 129 | if err != nil { |
| 130 | t.Fatalf("Failed to get value: %v", err) |
| 131 | } |
| 132 | if val != "new-value" { |
| 133 | t.Errorf("Value should not have changed: got %q, want %q", val, "new-value") |