From db48a8f31730fa92e1d3f90969ae6ca71588a1d0 Mon Sep 17 00:00:00 2001 From: Noble Mittal Date: Thu, 18 Jan 2024 02:05:19 +0530 Subject: [PATCH 1/5] Add required tests for go/mysql/hex Signed-off-by: Noble Mittal --- go/mysql/hex/hex_test.go | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 go/mysql/hex/hex_test.go diff --git a/go/mysql/hex/hex_test.go b/go/mysql/hex/hex_test.go new file mode 100644 index 00000000000..b4fa19ab0ac --- /dev/null +++ b/go/mysql/hex/hex_test.go @@ -0,0 +1,102 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package hex + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEncodeBytes(t *testing.T) { + testCases := []struct { + input []byte + want []byte + }{ + {[]byte{0xAB, 0xCD, 0xEF}, []byte("ABCDEF")}, + {[]byte{0x01, 0x23, 0x45}, []byte("012345")}, + } + + for _, tCase := range testCases { + got := EncodeBytes(tCase.input) + assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) + } +} + +func TestEncodeUint(t *testing.T) { + testCases := []struct { + input uint64 + want []byte + }{ + {0, []byte("0")}, + {123, []byte("7B")}, + {255, []byte("FF")}, + {4096, []byte("1000")}, + } + + for _, tCase := range testCases { + got := EncodeUint(tCase.input) + assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) + } +} + +func TestDecodeUint(t *testing.T) { + testCases := []struct { + input uint64 + want []byte + }{ + {0, []byte{0}}, + {123, []byte{0x01, 0x23}}, + {255, []byte{0x02, 0x55}}, + {4096, []byte{0x40, 0x96}}, + } + + for _, tCase := range testCases { + got := DecodeUint(tCase.input) + assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) + } +} + +func TestDecodedLen(t *testing.T) { + testCases := []struct { + input []byte + want int + }{ + {[]byte{0}, 1}, + {[]byte{0x01, 0x23}, 1}, + {[]byte("ABCDE"), 3}, + {[]byte("0123456789ABCDEF"), 8}, + } + + for _, tCase := range testCases { + got := DecodedLen(tCase.input) + assert.Equalf(t, tCase.want, got, "got %d, want %d", got, tCase.want) + } +} + +func TestDecodeBytes(t *testing.T) { + err := DecodeBytes([]byte("testDst"), []byte("1")) + assert.NoErrorf(t, err, "expected no error, got %v", err) + + err = DecodeBytes([]byte("testDst"), []byte("12")) + assert.NoErrorf(t, err, "expected no error, got %v", err) + + // DecodeBytes should return an error for "é" as + // hex.decode returns an error for non-ASCII characters + err = DecodeBytes([]byte("testDst"), []byte("é")) + assert.Errorf(t, err, "expected error to appear for invalid byte, got %v", err) +} From ecb3b2dd5bbbc01d78df0e6768872ed09ec325d2 Mon Sep 17 00:00:00 2001 From: Noble Mittal Date: Thu, 18 Jan 2024 15:39:40 +0530 Subject: [PATCH 2/5] Replace assert.Errorf with assert.Error with no message Signed-off-by: Noble Mittal --- go/mysql/hex/hex_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/go/mysql/hex/hex_test.go b/go/mysql/hex/hex_test.go index b4fa19ab0ac..425015b5645 100644 --- a/go/mysql/hex/hex_test.go +++ b/go/mysql/hex/hex_test.go @@ -33,7 +33,7 @@ func TestEncodeBytes(t *testing.T) { for _, tCase := range testCases { got := EncodeBytes(tCase.input) - assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) + assert.Equal(t, tCase.want, got) } } @@ -50,7 +50,7 @@ func TestEncodeUint(t *testing.T) { for _, tCase := range testCases { got := EncodeUint(tCase.input) - assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) + assert.Equal(t, tCase.want, got) } } @@ -67,7 +67,7 @@ func TestDecodeUint(t *testing.T) { for _, tCase := range testCases { got := DecodeUint(tCase.input) - assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) + assert.Equal(t, tCase.want, got) } } @@ -84,16 +84,16 @@ func TestDecodedLen(t *testing.T) { for _, tCase := range testCases { got := DecodedLen(tCase.input) - assert.Equalf(t, tCase.want, got, "got %d, want %d", got, tCase.want) + assert.Equal(t, tCase.want, got) } } func TestDecodeBytes(t *testing.T) { err := DecodeBytes([]byte("testDst"), []byte("1")) - assert.NoErrorf(t, err, "expected no error, got %v", err) + assert.NoError(t, err) err = DecodeBytes([]byte("testDst"), []byte("12")) - assert.NoErrorf(t, err, "expected no error, got %v", err) + assert.NoError(t, err) // DecodeBytes should return an error for "é" as // hex.decode returns an error for non-ASCII characters From 0c4252c8f6b89c5891caef6b3a66a522040b29e2 Mon Sep 17 00:00:00 2001 From: Noble Mittal Date: Thu, 18 Jan 2024 16:52:19 +0530 Subject: [PATCH 3/5] Replace assert.Errorf with assert.Error Signed-off-by: Noble Mittal --- go/mysql/hex/hex_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/mysql/hex/hex_test.go b/go/mysql/hex/hex_test.go index 425015b5645..afd94e306e3 100644 --- a/go/mysql/hex/hex_test.go +++ b/go/mysql/hex/hex_test.go @@ -98,5 +98,5 @@ func TestDecodeBytes(t *testing.T) { // DecodeBytes should return an error for "é" as // hex.decode returns an error for non-ASCII characters err = DecodeBytes([]byte("testDst"), []byte("é")) - assert.Errorf(t, err, "expected error to appear for invalid byte, got %v", err) + assert.Error(t, err) } From fc8d15beced06d83a8dba9ac10edfc4e203df8aa Mon Sep 17 00:00:00 2001 From: Noble Mittal Date: Thu, 18 Jan 2024 16:55:47 +0530 Subject: [PATCH 4/5] Add error message in assert.Error Signed-off-by: Noble Mittal --- go/mysql/hex/hex_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/mysql/hex/hex_test.go b/go/mysql/hex/hex_test.go index afd94e306e3..d0f4e221145 100644 --- a/go/mysql/hex/hex_test.go +++ b/go/mysql/hex/hex_test.go @@ -98,5 +98,5 @@ func TestDecodeBytes(t *testing.T) { // DecodeBytes should return an error for "é" as // hex.decode returns an error for non-ASCII characters err = DecodeBytes([]byte("testDst"), []byte("é")) - assert.Error(t, err) + assert.Error(t, err, "DecodeBytes() should have errored as src contains non-ASCII character") } From 76ef13a6ff1bffe8201e733c811c22b4e378d781 Mon Sep 17 00:00:00 2001 From: Noble Mittal Date: Thu, 18 Jan 2024 17:56:22 +0530 Subject: [PATCH 5/5] Remove the custom message from assert.Error Signed-off-by: Noble Mittal --- go/mysql/hex/hex_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/mysql/hex/hex_test.go b/go/mysql/hex/hex_test.go index d0f4e221145..afd94e306e3 100644 --- a/go/mysql/hex/hex_test.go +++ b/go/mysql/hex/hex_test.go @@ -98,5 +98,5 @@ func TestDecodeBytes(t *testing.T) { // DecodeBytes should return an error for "é" as // hex.decode returns an error for non-ASCII characters err = DecodeBytes([]byte("testDst"), []byte("é")) - assert.Error(t, err, "DecodeBytes() should have errored as src contains non-ASCII character") + assert.Error(t, err) }