github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/polygon_test.go (about) 1 // Copyright 2020-2021 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 "testing" 19 20 "github.com/stretchr/testify/require" 21 22 "github.com/dolthub/go-mysql-server/sql" 23 "github.com/dolthub/go-mysql-server/sql/expression" 24 "github.com/dolthub/go-mysql-server/sql/types" 25 ) 26 27 func TestPolygon(t *testing.T) { 28 t.Run("create valid polygon", func(t *testing.T) { 29 require := require.New(t) 30 f, err := NewPolygon(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}, types.LineStringType{})) 31 require.NoError(err) 32 33 v, err := f.Eval(sql.NewEmptyContext(), nil) 34 require.NoError(err) 35 require.Equal(types.Polygon{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}}}, v) 36 }) 37 38 t.Run("create valid polygon with multiple linestrings", func(t *testing.T) { 39 require := require.New(t) 40 f, err := NewPolygon(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}, types.LineStringType{}), 41 expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 0, Y: 0}}}, types.LineStringType{}), 42 expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}}}, types.LineStringType{})) 43 require.NoError(err) 44 45 v, err := f.Eval(sql.NewEmptyContext(), nil) 46 require.NoError(err) 47 require.Equal(types.Polygon{Lines: []types.LineString{{Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 1}, {X: 1, Y: 0}, {X: 0, Y: 0}}}, {Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 0, Y: 0}}}, {Points: []types.Point{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}}}}}, v) 48 }) 49 50 t.Run("create invalid using invalid linestring", func(t *testing.T) { 51 require := require.New(t) 52 f, err := NewPolygon(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}}}, types.LineStringType{})) 53 require.NoError(err) 54 55 _, err = f.Eval(sql.NewEmptyContext(), nil) 56 require.Error(err) 57 }) 58 59 t.Run("create invalid using non-linearring linestring", func(t *testing.T) { 60 require := require.New(t) 61 f, err := NewPolygon(expression.NewLiteral(types.LineString{Points: []types.Point{{X: 0, Y: 0}, {X: 0, Y: 0}}}, types.LineStringType{})) 62 require.NoError(err) 63 64 _, err = f.Eval(sql.NewEmptyContext(), nil) 65 require.Error(err) 66 }) 67 } 68 69 func TestNewPolygon(t *testing.T) { 70 require := require.New(t) 71 _, err := NewPolygon(expression.NewLiteral(nil, types.LineStringType{}), 72 expression.NewLiteral(nil, types.LineStringType{}), 73 expression.NewLiteral(nil, types.LineStringType{}), 74 ) 75 require.NoError(err) 76 }