github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/st_length_test.go (about) 1 // Copyright 2020-2022 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 spatial 16 17 import ( 18 "math" 19 "testing" 20 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 TestSTLength(t *testing.T) { 29 t.Run("select unit length", func(t *testing.T) { 30 require := require.New(t) 31 f, err := NewSTLength(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 0, Y: 1}}}, types.LineStringType{})) 32 require.NoError(err) 33 34 v, err := f.Eval(sql.NewEmptyContext(), nil) 35 require.NoError(err) 36 require.Equal(1.0, v) 37 }) 38 39 t.Run("select sqrt 2", func(t *testing.T) { 40 require := require.New(t) 41 f, err := NewSTLength(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}}}, types.LineStringType{})) 42 require.NoError(err) 43 44 v, err := f.Eval(sql.NewEmptyContext(), nil) 45 require.NoError(err) 46 require.Equal(math.Sqrt2, v) 47 }) 48 49 t.Run("select perimeter of unit square", func(t *testing.T) { 50 require := require.New(t) 51 f, err := NewSTLength(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}, types.LineStringType{})) 52 require.NoError(err) 53 54 v, err := f.Eval(sql.NewEmptyContext(), nil) 55 require.NoError(err) 56 require.Equal(4.0, v) 57 }) 58 59 t.Run("select length of some line", func(t *testing.T) { 60 require := require.New(t) 61 f, err := NewSTLength(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 0, Y: 1}, {X: -1, Y: -1}, {X: -1, Y: 1.23}, {X: 55, Y: 12}}}, types.LineStringType{})) 62 require.NoError(err) 63 64 v, err := f.Eval(sql.NewEmptyContext(), nil) 65 require.NoError(err) 66 require.Equal(62.49231544303096, v) 67 }) 68 69 t.Run("select length of NULL", func(t *testing.T) { 70 require := require.New(t) 71 f, err := NewSTLength(expression.NewLiteral(nil, types.Null)) 72 require.NoError(err) 73 v, err := f.Eval(sql.NewEmptyContext(), nil) 74 require.NoError(err) 75 require.Equal(nil, v) 76 }) 77 78 t.Run("select length of wrong spatial type", func(t *testing.T) { 79 require := require.New(t) 80 f, err := NewSTLength(expression.NewLiteral(types.Point{X: 0, Y: 0}, types.PointType{})) 81 require.NoError(err) 82 v, err := f.Eval(sql.NewEmptyContext(), nil) 83 require.NoError(err) 84 require.Equal(nil, v) 85 }) 86 87 t.Run("select length of wrong type", func(t *testing.T) { 88 require := require.New(t) 89 f, err := NewSTLength(expression.NewLiteral("abc", types.Text)) 90 require.NoError(err) 91 _, err = f.Eval(sql.NewEmptyContext(), nil) 92 require.Error(err) 93 }) 94 }