github.com/altipla-consulting/ravendb-go-client@v0.1.3/tests/bounding_box_index_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	ravendb "github.com/altipla-consulting/ravendb-go-client"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func boundingBox_boundingBoxTest(t *testing.T, driver *RavenTestDriver) {
    12  	var err error
    13  	store := driver.getDocumentStoreMust(t)
    14  	defer store.Close()
    15  
    16  	polygon := "POLYGON ((0 0, 0 5, 1 5, 1 1, 5 1, 5 5, 6 5, 6 0, 0 0))"
    17  	rectangle1 := "2 2 4 4"
    18  	rectangle2 := "6 6 10 10"
    19  	rectangle3 := "0 0 6 6"
    20  
    21  	bboxIndex := NewBBoxIndex()
    22  	err = bboxIndex.Execute(store, nil, "")
    23  	assert.NoError(t, err)
    24  	quadTreeIndex := NewQuadTreeIndex()
    25  	err = quadTreeIndex.Execute(store, nil, "")
    26  	assert.NoError(t, err)
    27  
    28  	{
    29  		session := openSessionMust(t, store)
    30  
    31  		doc := &SpatialDoc{
    32  			Shape: polygon,
    33  		}
    34  		err = session.Store(doc)
    35  		assert.NoError(t, err)
    36  
    37  		err = session.SaveChanges()
    38  		assert.NoError(t, err)
    39  
    40  		session.Close()
    41  	}
    42  
    43  	err = driver.waitForIndexing(store, "", 0)
    44  	assert.NoError(t, err)
    45  
    46  	{
    47  		session := openSessionMust(t, store)
    48  
    49  		clazz := reflect.TypeOf(&SpatialDoc{})
    50  		q := session.QueryCollectionForType(clazz)
    51  		result, err := q.Count()
    52  		assert.NoError(t, err)
    53  		assert.Equal(t, result, 1)
    54  
    55  		session.Close()
    56  	}
    57  
    58  	{
    59  		session := openSessionMust(t, store)
    60  
    61  		q := session.QueryIndex(bboxIndex.IndexName)
    62  		clause := func(x *ravendb.SpatialCriteriaFactory) ravendb.SpatialCriteria {
    63  			return x.Intersects(rectangle1)
    64  		}
    65  		q.Spatial3("shape", clause)
    66  		result, err := q.Count()
    67  		assert.NoError(t, err)
    68  		assert.Equal(t, result, 1)
    69  
    70  		session.Close()
    71  	}
    72  
    73  	{
    74  		session := openSessionMust(t, store)
    75  
    76  		q := session.QueryIndex(bboxIndex.IndexName)
    77  		clause := func(x *ravendb.SpatialCriteriaFactory) ravendb.SpatialCriteria {
    78  			return x.Intersects(rectangle2)
    79  		}
    80  		q.Spatial3("shape", clause)
    81  		result, err := q.Count()
    82  		assert.NoError(t, err)
    83  		assert.Equal(t, result, 0)
    84  
    85  		session.Close()
    86  	}
    87  
    88  	{
    89  		session := openSessionMust(t, store)
    90  
    91  		q := session.QueryIndex(bboxIndex.IndexName)
    92  		clause := func(x *ravendb.SpatialCriteriaFactory) ravendb.SpatialCriteria {
    93  			return x.Disjoint(rectangle2)
    94  		}
    95  		q.Spatial3("shape", clause)
    96  		result, err := q.Count()
    97  		assert.NoError(t, err)
    98  		assert.Equal(t, result, 1)
    99  
   100  		session.Close()
   101  	}
   102  
   103  	{
   104  		session := openSessionMust(t, store)
   105  
   106  		q := session.QueryIndex(bboxIndex.IndexName)
   107  		clause := func(x *ravendb.SpatialCriteriaFactory) ravendb.SpatialCriteria {
   108  			return x.Within(rectangle3)
   109  		}
   110  		q.Spatial3("shape", clause)
   111  		result, err := q.Count()
   112  		assert.NoError(t, err)
   113  		assert.Equal(t, result, 1)
   114  
   115  		session.Close()
   116  	}
   117  
   118  	{
   119  		session := openSessionMust(t, store)
   120  
   121  		q := session.QueryIndex(quadTreeIndex.IndexName)
   122  		clause := func(x *ravendb.SpatialCriteriaFactory) ravendb.SpatialCriteria {
   123  			return x.Intersects(rectangle2)
   124  		}
   125  		q.Spatial3("shape", clause)
   126  		result, err := q.Count()
   127  		assert.NoError(t, err)
   128  		assert.Equal(t, result, 0)
   129  
   130  		session.Close()
   131  	}
   132  
   133  	{
   134  		session := openSessionMust(t, store)
   135  
   136  		q := session.QueryIndex(quadTreeIndex.IndexName)
   137  		clause := func(x *ravendb.SpatialCriteriaFactory) ravendb.SpatialCriteria {
   138  			return x.Intersects(rectangle1)
   139  		}
   140  		q.Spatial3("shape", clause)
   141  		result, err := q.Count()
   142  		assert.NoError(t, err)
   143  		assert.Equal(t, result, 0)
   144  
   145  		session.Close()
   146  	}
   147  }
   148  
   149  type SpatialDoc struct {
   150  	ID    string
   151  	Shape string `json:"shape"`
   152  }
   153  
   154  func NewBBoxIndex() *ravendb.IndexCreationTask {
   155  	res := ravendb.NewIndexCreationTask("BBoxIndex")
   156  	res.Map = "docs.SpatialDocs.Select(doc => new {\n" +
   157  		"    shape = this.CreateSpatialField(doc.shape)\n" +
   158  		"})"
   159  	indexing := func() *ravendb.SpatialOptions {
   160  		return ravendb.NewGeograpyboundingBoxIndex()
   161  	}
   162  	res.Spatial("shape", indexing)
   163  	return res
   164  }
   165  
   166  func NewQuadTreeIndex() *ravendb.IndexCreationTask {
   167  	res := ravendb.NewIndexCreationTask("QuadTreeIndex")
   168  
   169  	res.Map = `docs.SpatialDocs.Select(doc => new {
   170     shape = this.CreateSpatialField(doc.shape)
   171  })`
   172  	indexing := func() *ravendb.SpatialOptions {
   173  		bounds := ravendb.NewSpatialBounds(0, 0, 16, 16)
   174  		return ravendb.NewCartesianQuadPrefixTreeIndex(6, bounds)
   175  	}
   176  	res.Spatial("shape", indexing)
   177  	return res
   178  }
   179  
   180  func TestBoundingBox(t *testing.T) {
   181  	driver := createTestDriver(t)
   182  	destroy := func() { destroyDriver(t, driver) }
   183  	defer recoverTest(t, destroy)
   184  
   185  	// matches order of Java tests
   186  	boundingBox_boundingBoxTest(t, driver)
   187  }