Skip to content

Commit

Permalink
misc: update assert to require (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish authored Jan 11, 2022
1 parent 4a262b3 commit 28d36cd
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 398 deletions.
165 changes: 82 additions & 83 deletions util/client/httpclient/response_test.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions util/client/pdclient/pd_api_highlevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
func TestAPIClient_HLGetLocationLabels(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.HLGetLocationLabels(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, []string{}, resp)
}

func TestAPIClient_HLGetStoreLocations(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.HLGetStoreLocations(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, &pdclient.StoreLocations{
LocationLabels: []string{},
Stores: []pdclient.StoreLabels{
Expand All @@ -36,7 +36,7 @@ func TestAPIClient_HLGetStoreLocations(t *testing.T) {
func TestAPIClient_HLGetStores(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.HLGetStores(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, []pdclient.GetStoresResponseStore{
pdclient.GetStoresResponseStore{
Address: "172.16.5.141:20160",
Expand Down
10 changes: 5 additions & 5 deletions util/client/pdclient/pd_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestAPIClient_GetConfigReplicate(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.GetConfigReplicate(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, &pdclient.GetConfigReplicateResponse{
LocationLabels: "",
}, resp)
Expand All @@ -24,7 +24,7 @@ func TestAPIClient_GetConfigReplicate(t *testing.T) {
func TestAPIClient_GetHealth(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.GetHealth(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, &pdclient.GetHealthResponse{
pdclient.GetHealthResponseMember{MemberID: 0x28cb7236f465dbeb, Health: true},
pdclient.GetHealthResponseMember{MemberID: 0x79cc97f3bcb16deb, Health: true},
Expand All @@ -35,7 +35,7 @@ func TestAPIClient_GetHealth(t *testing.T) {
func TestAPIClient_GetMembers(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.GetMembers(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, &pdclient.GetMembersResponse{
Members: []pdclient.GetMembersResponseMember{
pdclient.GetMembersResponseMember{
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestAPIClient_GetMembers(t *testing.T) {
func TestAPIClient_GetStatus(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.GetStatus(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, &pdclient.GetStatusResponse{
StartTimestamp: 1635762685,
}, resp)
Expand All @@ -73,7 +73,7 @@ func TestAPIClient_GetStatus(t *testing.T) {
func TestAPIClient_GetStores(t *testing.T) {
apiClient := fixture.NewAPIAPIClientFixture()
resp, err := apiClient.GetStores(context.Background())
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, &pdclient.GetStoresResponse{
Stores: []pdclient.GetStoresResponseStoresElem{
pdclient.GetStoresResponseStoresElem{
Expand Down
20 changes: 10 additions & 10 deletions util/client/tidbclient/tidbproto/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package tidbproto
import (
"testing"

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

func TestDecodeBytes(t *testing.T) {
key := "abcdefghijklmnopqrstuvwxyz"
for i := 0; i < len(key); i++ {
_, k, err := decodeBytes(encodeBytes([]byte(key[:i])), nil)
assert.Nil(t, err)
assert.Equal(t, string(k), key[:i])
require.NoError(t, err)
require.Equal(t, string(k), key[:i])
}
}

Expand All @@ -22,7 +22,7 @@ func TestTiDBInfo(t *testing.T) {

// no encode
_, err := buf.DecodeKey([]byte("t\x80\x00\x00\x00\x00\x00\x00\xff"))
assert.NotNil(t, err)
require.Error(t, err)

testcases := []struct {
Key string
Expand Down Expand Up @@ -93,14 +93,14 @@ func TestTiDBInfo(t *testing.T) {
for _, testcase := range testcases {
key := encodeBytes([]byte(testcase.Key))
_, err := buf.DecodeKey(key)
assert.Nil(t, err)
require.NoError(t, err)
isMeta, tableID := buf.MetaOrTable()
assert.Equal(t, testcase.IsMeta, isMeta)
assert.Equal(t, testcase.TableID, tableID)
require.Equal(t, testcase.IsMeta, isMeta)
require.Equal(t, testcase.TableID, tableID)
isCommonHandle, rowID := buf.RowInfo()
assert.Equal(t, testcase.IsCommonHandle, isCommonHandle)
assert.Equal(t, testcase.RowID, rowID)
require.Equal(t, testcase.IsCommonHandle, isCommonHandle)
require.Equal(t, testcase.RowID, rowID)
indexID := buf.IndexInfo()
assert.Equal(t, testcase.IndexID, indexID)
require.Equal(t, testcase.IndexID, indexID)
}
}
18 changes: 9 additions & 9 deletions util/csvutil/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

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

type foo struct {
Expand All @@ -27,7 +27,7 @@ func TestCSVWriter(t *testing.T) {
buf := bytes.Buffer{}
w := NewCSVWriter(&buf)
err := w.WriteAsHeader(&foo{})
assert.Nil(t, err)
require.NoError(t, err)

rec := foo{
Digest: "digest_foo",
Expand All @@ -42,7 +42,7 @@ func TestCSVWriter(t *testing.T) {
privateField: "pfxyz",
}
err = w.WriteAsRow(&rec)
assert.Nil(t, err)
require.NoError(t, err)

rec = foo{
ConnectionID: "id123",
Expand All @@ -57,15 +57,15 @@ func TestCSVWriter(t *testing.T) {
DB: "dbAbc",
}
err = w.WriteAsRow(&rec)
assert.Nil(t, err)
require.NoError(t, err)

w.Flush()
expected := `
Digest,Query,Instance,DB,ConnectionID,Success,Timestamp,TimestampAsTime,QueryTime
digest_foo,query_bar,instance_box,db_abc,id_123,1,456,2021-10-01 16:46:40 UTC,789
digestFoo,queryBar,instanceBox,dbAbc,id123,2,0,1970-01-01 00:00:00 UTC,123
`
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buf.String()))
require.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buf.String()))
}

func TestCSVWriterWriteTimeTag(t *testing.T) {
Expand All @@ -81,9 +81,9 @@ func TestCSVWriterWriteTimeTag(t *testing.T) {
buf := bytes.Buffer{}
w := NewCSVWriter(&buf)
err := w.WriteAsRow(fooStruct{})
assert.Nil(t, err)
require.NoError(t, err)
w.Flush()
assert.Equal(t, "0,0,0,0,0,1970-01-01 00:00:00 UTC\n", buf.String())
require.Equal(t, "0,0,0,0,0,1970-01-01 00:00:00 UTC\n", buf.String())

type barStruct struct {
FieldInt int `csv:",time"`
Expand All @@ -96,9 +96,9 @@ func TestCSVWriterWriteTimeTag(t *testing.T) {
FieldUint64: 1633106801,
FieldFloat32: 1633106802,
})
assert.Nil(t, err)
require.NoError(t, err)
w.Flush()
assert.Equal(t, "2021-10-01 16:46:40 UTC,2021-10-01 16:46:41 UTC,2021-10-01 16:46:42 UTC\n", buf.String())
require.Equal(t, "2021-10-01 16:46:40 UTC,2021-10-01 16:46:41 UTC,2021-10-01 16:46:42 UTC\n", buf.String())
}

func BenchmarkCSVWriterWriteAsHeader(b *testing.B) {
Expand Down
26 changes: 13 additions & 13 deletions util/distro/distro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ package distro
import (
"testing"

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

func TestR(t *testing.T) {
assert.Equal(t, "TiDB", R().TiDB)
require.Equal(t, "TiDB", R().TiDB)
}

func TestReplaceGlobal(t *testing.T) {
restoreFn := ReplaceGlobal(DistributionResource{
TiDB: "myTiDB",
PD: "",
})
assert.Equal(t, false, R().IsDistro)
assert.Equal(t, "myTiDB", R().TiDB)
assert.Equal(t, "PD", R().PD)
assert.Equal(t, "TiKV", R().TiKV)
require.Equal(t, false, R().IsDistro)
require.Equal(t, "myTiDB", R().TiDB)
require.Equal(t, "PD", R().PD)
require.Equal(t, "TiKV", R().TiKV)
restoreFn()
assert.Equal(t, "TiDB", R().TiDB)
assert.Equal(t, "PD", R().PD)
assert.Equal(t, "TiKV", R().TiKV)
require.Equal(t, "TiDB", R().TiDB)
require.Equal(t, "PD", R().PD)
require.Equal(t, "TiKV", R().TiKV)

restoreFn = ReplaceGlobal(DistributionResource{
IsDistro: true,
})
assert.Equal(t, true, R().IsDistro)
assert.Equal(t, "TiDB", R().TiDB)
assert.Equal(t, "PD", R().PD)
assert.Equal(t, "TiKV", R().TiKV)
require.Equal(t, true, R().IsDistro)
require.Equal(t, "TiDB", R().TiDB)
require.Equal(t, "PD", R().PD)
require.Equal(t, "TiKV", R().TiKV)
restoreFn()
}
16 changes: 8 additions & 8 deletions util/gormutil/datatype/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestIntJSON(t *testing.T) {
nv := Int(123)
v, err := json.Marshal(nv)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, string(v), "123")

st := struct {
Expand All @@ -21,28 +21,28 @@ func TestIntJSON(t *testing.T) {
Foo: Int(415425),
}
v, err = json.Marshal(st)
require.Nil(t, err)
require.NoError(t, err)
require.JSONEq(t, `{"Foo":415425}`, string(v))

var nv2 Int
err = json.Unmarshal([]byte("12345"), &nv2)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, Int(12345), nv2)

err = json.Unmarshal([]byte(`{"Foo":56789}`), &st)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, Int(56789), st.Foo)

err = json.Unmarshal([]byte(`{"Foo":"123"}`), &st)
require.NotNil(t, err)
require.Error(t, err)

err = json.Unmarshal([]byte(`{"Foo":1300.45}`), &st)
require.NotNil(t, err)
require.Error(t, err)

nv3 := Int(48691071)
v, err = json.Marshal(nv3)
require.Nil(t, err)
require.NoError(t, err)
err = json.Unmarshal(v, &nv2)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, nv2, nv3)
}
Loading

0 comments on commit 28d36cd

Please sign in to comment.