github.com/dolthub/go-mysql-server@v0.18.0/sql/types/geometry_test.go (about) 1 // Copyright 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 types 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 "gopkg.in/src-d/go-errors.v1" 23 24 "github.com/dolthub/go-mysql-server/sql" 25 ) 26 27 func TestSpatialTypeMatchSRID(t *testing.T) { 28 var ( 29 // SRID 0 points 30 Cx1y2 = Point{CartesianSRID, 1, 2} 31 Cx2y3 = Point{CartesianSRID, 2, 3} 32 Cx0y0 = Point{CartesianSRID, 0, 0} 33 34 // SRID 4326 points 35 Gx1y2 = Point{GeoSpatialSRID, 1, 2} 36 Gx2y3 = Point{GeoSpatialSRID, 2, 3} 37 Gx0y0 = Point{GeoSpatialSRID, 0, 0} 38 Gx0y1 = Point{GeoSpatialSRID, 0, 1} 39 Gx1y0 = Point{GeoSpatialSRID, 1, 0} 40 ) 41 tests := []struct { 42 typeVal sql.SpatialColumnType 43 objVal interface{} 44 expected *errors.Kind 45 }{ 46 {PointType{CartesianSRID, false}, Cx1y2, nil}, 47 {PointType{CartesianSRID, false}, Gx1y2, nil}, 48 {PointType{GeoSpatialSRID, true}, Cx1y2, sql.ErrNotMatchingSRID}, 49 {PointType{GeoSpatialSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, sql.ErrNotPoint}, 50 {PointType{GeoSpatialSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, sql.ErrNotPoint}, 51 52 {LineStringType{GeoSpatialSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, nil}, 53 // MySQL checks only the container's SRID value, so the objects inside can have any SRID value. 54 // For example, LineStringType column with 4326 allows LineString object with 4326 containing Points with 0 and 4326 SRID values. 55 {LineStringType{CartesianSRID, false}, LineString{GeoSpatialSRID, []Point{Cx1y2, Gx2y3}}, nil}, 56 {LineStringType{CartesianSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, sql.ErrNotMatchingSRID}, 57 {LineStringType{GeoSpatialSRID, true}, Gx2y3, sql.ErrNotLineString}, 58 {LineStringType{GeoSpatialSRID, true}, Polygon{GeoSpatialSRID, []LineString{{GeoSpatialSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}}}, sql.ErrNotLineString}, 59 60 {PolygonType{CartesianSRID, true}, Polygon{CartesianSRID, []LineString{{GeoSpatialSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}}}, nil}, 61 {PolygonType{CartesianSRID, false}, Polygon{GeoSpatialSRID, []LineString{{GeoSpatialSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}}}, nil}, 62 {PolygonType{CartesianSRID, true}, Polygon{GeoSpatialSRID, []LineString{{GeoSpatialSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}}}, sql.ErrNotMatchingSRID}, 63 {PolygonType{GeoSpatialSRID, true}, Gx2y3, ErrNotPolygon}, 64 {PolygonType{GeoSpatialSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, ErrNotPolygon}, 65 66 {GeometryType{CartesianSRID, false}, Cx1y2, nil}, 67 {GeometryType{CartesianSRID, false}, Gx1y2, nil}, 68 {GeometryType{GeoSpatialSRID, true}, Gx1y2, nil}, 69 {GeometryType{GeoSpatialSRID, true}, Cx1y2, sql.ErrNotMatchingSRID}, 70 {GeometryType{GeoSpatialSRID, true}, LineString{GeoSpatialSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}, nil}, 71 {GeometryType{GeoSpatialSRID, true}, LineString{CartesianSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}, sql.ErrNotMatchingSRID}, 72 {GeometryType{GeoSpatialSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, nil}, 73 {GeometryType{CartesianSRID, true}, LineString{GeoSpatialSRID, []Point{Cx1y2, Cx2y3}}, sql.ErrNotMatchingSRID}, 74 {GeometryType{GeoSpatialSRID, true}, Polygon{GeoSpatialSRID, []LineString{{GeoSpatialSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Gx0y0}}}}, nil}, 75 {GeometryType{GeoSpatialSRID, true}, Polygon{GeoSpatialSRID, []LineString{{CartesianSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Cx0y0}}}}, nil}, 76 {GeometryType{CartesianSRID, true}, Polygon{GeoSpatialSRID, []LineString{{CartesianSRID, []Point{Gx0y0, Gx0y1, Gx1y0, Cx0y0}}}}, sql.ErrNotMatchingSRID}, 77 } 78 79 for _, test := range tests { 80 s, d := test.typeVal.GetSpatialTypeSRID() 81 g, _ := test.typeVal.(sql.Type) 82 t.Run(fmt.Sprintf("%s %v %v match %v", g.String(), s, d, test.objVal), func(t *testing.T) { 83 err := test.typeVal.MatchSRID(test.objVal) 84 if test.expected == nil { 85 require.NoError(t, err) 86 } else { 87 require.Error(t, err) 88 require.True(t, test.expected.Is(err), "Expected error of type %s but got %s", test.expected, err) 89 } 90 }) 91 } 92 }