github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geoindex/s2_geography_index_test.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package geoindex
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/geo"
    19  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    20  	"github.com/cockroachdb/datadriven"
    21  )
    22  
    23  // TODO(sumeer): applies to this and the geometry test. The current test is
    24  // verbose in printing out the actual expressions, which are useful as a
    25  // sanity check, but hard to validate.
    26  // - Add datadriven cases that test relationships between shapes.
    27  // - Add randomized tests
    28  
    29  func TestS2GeographyIndexBasic(t *testing.T) {
    30  	defer leaktest.AfterTest(t)()
    31  
    32  	ctx := context.Background()
    33  	var index GeographyIndex
    34  	shapes := make(map[string]*geo.Geography)
    35  	datadriven.RunTest(t, "testdata/s2_geography", func(t *testing.T, d *datadriven.TestData) string {
    36  		switch d.Cmd {
    37  		case "init":
    38  			cfg := s2Config(t, d)
    39  			index = NewS2GeographyIndex(S2GeographyConfig{S2Config: &cfg})
    40  			return ""
    41  		case "geometry":
    42  			g, err := geo.ParseGeography(d.Input)
    43  			if err != nil {
    44  				return err.Error()
    45  			}
    46  			shapes[nameArg(t, d)] = g
    47  			return ""
    48  		case "index-keys":
    49  			return keysToString(index.InvertedIndexKeys(ctx, shapes[nameArg(t, d)]))
    50  		case "inner-covering":
    51  			return cellUnionToString(index.TestingInnerCovering(shapes[nameArg(t, d)]))
    52  		case "covers":
    53  			return spansToString(index.Covers(ctx, shapes[nameArg(t, d)]))
    54  		case "intersects":
    55  			return spansToString(index.Intersects(ctx, shapes[nameArg(t, d)]))
    56  		case "covered-by":
    57  			return checkExprAndToString(index.CoveredBy(ctx, shapes[nameArg(t, d)]))
    58  		default:
    59  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    60  		}
    61  	})
    62  }