Skip to content

Commit

Permalink
test: Add missing/required tests for sqltypes and mathstats (#15920)
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <noblemittal@outlook.com>
  • Loading branch information
beingnoble03 committed May 16, 2024
1 parent 29a334f commit 6ff462c
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 232 deletions.
22 changes: 22 additions & 0 deletions go/mathstats/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ func TestSampleClear(t *testing.T) {
assert.False(t, s.Sorted, "Sorting status should be false after clearing")
}

func TestIQR(t *testing.T) {
tt := []struct {
sample Sample
expected float64
}{
{Sample{Xs: []float64{15, 20, 35, 40, 50}}, 24.999999999999996},
{Sample{Xs: []float64{}, Sorted: false}, math.NaN()},
{Sample{Xs: []float64{15, 0, 0, 40, 50}}, 43.33333333333333},
{Sample{Xs: []float64{10, 2, 1, 0, 23}}, 13.666666666666663},
}

for _, tc := range tt {
iqr := tc.sample.IQR()

if math.IsNaN(tc.expected) {
assert.True(t, math.IsNaN(iqr))
} else {
assert.Equal(t, tc.expected, iqr)
}
}
}

func TestSampleSort(t *testing.T) {
tt := []struct {
sample Sample
Expand Down
126 changes: 37 additions & 89 deletions go/sqltypes/bind_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package sqltypes

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -92,15 +91,10 @@ func TestBuildBindVariables(t *testing.T) {
}}
for _, tcase := range tcases {
bindVars, err := BuildBindVariables(tcase.in)
if err != nil {
if err.Error() != tcase.err {
t.Errorf("MapToBindVars(%v) error: %v, want %s", tcase.in, err, tcase.err)
}
continue
}
if tcase.err != "" {
t.Errorf("MapToBindVars(%v) error: nil, want %s", tcase.in, tcase.err)
continue
if tcase.err == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tcase.err)
}
if !BindVariablesEqual(bindVars, tcase.out) {
t.Errorf("MapToBindVars(%v): %v, want %s", tcase.in, bindVars, tcase.out)
Expand Down Expand Up @@ -371,14 +365,10 @@ func TestValidateBindVarables(t *testing.T) {
for _, tcase := range tcases {
err := ValidateBindVariables(tcase.in)
if tcase.err != "" {
if err == nil || err.Error() != tcase.err {
t.Errorf("ValidateBindVars(%v): %v, want %s", tcase.in, err, tcase.err)
}
assert.ErrorContains(t, err, tcase.err)
continue
}
if err != nil {
t.Errorf("ValidateBindVars(%v): %v, want nil", tcase.in, err)
}
assert.NoError(t, err)
}
}

Expand Down Expand Up @@ -582,22 +572,16 @@ func TestValidateBindVariable(t *testing.T) {
for _, tcase := range testcases {
err := ValidateBindVariable(tcase.in)
if tcase.err != "" {
if err == nil || !strings.Contains(err.Error(), tcase.err) {
t.Errorf("ValidateBindVar(%v) error: %v, must contain %v", tcase.in, err, tcase.err)
}
assert.ErrorContains(t, err, tcase.err)
continue
}
if err != nil {
t.Errorf("ValidateBindVar(%v) error: %v", tcase.in, err)
}
assert.NoError(t, err)
}

// Special case: nil bind var.
err := ValidateBindVariable(nil)
want := "bind variable is nil"
if err == nil || err.Error() != want {
t.Errorf("ValidateBindVar(nil) error: %v, want %s", err, want)
}
assert.ErrorContains(t, err, want)
}

func TestBindVariableToValue(t *testing.T) {
Expand Down Expand Up @@ -633,19 +617,13 @@ func TestBindVariablesEqual(t *testing.T) {
Value: []byte("1"),
},
}
if !BindVariablesEqual(bv1, bv2) {
t.Errorf("%v != %v, want equal", bv1, bv2)
}
if !BindVariablesEqual(bv1, bv3) {
t.Errorf("%v = %v, want not equal", bv1, bv3)
}
assert.True(t, BindVariablesEqual(bv1, bv2))
assert.True(t, BindVariablesEqual(bv1, bv3))
}

func TestBindVariablesFormat(t *testing.T) {
tupleBindVar, err := BuildBindVariable([]int64{1, 2})
if err != nil {
t.Fatalf("failed to create a tuple bind var: %v", err)
}
require.NoError(t, err, "failed to create a tuple bind var")

bindVariables := map[string]*querypb.BindVariable{
"key_1": StringBindVariable("val_1"),
Expand All @@ -655,68 +633,38 @@ func TestBindVariablesFormat(t *testing.T) {
}

formattedStr := FormatBindVariables(bindVariables, true /* full */, false /* asJSON */)
if !strings.Contains(formattedStr, "key_1") ||
!strings.Contains(formattedStr, "val_1") {
t.Fatalf("bind variable 'key_1': 'val_1' is not formatted")
}
if !strings.Contains(formattedStr, "key_2") ||
!strings.Contains(formattedStr, "789") {
t.Fatalf("bind variable 'key_2': '789' is not formatted")
}
if !strings.Contains(formattedStr, "key_3") || !strings.Contains(formattedStr, "val_3") {
t.Fatalf("bind variable 'key_3': 'val_3' is not formatted")
}
if !strings.Contains(formattedStr, "key_4:type:TUPLE") {
t.Fatalf("bind variable 'key_4': (1, 2) is not formatted")
}
assert.Contains(t, formattedStr, "key_1")
assert.Contains(t, formattedStr, "val_1")

formattedStr = FormatBindVariables(bindVariables, false /* full */, false /* asJSON */)
if !strings.Contains(formattedStr, "key_1") {
t.Fatalf("bind variable 'key_1' is not formatted")
}
if !strings.Contains(formattedStr, "key_2") ||
!strings.Contains(formattedStr, "789") {
t.Fatalf("bind variable 'key_2': '789' is not formatted")
}
if !strings.Contains(formattedStr, "key_3") || !strings.Contains(formattedStr, "5 bytes") {
t.Fatalf("bind variable 'key_3' is not formatted")
}
if !strings.Contains(formattedStr, "key_4") || !strings.Contains(formattedStr, "2 items") {
t.Fatalf("bind variable 'key_4' is not formatted")
}
assert.Contains(t, formattedStr, "key_2")
assert.Contains(t, formattedStr, "789")

formattedStr = FormatBindVariables(bindVariables, true /* full */, true /* asJSON */)
t.Logf("%q", formattedStr)
if !strings.Contains(formattedStr, "\"key_1\": {\"type\": \"VARCHAR\", \"value\": \"val_1\"}") {
t.Fatalf("bind variable 'key_1' is not formatted")
}
assert.Contains(t, formattedStr, "key_3")
assert.Contains(t, formattedStr, "val_3")

if !strings.Contains(formattedStr, "\"key_2\": {\"type\": \"INT64\", \"value\": 789}") {
t.Fatalf("bind variable 'key_2' is not formatted")
}
assert.Contains(t, formattedStr, "key_4:type:TUPLE")

if !strings.Contains(formattedStr, "\"key_3\": {\"type\": \"VARBINARY\", \"value\": \"val_3\"}") {
t.Fatalf("bind variable 'key_3' is not formatted")
}
formattedStr = FormatBindVariables(bindVariables, false /* full */, false /* asJSON */)
assert.Contains(t, formattedStr, "key_1")

if !strings.Contains(formattedStr, "\"key_4\": {\"type\": \"TUPLE\", \"value\": \"\"}") {
t.Fatalf("bind variable 'key_4' is not formatted")
}
assert.Contains(t, formattedStr, "key_2")
assert.Contains(t, formattedStr, "789")

formattedStr = FormatBindVariables(bindVariables, false /* full */, true /* asJSON */)
if !strings.Contains(formattedStr, "\"key_1\": {\"type\": \"VARCHAR\", \"value\": \"5 bytes\"}") {
t.Fatalf("bind variable 'key_1' is not formatted")
}
assert.Contains(t, formattedStr, "key_3")
assert.Contains(t, formattedStr, "5 bytes")

if !strings.Contains(formattedStr, "\"key_2\": {\"type\": \"INT64\", \"value\": 789}") {
t.Fatalf("bind variable 'key_2' is not formatted")
}
assert.Contains(t, formattedStr, "key_4")
assert.Contains(t, formattedStr, "2 items")

if !strings.Contains(formattedStr, "\"key_3\": {\"type\": \"VARCHAR\", \"value\": \"5 bytes\"}") {
t.Fatalf("bind variable 'key_3' is not formatted")
}
formattedStr = FormatBindVariables(bindVariables, true /* full */, true /* asJSON */)
assert.Contains(t, formattedStr, "\"key_1\": {\"type\": \"VARCHAR\", \"value\": \"val_1\"}")
assert.Contains(t, formattedStr, "\"key_2\": {\"type\": \"INT64\", \"value\": 789}")
assert.Contains(t, formattedStr, "\"key_3\": {\"type\": \"VARBINARY\", \"value\": \"val_3\"}")
assert.Contains(t, formattedStr, "\"key_4\": {\"type\": \"TUPLE\", \"value\": \"\"}")

if !strings.Contains(formattedStr, "\"key_4\": {\"type\": \"VARCHAR\", \"value\": \"2 items\"}") {
t.Fatalf("bind variable 'key_4' is not formatted")
}
formattedStr = FormatBindVariables(bindVariables, false /* full */, true /* asJSON */)
assert.Contains(t, formattedStr, "\"key_1\": {\"type\": \"VARCHAR\", \"value\": \"5 bytes\"}")
assert.Contains(t, formattedStr, "\"key_2\": {\"type\": \"INT64\", \"value\": 789}")
assert.Contains(t, formattedStr, "\"key_3\": {\"type\": \"VARCHAR\", \"value\": \"5 bytes\"}")
assert.Contains(t, formattedStr, "\"key_4\": {\"type\": \"VARCHAR\", \"value\": \"2 items\"}")
}
47 changes: 47 additions & 0 deletions go/sqltypes/named_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestToNamedResult(t *testing.T) {
for i := range in.Rows {
require.Equal(t, in.Rows[i][0], named.Rows[i]["id"])
require.Equal(t, int64(i), named.Rows[i].AsInt64("id", 0))
require.Equal(t, int32(i), named.Rows[i].AsInt32("id", 0))

require.Equal(t, in.Rows[i][1], named.Rows[i]["status"])
require.Equal(t, fmt.Sprintf("s%d", i), named.Rows[i].AsString("status", "notfound"))
Expand Down Expand Up @@ -173,3 +174,49 @@ func TestRow(t *testing.T) {
})
}
}

func TestAsBool(t *testing.T) {
row := RowNamedValues{
"testFalse": MakeTrusted(Int64, []byte("0")),
"testTrue": MakeTrusted(Int64, []byte("1")),
}

r := row.AsBool("testFalse", true)
assert.False(t, r)

r = row.AsBool("testTrue", false)
assert.True(t, r)

r = row.AsBool("invalidField", true)
assert.True(t, r)
}

func TestAsBytes(t *testing.T) {
row := RowNamedValues{
"testField": MakeTrusted(Int64, []byte("1002")),
}

r := row.AsBytes("testField", []byte("default"))
assert.Equal(t, []byte("1002"), r)

r = row.AsBytes("invalidField", []byte("default"))
assert.Equal(t, []byte("default"), r)

}

func TestAsFloat64(t *testing.T) {
row := RowNamedValues{
"testField": MakeTrusted(Int64, []byte("1002")),
"testField2": MakeTrusted(Float64, []byte("10.02")),
}

r := row.AsFloat64("testField", 23.12)
assert.Equal(t, float64(1002), r)

r = row.AsFloat64("testField2", 23.12)
assert.Equal(t, 10.02, r)

r = row.AsFloat64("invalidField", 23.12)
assert.Equal(t, 23.12, r)

}
Loading

0 comments on commit 6ff462c

Please sign in to comment.