Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add tests for go/ioutil and refactor existing #15885

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions go/hack/hack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,30 @@ package hack

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestByteToString(t *testing.T) {
v1 := []byte("1234")
if s := String(v1); s != "1234" {
t.Errorf("String(\"1234\"): %q, want 1234", s)
}
s := String(v1)
assert.Equal(t, "1234", s)

v1 = []byte("")
if s := String(v1); s != "" {
t.Errorf("String(\"\"): %q, want empty", s)
}
s = String(v1)
assert.Equal(t, "", s)

v1 = nil
if s := String(v1); s != "" {
t.Errorf("String(\"\"): %q, want empty", s)
}
s = String(v1)
assert.Equal(t, "", s)
}

func TestStringToByte(t *testing.T) {
s := "1234"
b := StringBytes(s)
assert.Equal(t, []byte("1234"), b)

s = ""
b = StringBytes(s)
assert.Nil(t, b)
}
58 changes: 58 additions & 0 deletions go/ioutil/meter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 ioutil

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

var (
calledBytes int
calledDuration time.Duration
)

func testfn(b int, d time.Duration) {
calledBytes = b
calledDuration = d
}

func TestMeter(t *testing.T) {
tm := meter{
fs: []func(b int, d time.Duration){testfn},
bytes: 123,
duration: time.Second,
}

assert.Equal(t, int64(123), tm.Bytes())
assert.Equal(t, time.Second, tm.Duration())

tf := func(p []byte) (int, error) {
return 1, nil
}

b, err := tm.measure(tf, []byte(""))
wantDuration := time.Second + calledDuration
wantBytes := int64(123) + int64(calledBytes)

assert.NoError(t, err)
assert.Equal(t, 1, b)
assert.Equal(t, wantDuration, tm.duration)
assert.Equal(t, wantBytes, tm.bytes)
}
55 changes: 55 additions & 0 deletions go/ioutil/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 ioutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

type mockReadCloser struct{}

func (*mockReadCloser) Read([]byte) (int, error) {
return 2, nil
}

func (*mockReadCloser) Close() error {
return nil
}

type mockRead struct{}

func (*mockRead) Read([]byte) (int, error) {
return 3, nil
}

func TestMeteredReader(t *testing.T) {
mrc := NewMeteredReadCloser(&mockReadCloser{}, testfn)
n, err := mrc.Read([]byte(""))
assert.NoError(t, err)
assert.Equal(t, 2, n)
assert.Equal(t, 2, calledBytes)
assert.Equal(t, calledDuration, mrc.Duration())

mr := NewMeteredReader(&mockRead{}, testfn)
n, err = mr.Read([]byte(""))
assert.NoError(t, err)
assert.Equal(t, 3, n)
assert.Equal(t, 3, calledBytes)
assert.Equal(t, calledDuration, mr.Duration())
}
55 changes: 55 additions & 0 deletions go/ioutil/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 ioutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

type mockWriteCloser struct{}

func (*mockWriteCloser) Write([]byte) (int, error) {
return 2, nil
}

func (*mockWriteCloser) Close() error {
return nil
}

type mockWrite struct{}

func (*mockWrite) Write([]byte) (int, error) {
return 3, nil
}

func TestMeteredWriter(t *testing.T) {
mwc := NewMeteredWriteCloser(&mockWriteCloser{}, testfn)
n, err := mwc.Write([]byte(""))
assert.NoError(t, err)
assert.Equal(t, 2, n)
assert.Equal(t, 2, calledBytes)
assert.Equal(t, calledDuration, mwc.Duration())

mw := NewMeteredWriter(&mockWrite{}, testfn)
n, err = mw.Write([]byte(""))
assert.NoError(t, err)
assert.Equal(t, 3, n)
assert.Equal(t, 3, calledBytes)
assert.Equal(t, calledDuration, mw.Duration())
}
10 changes: 4 additions & 6 deletions go/jsonutil/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package jsonutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMarshalNoEscape(t *testing.T) {
Expand Down Expand Up @@ -53,9 +55,7 @@ func TestMarshalNoEscape(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
json, _ := MarshalNoEscape(c.v)
sjson := string(json[:len(json)-1])
if sjson != c.expected {
t.Errorf("expected: %v, got: %v", c.expected, sjson)
}
assert.Equal(t, c.expected, sjson)
})
}
}
Expand Down Expand Up @@ -97,9 +97,7 @@ func TestMarshalIndentNoEscape(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
json, _ := MarshalIndentNoEscape(c.v, c.prefix, c.ident)
sjson := string(json[:len(json)-1])
if sjson != c.expected {
t.Errorf("expected: %v, got: %v", c.expected, sjson)
}
assert.Equal(t, c.expected, sjson)
})
}
}
Loading