From 561a32db6f37f4ace3ce0675e490c0f5d2fafc73 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:48:26 +0530 Subject: [PATCH 1/4] tests: improve coverage for go/bytes2/buffer.go Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- go/bytes2/buffer_test.go | 48 +++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/go/bytes2/buffer_test.go b/go/bytes2/buffer_test.go index 83cdb346ec9..fa72a223fa8 100644 --- a/go/bytes2/buffer_test.go +++ b/go/bytes2/buffer_test.go @@ -16,23 +16,45 @@ limitations under the License. package bytes2 -import ( - "testing" -) +import "testing" + +// Checks if the actual value is equal to the expected value for the given function +func assertEqual(t *testing.T, got any, want any, funcName string) { + if got != want { + t.Errorf("%s: got %v, want %v", funcName, got, want) + } +} func TestBuffer(t *testing.T) { + // Initialize a new buffer b := NewBuffer(nil) + + // Test Write function b.Write([]byte("ab")) + assertEqual(t, string(b.Bytes()), "ab", "Write()") + + // Test WriteString function b.WriteString("cd") + assertEqual(t, string(b.Bytes()), "abcd", "WriteString()") + + // Test WriteByte function b.WriteByte('e') - want := "abcde" - if got := string(b.Bytes()); got != want { - t.Errorf("b.Bytes(): %s, want %s", got, want) - } - if got := b.String(); got != want { - t.Errorf("b.String(): %s, want %s", got, want) - } - if got := b.Len(); got != 5 { - t.Errorf("b.Len(): %d, want 5", got) - } + assertEqual(t, string(b.Bytes()), "abcde", "WriteByte()") + + // Test Bytes function + assertEqual(t, string(b.Bytes()), "abcde", "Bytes()") + + // Test String function + assertEqual(t, b.String(), "abcde", "String()") + + // Test StringUnsafe function + assertEqual(t, b.StringUnsafe(), "abcde", "StringUnsafe()") + + // Test Len function + assertEqual(t, b.Len(), 5, "Len()") + + // Test Reset function + b.Reset() + assertEqual(t, string(b.Bytes()), "", "Reset()") + assertEqual(t, b.Len(), 0, "Reset() - Len()") } From 9f59827d3fa7eea1df0784024b9b389ea4026666 Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:51:41 +0530 Subject: [PATCH 2/4] refac: make use of testify/assert Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- go/bytes2/buffer_test.go | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/go/bytes2/buffer_test.go b/go/bytes2/buffer_test.go index fa72a223fa8..a3e51d32375 100644 --- a/go/bytes2/buffer_test.go +++ b/go/bytes2/buffer_test.go @@ -16,45 +16,41 @@ limitations under the License. package bytes2 -import "testing" +import ( + "testing" -// Checks if the actual value is equal to the expected value for the given function -func assertEqual(t *testing.T, got any, want any, funcName string) { - if got != want { - t.Errorf("%s: got %v, want %v", funcName, got, want) - } -} + "github.com/stretchr/testify/assert" +) func TestBuffer(t *testing.T) { - // Initialize a new buffer b := NewBuffer(nil) // Test Write function b.Write([]byte("ab")) - assertEqual(t, string(b.Bytes()), "ab", "Write()") + assert.Equal(t, "ab", string(b.Bytes()), "Write()") // Test WriteString function b.WriteString("cd") - assertEqual(t, string(b.Bytes()), "abcd", "WriteString()") + assert.Equal(t, "abcd", string(b.Bytes()), "WriteString()") // Test WriteByte function b.WriteByte('e') - assertEqual(t, string(b.Bytes()), "abcde", "WriteByte()") + assert.Equal(t, "abcde", string(b.Bytes()), "WriteByte()") // Test Bytes function - assertEqual(t, string(b.Bytes()), "abcde", "Bytes()") + assert.Equal(t, "abcde", string(b.Bytes()), "Bytes()") // Test String function - assertEqual(t, b.String(), "abcde", "String()") + assert.Equal(t, "abcde", b.String(), "String()") // Test StringUnsafe function - assertEqual(t, b.StringUnsafe(), "abcde", "StringUnsafe()") + assert.Equal(t, "abcde", b.StringUnsafe(), "StringUnsafe()") // Test Len function - assertEqual(t, b.Len(), 5, "Len()") + assert.Equal(t, 5, b.Len(), "Len()") // Test Reset function b.Reset() - assertEqual(t, string(b.Bytes()), "", "Reset()") - assertEqual(t, b.Len(), 0, "Reset() - Len()") + assert.Equal(t, "", string(b.Bytes()), "Reset()") + assert.Equal(t, 0, b.Len(), "Reset(): Len()") } From 75589af102bf1896d12779c6cf0880436353ac6d Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:01:38 +0530 Subject: [PATCH 3/4] refac: address review Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- go/bytes2/buffer_test.go | 12 ++++++------ go/vt/vtctl/workflow/switcher_dry_run.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/go/bytes2/buffer_test.go b/go/bytes2/buffer_test.go index a3e51d32375..1652f176df4 100644 --- a/go/bytes2/buffer_test.go +++ b/go/bytes2/buffer_test.go @@ -38,19 +38,19 @@ func TestBuffer(t *testing.T) { assert.Equal(t, "abcde", string(b.Bytes()), "WriteByte()") // Test Bytes function - assert.Equal(t, "abcde", string(b.Bytes()), "Bytes()") + assert.Equal(t, "abcde", string(b.Bytes())) // Test String function - assert.Equal(t, "abcde", b.String(), "String()") + assert.Equal(t, "abcde", b.String()) // Test StringUnsafe function - assert.Equal(t, "abcde", b.StringUnsafe(), "StringUnsafe()") + assert.Equal(t, "abcde", b.StringUnsafe()) // Test Len function - assert.Equal(t, 5, b.Len(), "Len()") + assert.Equal(t, 5, b.Len()) // Test Reset function b.Reset() - assert.Equal(t, "", string(b.Bytes()), "Reset()") - assert.Equal(t, 0, b.Len(), "Reset(): Len()") + assert.Equal(t, "", string(b.Bytes())) + assert.Equal(t, 0, b.Len()) } diff --git a/go/vt/vtctl/workflow/switcher_dry_run.go b/go/vt/vtctl/workflow/switcher_dry_run.go index 1932e48fe20..cae26160ad6 100644 --- a/go/vt/vtctl/workflow/switcher_dry_run.go +++ b/go/vt/vtctl/workflow/switcher_dry_run.go @@ -24,7 +24,7 @@ import ( "strings" "time" - "vitess.io/vitess/go/maps2" + "golang.org/x/exp/maps" "vitess.io/vitess/go/mysql/replication" binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" @@ -380,7 +380,7 @@ func (dr *switcherDryRun) resetSequences(ctx context.Context) error { } func (dr *switcherDryRun) initializeTargetSequences(ctx context.Context, sequencesByBackingTable map[string]*sequenceMetadata) error { - sortedBackingTableNames := maps2.Keys(sequencesByBackingTable) + sortedBackingTableNames := maps.Keys(sequencesByBackingTable) slices.Sort(sortedBackingTableNames) dr.drLog.Log(fmt.Sprintf("The following sequence backing tables used by tables being moved will be initialized: %s", strings.Join(sortedBackingTableNames, ","))) From 49f5312544d3863439a9834adadef0f7796e7c6e Mon Sep 17 00:00:00 2001 From: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> Date: Fri, 19 Jan 2024 13:22:20 +0530 Subject: [PATCH 4/4] revert buffer tests Signed-off-by: Eshaan Aggarwal <96648934+EshaanAgg@users.noreply.github.com> --- go/bytes2/buffer_test.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/go/bytes2/buffer_test.go b/go/bytes2/buffer_test.go index 8cabe6273f1..1652f176df4 100644 --- a/go/bytes2/buffer_test.go +++ b/go/bytes2/buffer_test.go @@ -35,20 +35,22 @@ func TestBuffer(t *testing.T) { // Test WriteByte function b.WriteByte('e') - want := "abcde" - if got := string(b.Bytes()); got != want { - t.Errorf("b.Bytes(): %s, want %s", got, want) - } - if got := b.String(); got != want { - t.Errorf("b.String(): %s, want %s", got, want) - } - if got := b.Len(); got != 5 { - t.Errorf("b.Len(): %d, want 5", got) - } - if got := b.StringUnsafe(); got != want { - t.Errorf("b.StringUnsafe(): %s, want %s", got, want) - } - if b.Reset(); len(b.bytes) != 0 { - t.Errorf("b.Reset(): got %s, want \"\"", b.bytes) - } + assert.Equal(t, "abcde", string(b.Bytes()), "WriteByte()") + + // Test Bytes function + assert.Equal(t, "abcde", string(b.Bytes())) + + // Test String function + assert.Equal(t, "abcde", b.String()) + + // Test StringUnsafe function + assert.Equal(t, "abcde", b.StringUnsafe()) + + // Test Len function + assert.Equal(t, 5, b.Len()) + + // Test Reset function + b.Reset() + assert.Equal(t, "", string(b.Bytes())) + assert.Equal(t, 0, b.Len()) }