github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/couchdb/mango/index_test.go (about)

     1  package mango
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestIndexMarshaling(t *testing.T) {
    11  	t.Run("WithFields", func(t *testing.T) {
    12  		def := MakeIndex("io.cozy.foo", "my-index", IndexDef{Fields: []string{"dir_id", "name"}})
    13  		jsonbytes, _ := json.Marshal(def.Request)
    14  		expected := `{"ddoc":"my-index","index":{"fields":["dir_id","name"]}}`
    15  		assert.Equal(t, expected, string(jsonbytes), "index should MarshalJSON properly")
    16  	})
    17  
    18  	t.Run("WithFieldsAndPartialFilter", func(t *testing.T) {
    19  		def := MakeIndex("io.cozy.foo", "my-index", IndexDef{Fields: []string{"dir_id", "name"}, PartialFilter: And(NotExists("trashed"), In("name", []interface{}{"file1", "file2"}))})
    20  		jsonbytes, _ := json.Marshal(def.Request)
    21  		expected := `{"ddoc":"my-index","index":{"fields":["dir_id","name"],"partial_filter_selector":{"$and":[{"trashed":{"$exists":false}},{"name":{"$in":["file1","file2"]}}]}}}`
    22  		assert.Equal(t, expected, string(jsonbytes), "index should MarshalJSON properly")
    23  	})
    24  }