From 2e2ad4996fe76408e3705b10c05949edc075755b Mon Sep 17 00:00:00 2001 From: Tanisha Saheb <141667332+tanishasaheb15@users.noreply.github.com> Date: Sat, 26 Aug 2023 11:54:52 +0000 Subject: [PATCH 1/4] unit test cases for /pkg/util/misc --- pkg/util/misc/misc_test.go | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 pkg/util/misc/misc_test.go diff --git a/pkg/util/misc/misc_test.go b/pkg/util/misc/misc_test.go new file mode 100644 index 00000000..416a2144 --- /dev/null +++ b/pkg/util/misc/misc_test.go @@ -0,0 +1,80 @@ +package misc_test + +import ( + "bytes" + "errors" + "testing" + + "github.com/arana-db/arana/pkg/util/misc" +) + +func TestParseTable(t *testing.T) { + tests := []struct { + input string + db string + tbl string + err error + }{ + {"dbname.tablename", "dbname", "tablename", nil}, + {"invalid", "", "", errors.New("invalid table name: invalid")}, + {"", "", "", errors.New("invalid table name: ")}, + } + + for _, tt := range tests { + db, tbl, err := misc.ParseTable(tt.input) + if db != tt.db || tbl != tt.tbl || (err != nil && err.Error() != tt.err.Error()) { + t.Errorf("ParseTable(%s) = (%s, %s, %v), want (%s, %s, %v)", tt.input, db, tbl, err, tt.db, tt.tbl, tt.err) + } + } +} + +func TestTryClose(t *testing.T) { + var buf bytes.Buffer + err := misc.TryClose(&buf) + if err != nil { + t.Errorf("TryClose() failed on a valid io.Closer: %v", err) + } + + err = misc.TryClose(123) // an integer isn't an io.Closer + if err != nil { + t.Errorf("TryClose() failed on an invalid io.Closer: %v", err) + } +} + +func TestReverseSlice(t *testing.T) { + tests := []struct { + input []int + expected []int + }{ + {[]int{1, 2, 3}, []int{3, 2, 1}}, + {[]int{}, []int{}}, + {[]int{1}, []int{1}}, + } + + for _, tt := range tests { + misc.ReverseSlice(tt.input) + for i, val := range tt.input { + if val != tt.expected[i] { + t.Errorf("ReverseSlice(%v) = %v, want %v", tt.input, tt.input, tt.expected) + break + } + } + } +} + +func TestCartesianProduct(t *testing.T) { + input := [][]int{{1, 2}, {3, 4}} + expected := [][]int{{1, 3}, {1, 4}, {2, 3}, {2, 4}} + result := misc.CartesianProduct(input) + if len(result) != len(expected) { + t.Fatalf("CartesianProduct(%v) has %d results, want %d", input, len(result), len(expected)) + } + for i, slice := range result { + for j, val := range slice { + if val != expected[i][j] { + t.Errorf("CartesianProduct(%v) = %v, want %v", input, result, expected) + break + } + } + } +} From 08ae4647c77b6ecc1681dfff8f1d52528d6e609f Mon Sep 17 00:00:00 2001 From: Tanisha Saheb <141667332+tanishasaheb15@users.noreply.github.com> Date: Sat, 26 Aug 2023 12:07:08 +0000 Subject: [PATCH 2/4] Add License discription --- pkg/util/misc/misc_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/util/misc/misc_test.go b/pkg/util/misc/misc_test.go index 416a2144..9d53b980 100644 --- a/pkg/util/misc/misc_test.go +++ b/pkg/util/misc/misc_test.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 misc_test import ( From a0cf19760678199a8c6938f809c72d6dc2f91a55 Mon Sep 17 00:00:00 2001 From: Tanisha Saheb <141667332+tanishasaheb15@users.noreply.github.com> Date: Sun, 27 Aug 2023 13:15:42 +0000 Subject: [PATCH 3/4] Fix the import format --- pkg/util/misc/misc_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/util/misc/misc_test.go b/pkg/util/misc/misc_test.go index 9d53b980..1b515477 100644 --- a/pkg/util/misc/misc_test.go +++ b/pkg/util/misc/misc_test.go @@ -21,7 +21,9 @@ import ( "bytes" "errors" "testing" +) +import ( "github.com/arana-db/arana/pkg/util/misc" ) From e5d338151ce16f22c53c09a603d7157344a669f5 Mon Sep 17 00:00:00 2001 From: Tanisha Saheb <141667332+tanishasaheb15@users.noreply.github.com> Date: Sun, 27 Aug 2023 13:25:48 +0000 Subject: [PATCH 4/4] add dependency --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index c023d0f8..e8649596 100644 --- a/go.mod +++ b/go.mod @@ -66,6 +66,7 @@ require ( github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/docker v20.10.11+incompatible // indirect github.com/docker/go-connections v0.4.0 // indirect + github.com/dubbogo/tools v1.0.9 // indirect github.com/dustin/go-humanize v1.0.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-errors/errors v1.0.1 // indirect