github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvnemesis/operations_test.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package kvnemesis 12 13 import ( 14 "strings" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestOperationsFormat(t *testing.T) { 22 defer leaktest.AfterTest(t)() 23 24 tests := []struct { 25 step Step 26 expected string 27 }{ 28 {step: step(get(`a`)), expected: `db0.Get(ctx, "a")`}, 29 {step: step(batch(get(`b`), get(`c`))), expected: ` 30 { 31 b := &Batch{} 32 b.Get(ctx, "b") 33 b.Get(ctx, "c") 34 db0.Run(ctx, b) 35 } 36 `}, 37 { 38 step: step(closureTxn(ClosureTxnType_Commit, batch(get(`d`), get(`e`)), put(`f`, `g`))), 39 expected: ` 40 db0.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { 41 { 42 b := &Batch{} 43 b.Get(ctx, "d") 44 b.Get(ctx, "e") 45 txn.Run(ctx, b) 46 } 47 txn.Put(ctx, "f", g) 48 return nil 49 }) 50 `, 51 }, 52 } 53 54 for _, test := range tests { 55 expected := strings.TrimSpace(test.expected) 56 var actual strings.Builder 57 test.step.format(&actual, formatCtx{indent: "\t\t\t"}) 58 assert.Equal(t, expected, strings.TrimSpace(actual.String())) 59 } 60 }