github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/samples/go/decent/lib/term_index_test.go (about)

     1  // See: https://github.com/attic-labs/noms/issues/3808
     2  // +build ignore
     3  
     4  // Copyright 2017 Attic Labs, Inc. All rights reserved.
     5  // Licensed under the Apache License, version 2.0:
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  
     8  package lib
     9  
    10  import (
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/attic-labs/noms/go/chunks"
    15  	"github.com/attic-labs/noms/go/types"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestRun(t *testing.T) {
    20  	a := assert.New(t)
    21  
    22  	storage := &chunks.MemoryStorage{}
    23  	vs := types.NewValueStore(storage.NewView())
    24  	defer vs.Close()
    25  
    26  	docs := []struct {
    27  		terms string
    28  		id    int
    29  	}{
    30  		{"foo bar baz", 1},
    31  		{"foo baz", 2},
    32  		{"baz bat boo", 3},
    33  	}
    34  
    35  	indexEditor := NewTermIndex(vs, types.NewMap(vs)).Edit()
    36  	for _, doc := range docs {
    37  		indexEditor.InsertAll(strings.Split(doc.terms, " "), types.Number(doc.id))
    38  	}
    39  
    40  	index := indexEditor.Value()
    41  
    42  	getMap := func(keys ...int) types.Map {
    43  		m := types.NewMap(vs).Edit()
    44  		for _, k := range keys {
    45  			m.Set(types.Number(k), types.Bool(true))
    46  		}
    47  		return m.Map()
    48  	}
    49  
    50  	test := func(search string, expect types.Map) {
    51  		actual := index.Search(strings.Split(search, " "))
    52  		a.True(expect.Equals(actual))
    53  	}
    54  
    55  	test("foo", getMap(1, 2))
    56  	test("baz", getMap(1, 2, 3))
    57  	test("bar baz", getMap(1))
    58  	test("boo", getMap(3))
    59  	test("blarg", getMap())
    60  }