Skip to content

Commit

Permalink
unit test cases for /pkg/util/misc (#755)
Browse files Browse the repository at this point in the history
* unit test cases for /pkg/util/misc

* Add License discription

* Fix the import format

* add dependency
  • Loading branch information
tanishasaheb15 authored Aug 28, 2023
1 parent f285e71 commit 304e9ff
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
99 changes: 99 additions & 0 deletions pkg/util/misc/misc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 (
"bytes"
"errors"
"testing"
)

import (
"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
}
}
}
}

0 comments on commit 304e9ff

Please sign in to comment.