MCPcopy
hub / github.com/redis/go-redis / TestSetIFDEQWithDigest

Function TestSetIFDEQWithDigest

digest_test.go:76–137  ·  view source on GitHub ↗

TestSetIFDEQWithDigest validates the SetIFDEQ command works with digests

(t *testing.T)

Source from the content-addressed store, hash-verified

74
75// TestSetIFDEQWithDigest validates the SetIFDEQ command works with digests
76func 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")

Callers

nothing calls this directly

Calls 11

skipIfRedisBelow84Function · 0.85
CloseMethod · 0.65
ErrMethod · 0.65
PingMethod · 0.65
DelMethod · 0.65
SetMethod · 0.65
DigestMethod · 0.65
SetIFDEQMethod · 0.65
ResultMethod · 0.65
GetMethod · 0.65
ValMethod · 0.45

Tested by

no test coverage detected