From 4c49e5204fb60b01a76b78619eb6627a1a387a71 Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 2 Sep 2021 20:50:14 +0800 Subject: [PATCH] types: migrate test-infra to testify for set_test.go (#27709) --- types/main_test.go | 31 +++++++++++ types/set_serial_test.go | 108 ++++++++++++++++++++++++++++++++++++++ types/set_test.go | 110 --------------------------------------- 3 files changed, 139 insertions(+), 110 deletions(-) create mode 100644 types/main_test.go create mode 100644 types/set_serial_test.go delete mode 100644 types/set_test.go diff --git a/types/main_test.go b/types/main_test.go new file mode 100644 index 0000000000000..26127901cab34 --- /dev/null +++ b/types/main_test.go @@ -0,0 +1,31 @@ +// Copyright 2021 PingCAP, Inc. +// +// 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 types + +import ( + "testing" + + "github.com/pingcap/tidb/util/testbridge" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testbridge.WorkaroundGoCheckFlags() + opts := []goleak.Option{ + goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"), + goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), + } + goleak.VerifyTestMain(m, opts...) +} diff --git a/types/set_serial_test.go b/types/set_serial_test.go new file mode 100644 index 0000000000000..0443058e706df --- /dev/null +++ b/types/set_serial_test.go @@ -0,0 +1,108 @@ +// Copyright 2015 PingCAP, Inc. +// +// 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 types + +import ( + "testing" + + "github.com/pingcap/parser/mysql" + "github.com/pingcap/tidb/util/collate" + "github.com/stretchr/testify/require" +) + +func TestSet(t *testing.T) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + + elems := []string{"a", "b", "c", "d"} + + t.Run("ParseSet", func(t *testing.T) { + tests := []struct { + Name string + ExpectedValue uint64 + ExpectedName string + }{ + {"a", 1, "a"}, + {"a,b,a", 3, "a,b"}, + {"b,a", 3, "a,b"}, + {"a,b,c,d", 15, "a,b,c,d"}, + {"d", 8, "d"}, + {"", 0, ""}, + {"0", 0, ""}, + } + + for _, collation := range []string{mysql.DefaultCollationName, "utf8_unicode_ci"} { + for _, test := range tests { + e, err := ParseSet(elems, test.Name, collation) + require.NoError(t, err) + require.Equal(t, float64(test.ExpectedValue), e.ToNumber()) + require.Equal(t, test.ExpectedName, e.String()) + } + } + }) + + t.Run("ParseSet_ci", func(t *testing.T) { + tests := []struct { + Name string + ExpectedValue uint64 + ExpectedName string + }{ + {"A ", 1, "a"}, + {"a,B,a", 3, "a,b"}, + } + + for _, test := range tests { + e, err := ParseSet(elems, test.Name, "utf8_general_ci") + require.NoError(t, err) + require.Equal(t, float64(test.ExpectedValue), e.ToNumber()) + require.Equal(t, test.ExpectedName, e.String()) + } + }) + + t.Run("ParseSetValue", func(t *testing.T) { + tests := []struct { + Number uint64 + ExpectedName string + }{ + {0, ""}, + {1, "a"}, + {3, "a,b"}, + {9, "a,d"}, + } + + for _, test := range tests { + e, err := ParseSetValue(elems, test.Number) + require.NoError(t, err) + require.Equal(t, float64(test.Number), e.ToNumber()) + require.Equal(t, test.ExpectedName, e.String()) + } + }) + + t.Run("ParseSet_err", func(t *testing.T) { + tests := []string{"a.e", "e.f"} + for _, test := range tests { + _, err := ParseSet(elems, test, mysql.DefaultCollationName) + require.Error(t, err) + } + }) + + t.Run("ParseSetValue_err", func(t *testing.T) { + tests := []uint64{100, 16, 64} + for _, test := range tests { + _, err := ParseSetValue(elems, test) + require.Error(t, err) + } + }) +} diff --git a/types/set_test.go b/types/set_test.go deleted file mode 100644 index 7bd9675c7a115..0000000000000 --- a/types/set_test.go +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright 2015 PingCAP, Inc. -// -// 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 types - -import ( - . "github.com/pingcap/check" - "github.com/pingcap/parser/mysql" - "github.com/pingcap/tidb/util/collate" - "github.com/pingcap/tidb/util/testleak" -) - -var _ = SerialSuites(&testSetSuite{}) - -type testSetSuite struct { -} - -func (s *testSetSuite) TestSet(c *C) { - defer testleak.AfterTest(c)() - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - elems := []string{"a", "b", "c", "d"} - tbl := []struct { - Name string - ExpectedValue uint64 - ExpectedName string - }{ - {"a", 1, "a"}, - {"a,b,a", 3, "a,b"}, - {"b,a", 3, "a,b"}, - {"a,b,c,d", 15, "a,b,c,d"}, - {"d", 8, "d"}, - {"", 0, ""}, - {"0", 0, ""}, - } - citbl := []struct { - Name string - ExpectedValue uint64 - ExpectedName string - }{ - {"A ", 1, "a"}, - {"a,B,a", 3, "a,b"}, - } - - for _, t := range tbl { - e, err := ParseSet(elems, t.Name, mysql.DefaultCollationName) - c.Assert(err, IsNil) - c.Assert(e.ToNumber(), Equals, float64(t.ExpectedValue)) - c.Assert(e.String(), Equals, t.ExpectedName) - } - - for _, t := range tbl { - e, err := ParseSet(elems, t.Name, "utf8_unicode_ci") - c.Assert(err, IsNil) - c.Assert(e.ToNumber(), Equals, float64(t.ExpectedValue)) - c.Assert(e.String(), Equals, t.ExpectedName) - } - - for _, t := range citbl { - e, err := ParseSet(elems, t.Name, "utf8_general_ci") - c.Assert(err, IsNil) - c.Assert(e.ToNumber(), Equals, float64(t.ExpectedValue)) - c.Assert(e.String(), Equals, t.ExpectedName) - } - - tblNumber := []struct { - Number uint64 - ExpectedName string - }{ - {0, ""}, - {1, "a"}, - {3, "a,b"}, - {9, "a,d"}, - } - - for _, t := range tblNumber { - e, err := ParseSetValue(elems, t.Number) - c.Assert(err, IsNil) - c.Assert(e.String(), Equals, t.ExpectedName) - c.Assert(e.ToNumber(), Equals, float64(t.Number)) - } - - tblErr := []string{ - "a.e", - "e.f", - } - for _, t := range tblErr { - _, err := ParseSet(elems, t, mysql.DefaultCollationName) - c.Assert(err, NotNil) - } - - tblNumberErr := []uint64{ - 100, 16, 64, - } - for _, t := range tblNumberErr { - _, err := ParseSetValue(elems, t) - c.Assert(err, NotNil) - } -}