Skip to content

Commit

Permalink
Add/update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Levko Kravets <[email protected]>
  • Loading branch information
kravets-levko committed Oct 2, 2024
1 parent fb4a334 commit cc60b2f
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dbsql

import (
"database/sql/driver"
dbsqlerr "github.com/databricks/databricks-sql-go/errors"
"github.com/stretchr/testify/require"
"strconv"
"testing"
"time"
Expand All @@ -21,7 +23,7 @@ func TestParameter_Inference(t *testing.T) {
{Name: "", Value: nil},
{Name: "", Value: Parameter{Value: float64Ptr(6.2), Type: SqlUnkown}},
}
parameters := convertNamedValuesToSparkParams(values[:])
parameters, _ := convertNamedValuesToSparkParams(values[:])
assert.Equal(t, strconv.FormatFloat(float64(5.1), 'f', -1, 64), *parameters[0].Value.StringValue)
assert.NotNil(t, parameters[1].Value.StringValue)
assert.Equal(t, string("TIMESTAMP"), *parameters[1].Type)
Expand All @@ -34,14 +36,45 @@ func TestParameter_Inference(t *testing.T) {
assert.Equal(t, &cli_service.TSparkParameterValue{StringValue: strPtr("6.2")}, parameters[6].Value)
})
}
func TestParameters_Names(t *testing.T) {
t.Run("Should infer types correctly", func(t *testing.T) {
values := [2]driver.NamedValue{{Name: "1", Value: int(26)}, {Name: "", Value: Parameter{Name: "2", Type: SqlDecimal, Value: "6.2"}}}
parameters := convertNamedValuesToSparkParams(values[:])
assert.Equal(t, string("1"), *parameters[0].Name)
assert.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("26")}, *parameters[0].Value)
assert.Equal(t, string("2"), *parameters[1].Name)
assert.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("6.2")}, *parameters[1].Value)
assert.Equal(t, string("DECIMAL(2,1)"), *parameters[1].Type)

func TestParameters_ConvertToSpark(t *testing.T) {
t.Run("Should convert names parameters", func(t *testing.T) {
values := [2]driver.NamedValue{
{Name: "1", Value: int(26)},
{Name: "", Value: Parameter{Name: "2", Type: SqlDecimal, Value: "6.2"}},
}
parameters, err := convertNamedValuesToSparkParams(values[:])
require.NoError(t, err)
require.Equal(t, string("1"), *parameters[0].Name)
require.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("26")}, *parameters[0].Value)
require.Equal(t, string("2"), *parameters[1].Name)
require.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("6.2")}, *parameters[1].Value)
require.Equal(t, string("DECIMAL(2,1)"), *parameters[1].Type)
})

t.Run("Should convert positional parameters", func(t *testing.T) {
values := [2]driver.NamedValue{
{Value: int(26)},
{Name: "", Value: Parameter{Type: SqlDecimal, Value: "6.2"}},
}
parameters, err := convertNamedValuesToSparkParams(values[:])
require.NoError(t, err)
require.Nil(t, parameters[0].Name)
require.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("26")}, *parameters[0].Value)
require.Nil(t, parameters[1].Name)
require.Equal(t, cli_service.TSparkParameterValue{StringValue: strPtr("6.2")}, *parameters[1].Value)
require.Equal(t, string("DECIMAL(2,1)"), *parameters[1].Type)
})

t.Run("Should error out when named and positional parameters are mixed", func(t *testing.T) {
values := [4]driver.NamedValue{
{Name: "a", Value: int(26)},
{Name: "", Value: Parameter{Type: SqlDecimal, Value: "6.2"}},
{Value: "test"},
{Name: "b", Value: Parameter{Type: SqlDouble, Value: 123.456}},
}
_, err := convertNamedValuesToSparkParams(values[:])
require.Error(t, err)
require.Equal(t, err.Error(), dbsqlerr.ErrMixedNamedAndPositionalParameters)
})
}

0 comments on commit cc60b2f

Please sign in to comment.