Skip to content
Open
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
36 changes: 14 additions & 22 deletions dm/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@ import (
"path"
"testing"

"github.com/pingcap/check"
"github.com/stretchr/testify/require"
)

func TestCommon(t *testing.T) {
check.TestingT(t)
}

type testCommon struct{}

var _ = check.Suite(&testCommon{})

func (t *testCommon) TestKeyAdapter(c *check.C) {
func TestKeyAdapter(t *testing.T) {
testCases := []struct {
keys []string
adapter KeyAdapter
Expand Down Expand Up @@ -84,14 +76,14 @@ func (t *testCommon) TestKeyAdapter(c *check.C) {

for _, ca := range testCases {
encKey := ca.adapter.Encode(ca.keys...)
c.Assert(encKey, check.Equals, ca.want)
require.Equal(t, ca.want, encKey)
decKey, err := ca.adapter.Decode(encKey)
c.Assert(err, check.IsNil)
c.Assert(decKey, check.DeepEquals, ca.keys)
require.NoError(t, err)
require.Equal(t, ca.keys, decKey)
}
}

func (t *testCommon) TestEncodeAsPrefix(c *check.C) {
func TestEncodeAsPrefix(t *testing.T) {
testCases := []struct {
keys []string
adapter KeyAdapter
Expand All @@ -106,22 +98,22 @@ func (t *testCommon) TestEncodeAsPrefix(c *check.C) {

for _, ca := range testCases {
encKey := ca.adapter.Encode(ca.keys...)
c.Assert(encKey, check.Equals, ca.want)
require.Equal(t, ca.want, encKey)
_, err := ca.adapter.Decode(encKey)
c.Assert(err, check.NotNil)
require.NotNil(t, err)
}
}

func (t *testCommon) TestIsErrNetClosing(c *check.C) {
func TestIsErrNetClosing(t *testing.T) {
server, err := net.Listen("tcp", "localhost:0")
c.Assert(err, check.IsNil)
require.NoError(t, err)
err = server.Close()
c.Assert(IsErrNetClosing(err), check.IsFalse)
require.False(t, IsErrNetClosing(err))
_, err = server.Accept()
c.Assert(IsErrNetClosing(err), check.IsTrue)
require.True(t, IsErrNetClosing(err))
}

func (t *testCommon) TestJoinUseSlash(c *check.C) {
func TestJoinUseSlash(t *testing.T) {
// because we use "/" in Encode
c.Assert(path.Join("a", "b"), check.Equals, "a/b")
require.Equal(t, "a/b", path.Join("a", "b"))
}
20 changes: 6 additions & 14 deletions dm/config/source_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@ package config
import (
"testing"

"github.com/pingcap/check"
"github.com/pingcap/tiflow/dm/openapi/fixtures"
"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
check.TestingT(t)
}

type testConfig struct{}

var _ = check.Suite(&testConfig{})

func (t *testConfig) TestConverterWithSourceAndOpenAPISource(c *check.C) {
func TestConverterWithSourceAndOpenAPISource(t *testing.T) {
sourceCfg1, err := SourceCfgFromYaml(SampleSourceConfig)
c.Assert(err, check.IsNil)
require.NoError(t, err)

// 1. test user create source from dmctl, after convert to openapi.Source then convert back to source config
sourceCfg2 := OpenAPISourceToSourceCfg(SourceCfgToOpenAPISource(sourceCfg1))
Expand All @@ -40,12 +32,12 @@ func (t *testConfig) TestConverterWithSourceAndOpenAPISource(c *check.C) {
sourceCfg2.From.MaxAllowedPacket = sourceCfg1.From.MaxAllowedPacket

// we only need to make sure the source config that user can see is the same as the source config that user create
c.Assert(sourceCfg1.String(), check.Equals, sourceCfg2.String())
require.Equal(t, sourceCfg2.String(), sourceCfg1.String())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The arguments to require.Equal are swapped. The expected value should be the first argument (sourceCfg1.String()), and the actual value should be the second argument (sourceCfg2.String()). Swapping them ensures that test failure messages are accurate and easy to read.

Suggested change
require.Equal(t, sourceCfg2.String(), sourceCfg1.String())
\trequire.Equal(t, sourceCfg1.String(), sourceCfg2.String())


// 2. test user create source from openapi, after convert to source config then convert back to openapi.Source
openapiSource1, err := fixtures.GenOpenAPISourceForTest()
c.Assert(err, check.IsNil)
require.NoError(t, err)
openapiSource2 := SourceCfgToOpenAPISource(OpenAPISourceToSourceCfg(openapiSource1))
openapiSource2.Password = openapiSource1.Password // we set passwd to "******" for privacy
c.Assert(openapiSource1, check.DeepEquals, openapiSource2)
require.Equal(t, openapiSource2, openapiSource1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The arguments to require.Equal are swapped. The expected value should be the first argument (openapiSource1), and the actual value should be the second argument (openapiSource2). Swapping them ensures that test failure messages are accurate and easy to read.

Suggested change
require.Equal(t, openapiSource2, openapiSource1)
\trequire.Equal(t, openapiSource1, openapiSource2)

}
37 changes: 19 additions & 18 deletions dm/config/task_cli_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ package config

import (
"encoding/json"
"testing"

"github.com/pingcap/check"
"github.com/stretchr/testify/require"
)

type testStruct struct {
Expand All @@ -29,46 +30,46 @@ func (t *testStruct) ToJSON() string {
return string(cfg)
}

func (t *testConfig) TestTaskCliArgsDowngrade(c *check.C) {
func TestTaskCliArgsDowngrade(t *testing.T) {
s := testStruct{
TaskCliArgs: TaskCliArgs{"123", "1s", "1s"},
FutureField: "456",
}
data := s.ToJSON()

expected := `{"start_time":"123","safe_mode_duration":"1s","wait_time_on_stop":"1s","future_field":"456"}`
c.Assert(data, check.Equals, expected)
require.Equal(t, expected, data)

afterDowngrade := &TaskCliArgs{}
c.Assert(afterDowngrade.Decode([]byte(data)), check.IsNil)
c.Assert(afterDowngrade.StartTime, check.Equals, "123")
require.NoError(t, afterDowngrade.Decode([]byte(data)))
require.Equal(t, "123", afterDowngrade.StartTime)
}

func (t *testConfig) TestTaskCliArgsVerify(c *check.C) {
func TestTaskCliArgsVerify(t *testing.T) {
empty := TaskCliArgs{}
c.Assert(empty.Verify(), check.IsNil)
require.NoError(t, empty.Verify())
rightStartTime := TaskCliArgs{StartTime: "2006-01-02T15:04:05"}
c.Assert(rightStartTime.Verify(), check.IsNil)
require.NoError(t, rightStartTime.Verify())
rightStartTime = TaskCliArgs{StartTime: "2006-01-02 15:04:05"}
c.Assert(rightStartTime.Verify(), check.IsNil)
require.NoError(t, rightStartTime.Verify())
rightStartTime = TaskCliArgs{StartTime: "2006-01-02T15:04:05+08:00"}
c.Assert(rightStartTime.Verify(), check.IsNil)
require.NoError(t, rightStartTime.Verify())
rightStartTime = TaskCliArgs{StartTime: "2006-01-02 15:04:05+08:00"}
c.Assert(rightStartTime.Verify(), check.IsNil)
require.NoError(t, rightStartTime.Verify())
rightStartTime = TaskCliArgs{StartTime: "2006-01-02T15:04:05+0800"}
c.Assert(rightStartTime.Verify(), check.IsNil)
require.NoError(t, rightStartTime.Verify())
rightStartTime = TaskCliArgs{StartTime: "2006-01-02 15:04:05+0800"}
c.Assert(rightStartTime.Verify(), check.IsNil)
require.NoError(t, rightStartTime.Verify())
wrongStartTime := TaskCliArgs{StartTime: "15:04:05"}
c.Assert(wrongStartTime.Verify(), check.NotNil)
require.Error(t, wrongStartTime.Verify())

rightSafeModeDuration := TaskCliArgs{SafeModeDuration: "1s"}
c.Assert(rightSafeModeDuration.Verify(), check.IsNil)
require.NoError(t, rightSafeModeDuration.Verify())
wrongSafeModeDuration := TaskCliArgs{SafeModeDuration: "1"}
c.Assert(wrongSafeModeDuration.Verify(), check.NotNil)
require.Error(t, wrongSafeModeDuration.Verify())

rightWaitTimeOnStop := TaskCliArgs{WaitTimeOnStop: "1s"}
c.Assert(rightWaitTimeOnStop.Verify(), check.IsNil)
require.NoError(t, rightWaitTimeOnStop.Verify())
wrongWaitTimeOnStop := TaskCliArgs{WaitTimeOnStop: "1"}
c.Assert(wrongWaitTimeOnStop.Verify(), check.NotNil)
require.Error(t, wrongWaitTimeOnStop.Verify())
}
Loading
Loading