MCPcopy
hub / github.com/grafana/dskit / TestMockKV_Txn

Function TestMockKV_Txn

kv/etcd/mock_test.go:191–300  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

189}
190
191func TestMockKV_Txn(t *testing.T) {
192 t.Run("success compare value", func(t *testing.T) {
193 kv := newMockKV()
194 kv.values["/foo"] = mvccpb.KeyValue{
195 Key: []byte("/foo"),
196 CreateRevision: 1,
197 ModRevision: 3,
198 Version: 3,
199 Value: []byte("1"),
200 }
201
202 res, err := kv.Txn(context.Background()).
203 If(clientv3.Compare(clientv3.Value("/foo"), "=", "1")).
204 Then(clientv3.OpPut("/foo", "2")).
205 Commit()
206
207 require.NoError(t, err)
208 assert.True(t, res.Succeeded)
209 assert.Equal(t, kv.values["/foo"].Value, []byte("2"))
210 })
211
212 t.Run("failure compare value", func(t *testing.T) {
213 kv := newMockKV()
214 kv.values["/foo"] = mvccpb.KeyValue{
215 Key: []byte("/foo"),
216 CreateRevision: 1,
217 ModRevision: 3,
218 Version: 3,
219 Value: []byte("2"),
220 }
221
222 res, err := kv.Txn(context.Background()).
223 If(clientv3.Compare(clientv3.Value("/foo"), "=", "3")).
224 Then(clientv3.OpPut("/foo", "4")).
225 Else(clientv3.OpPut("/foo", "-1")).
226 Commit()
227
228 require.NoError(t, err)
229 assert.False(t, res.Succeeded)
230 assert.Equal(t, kv.values["/foo"].Value, []byte("-1"))
231 })
232
233 t.Run("success compare version exists", func(t *testing.T) {
234 kv := newMockKV()
235 kv.values["/foo"] = mvccpb.KeyValue{
236 Key: []byte("/foo"),
237 CreateRevision: 1,
238 ModRevision: 3,
239 Version: 3,
240 Value: []byte("1"),
241 }
242
243 res, err := kv.Txn(context.Background()).
244 If(clientv3.Compare(clientv3.Version("/foo"), "=", 3)).
245 Then(clientv3.OpPut("/foo", "2")).
246 Commit()
247
248 require.NoError(t, err)

Callers

nothing calls this directly

Calls 8

newMockKVFunction · 0.85
RunMethod · 0.80
CommitMethod · 0.80
ThenMethod · 0.80
IfMethod · 0.80
TxnMethod · 0.80
ElseMethod · 0.80
EqualMethod · 0.45

Tested by

no test coverage detected