github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/st_equals_test.go (about) 1 // Copyright 2023 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 TestSTEquals(t *testing.T) { 28 t.Run("point vs point equals", func(t *testing.T) { 29 require := require.New(t) 30 p1 := types.Point{X: 123, Y: 456} 31 p2 := types.Point{X: 123, Y: 456} 32 f := NewSTEquals(expression.NewLiteral(p1, types.PointType{}), expression.NewLiteral(p2, types.PointType{})) 33 v, err := f.Eval(sql.NewEmptyContext(), nil) 34 require.NoError(err) 35 require.Equal(true, v) 36 }) 37 38 t.Run("point vs point not equals", func(t *testing.T) { 39 require := require.New(t) 40 p1 := types.Point{X: 123, Y: 456} 41 p2 := types.Point{X: 789, Y: 321} 42 f := NewSTEquals(expression.NewLiteral(p1, types.PointType{}), expression.NewLiteral(p2, types.PointType{})) 43 v, err := f.Eval(sql.NewEmptyContext(), nil) 44 require.NoError(err) 45 require.Equal(false, v) 46 }) 47 48 t.Run("null vs point is null", func(t *testing.T) { 49 require := require.New(t) 50 p := types.Point{X: 789, Y: 321} 51 f := NewSTEquals(expression.NewLiteral(nil, types.Null), expression.NewLiteral(p, types.PointType{})) 52 v, err := f.Eval(sql.NewEmptyContext(), nil) 53 require.NoError(err) 54 require.Equal(nil, v) 55 }) 56 57 t.Run("different SRIDs error", func(t *testing.T) { 58 require := require.New(t) 59 p1 := types.Point{SRID: 0, X: 123, Y: 456} 60 p2 := types.Point{SRID: 4326, X: 123, Y: 456} 61 f := NewSTEquals(expression.NewLiteral(p1, types.PointType{}), expression.NewLiteral(p2, types.PointType{})) 62 _, err := f.Eval(sql.NewEmptyContext(), nil) 63 require.Error(err) 64 }) 65 } 66 67 func TestSTEqualsSkipped(t *testing.T) { 68 t.Skip("comparisons that aren't point vs point are unsupported") 69 70 t.Run("linestring vs linestring equals", func(t *testing.T) { 71 require := require.New(t) 72 l1 := types.LineString{Points: []types.Point{{X: 12, Y: 34}, {X: 56, Y: 78}, {X: 56, Y: 78}}} 73 l2 := types.LineString{Points: []types.Point{{X: 56, Y: 78}, {X: 12, Y: 34}, {X: 12, Y: 34}}} 74 f := NewSTEquals(expression.NewLiteral(l1, types.LineStringType{}), expression.NewLiteral(l2, types.LineStringType{})) 75 v, err := f.Eval(sql.NewEmptyContext(), nil) 76 require.NoError(err) 77 require.Equal(true, v) 78 }) 79 80 t.Run("linestring vs linestring not equals", func(t *testing.T) { 81 require := require.New(t) 82 l1 := types.LineString{Points: []types.Point{{X: 12, Y: 34}, {X: 56, Y: 78}}} 83 l2 := types.LineString{Points: []types.Point{{X: 56, Y: 78}, {X: 12, Y: 34}, {X: 123, Y: 349}}} 84 f := NewSTEquals(expression.NewLiteral(l1, types.LineStringType{}), expression.NewLiteral(l2, types.LineStringType{})) 85 v, err := f.Eval(sql.NewEmptyContext(), nil) 86 require.NoError(err) 87 require.Equal(false, v) 88 }) 89 90 t.Run("polygon vs multilinestring not equal", func(t *testing.T) { 91 require := require.New(t) 92 p1 := types.Point{X: 0, Y: 0} 93 p2 := types.Point{X: 0, Y: 1} 94 p3 := types.Point{X: 1, Y: 0} 95 p4 := types.Point{X: 1, Y: 1} 96 l1 := types.LineString{Points: []types.Point{p1, p2, p3, p4}} 97 p := types.Polygon{Lines: []types.LineString{l1}} 98 ml := types.MultiLineString{Lines: []types.LineString{l1}} 99 f := NewSTEquals(expression.NewLiteral(p, types.PolygonType{}), expression.NewLiteral(ml, types.MultiLineStringType{})) 100 v, err := f.Eval(sql.NewEmptyContext(), nil) 101 require.NoError(err) 102 require.Equal(false, v) 103 }) 104 105 t.Run("empty geometry vs empty geometry equal", func(t *testing.T) { 106 require := require.New(t) 107 g1 := types.GeomColl{} 108 g2 := types.GeomColl{} 109 f := NewSTEquals(expression.NewLiteral(g1, types.GeomCollType{}), expression.NewLiteral(g2, types.GeomCollType{})) 110 v, err := f.Eval(sql.NewEmptyContext(), nil) 111 require.NoError(err) 112 require.Equal(true, v) 113 }) 114 115 t.Run("point geometry vs linestring geometry not equal", func(t *testing.T) { 116 require := require.New(t) 117 p1 := types.Point{X: 0, Y: 0} 118 p2 := types.Point{X: 0, Y: 1} 119 l1 := types.LineString{Points: []types.Point{p1, p2}} 120 g1 := types.GeomColl{Geoms: []types.GeometryValue{p1, p2}} 121 g2 := types.GeomColl{Geoms: []types.GeometryValue{l1}} 122 f := NewSTEquals(expression.NewLiteral(g1, types.GeomCollType{}), expression.NewLiteral(g2, types.GeomCollType{})) 123 v, err := f.Eval(sql.NewEmptyContext(), nil) 124 require.NoError(err) 125 require.Equal(false, v) 126 }) 127 }