github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/space_test.go (about)

     1  // Copyright 2024 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package function
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/shopspring/decimal"
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/expression"
    25  	"github.com/dolthub/go-mysql-server/sql/types"
    26  )
    27  
    28  func TestSpace(t *testing.T) {
    29  	tests := []struct {
    30  		name string
    31  		arg  sql.Expression
    32  		exp  interface{}
    33  		err  bool
    34  		skip bool
    35  	}{
    36  		{
    37  			name: "null argument",
    38  			arg:  nil,
    39  			exp:  nil,
    40  			err:  false,
    41  		},
    42  		{
    43  			name: "zero",
    44  			arg:  expression.NewLiteral(int64(0), types.Int64),
    45  			exp:  "",
    46  			err:  false,
    47  		},
    48  		{
    49  			name: "one",
    50  			arg:  expression.NewLiteral(int64(1), types.Int64),
    51  			exp:  " ",
    52  			err:  false,
    53  		},
    54  		{
    55  			name: "ten",
    56  			arg:  expression.NewLiteral(int64(10), types.Int64),
    57  			exp:  "          ",
    58  			err:  false,
    59  		},
    60  		{
    61  			name: "negative",
    62  			arg:  expression.NewLiteral(int64(-1), types.Int64),
    63  			exp:  "",
    64  			err:  false,
    65  		},
    66  		{
    67  			name: "float32 rounds down",
    68  			arg:  expression.NewLiteral(float32(1.1), types.Float32),
    69  			exp:  " ",
    70  			err:  false,
    71  		},
    72  		{
    73  			name: "float64 rounds down",
    74  			arg:  expression.NewLiteral(1.1, types.Float64),
    75  			exp:  " ",
    76  			err:  false,
    77  		},
    78  		{
    79  			name: "decimal rounds down",
    80  			arg:  expression.NewLiteral(decimal.NewFromFloat(1.1), types.DecimalType_{}),
    81  			exp:  " ",
    82  			err:  false,
    83  		},
    84  		{
    85  			name: "float32 rounds up",
    86  			arg:  expression.NewLiteral(float32(1.99), types.Float32),
    87  			exp:  "  ",
    88  			err:  false,
    89  		},
    90  		{
    91  			name: "float64 rounds up",
    92  			arg:  expression.NewLiteral(1.99, types.Float64),
    93  			exp:  "  ",
    94  			err:  false,
    95  		},
    96  		{
    97  			name: "decimal rounds up",
    98  			arg:  expression.NewLiteral(decimal.NewFromFloat(1.99), types.DecimalType_{}),
    99  			exp:  "  ",
   100  			err:  false,
   101  		},
   102  		{
   103  			name: "negative float32 is 0",
   104  			arg:  expression.NewLiteral(float32(-12.34), types.Float32),
   105  			exp:  "",
   106  			err:  false,
   107  		},
   108  		{
   109  			name: "negative float64 is 0",
   110  			arg:  expression.NewLiteral(-12.34, types.Float64),
   111  			exp:  "",
   112  			err:  false,
   113  		},
   114  		{
   115  			name: "negative decimal is 0",
   116  			arg:  expression.NewLiteral(decimal.NewFromFloat(-12.34), types.DecimalType_{}),
   117  			exp:  "",
   118  			err:  false,
   119  		},
   120  		{
   121  			name: "empty string is 0",
   122  			arg:  expression.NewLiteral("notanumber", types.Text),
   123  			exp:  "",
   124  			err:  false,
   125  		},
   126  		{
   127  			name: "numerical string",
   128  			arg:  expression.NewLiteral("10", types.Text),
   129  			exp:  "          ",
   130  			err:  false,
   131  		},
   132  		{
   133  			// we don't do truncation yet
   134  			// https://github.com/dolthub/dolt/issues/7302
   135  			name: "scientific string is truncated",
   136  			arg:  expression.NewLiteral("1e1", types.Text),
   137  			exp:  "",
   138  			err:  false,
   139  			skip: true,
   140  		},
   141  	}
   142  
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			if tt.skip {
   146  				t.Skip()
   147  			}
   148  
   149  			ctx := sql.NewEmptyContext()
   150  			f := NewSpace(tt.arg)
   151  
   152  			res, err := f.Eval(ctx, nil)
   153  			if tt.err {
   154  				require.Error(t, err)
   155  				return
   156  			}
   157  
   158  			require.NoError(t, err)
   159  			require.Equal(t, tt.exp, res)
   160  		})
   161  	}
   162  }