Skip to content

Commit

Permalink
tests: Add tests for go/textutil (vitessio#14991)
Browse files Browse the repository at this point in the history
Signed-off-by: Manik Rana <[email protected]>
  • Loading branch information
Maniktherana authored Jan 22, 2024
1 parent 94ac917 commit 1bf8dda
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
100 changes: 100 additions & 0 deletions go/textutil/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"

binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)

func TestSplitDelimitedList(t *testing.T) {
Expand Down Expand Up @@ -65,6 +68,12 @@ func TestSplitUnescape(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expected, elems)
}
{
s := "invalid%2"
elems, err := SplitUnescape(s, ",")
assert.Error(t, err)
assert.Equal(t, []string{}, elems)
}
}

func TestSingleWordCamel(t *testing.T) {
Expand Down Expand Up @@ -108,3 +117,94 @@ func TestSingleWordCamel(t *testing.T) {
})
}
}

func TestValueIsSimulatedNull(t *testing.T) {
tt := []struct {
name string
val interface{}
isNull bool
}{
{
name: "case string false",
val: "test",
isNull: false,
},
{
name: "case string true",
val: SimulatedNullString,
isNull: true,
},
{
name: "case []string true",
val: []string{SimulatedNullString},
isNull: true,
},
{
name: "case []string false",
val: []string{SimulatedNullString, SimulatedNullString},
isNull: false,
},
{
name: "case binlogdatapb.OnDDLAction true",
val: binlogdatapb.OnDDLAction(SimulatedNullInt),
isNull: true,
},
{
name: "case int true",
val: SimulatedNullInt,
isNull: true,
},
{
name: "case int32 true",
val: int32(SimulatedNullInt),
isNull: true,
},
{
name: "case int64 true",
val: int64(SimulatedNullInt),
isNull: true,
},
{
name: "case []topodatapb.TabletType true",
val: []topodatapb.TabletType{topodatapb.TabletType(SimulatedNullInt)},
isNull: true,
},
{
name: "case binlogdatapb.VReplicationWorkflowState true",
val: binlogdatapb.VReplicationWorkflowState(SimulatedNullInt),
isNull: true,
},
{
name: "case default",
val: float64(1),
isNull: false,
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
isNull := ValueIsSimulatedNull(tc.val)
assert.Equal(t, tc.isNull, isNull)
})
}
}

func TestTitle(t *testing.T) {
tt := []struct {
s string
expect string
}{
{s: "hello world", expect: "Hello World"},
{s: "snake_case", expect: "Snake_case"},
{s: "TITLE CASE", expect: "TITLE CASE"},
{s: "HelLo wOrLd", expect: "HelLo WOrLd"},
{s: "", expect: ""},
}

for _, tc := range tt {
t.Run(tc.s, func(t *testing.T) {
title := Title(tc.s)
assert.Equal(t, tc.expect, title)
})
}
}
51 changes: 51 additions & 0 deletions go/textutil/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2024 The Vitess Authors.
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 textutil

import (
"testing"
"text/template"

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

func TestExecuteTemplate(t *testing.T) {
tmplText := "Result: {{.Value}}"
inputData := struct{ Value string }{Value: "Test"}
tmpl, err := template.New("test").Parse(tmplText)
require.NoError(t, err)

result, err := ExecuteTemplate(tmpl, inputData)
require.NoError(t, err)

expectedResult := "Result: Test"
assert.Equal(t, expectedResult, result)

}

func TestExecuteTemplateWithError(t *testing.T) {
templText := "{{.UndefinedVariable}}"
invalidInput := struct{ Name string }{Name: "foo"}

tmpl, err := template.New("test").Parse(templText)
require.NoError(t, err)

result, err := ExecuteTemplate(tmpl, invalidInput)
assert.Error(t, err)
assert.Equal(t, "", result)
}

0 comments on commit 1bf8dda

Please sign in to comment.