Skip to content

Commit

Permalink
Fix init and add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fat-fellow committed Jul 22, 2024
1 parent 56d8dd2 commit 877018f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 15 deletions.
81 changes: 69 additions & 12 deletions go/main_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"encoding/base64"
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/anyproto/tantivy-go/go/tantivy"
Expand All @@ -16,11 +18,11 @@ const minGram = 2
func Test(t *testing.T) {

t.Run("docs search and remove - when by raw Id", func(t *testing.T) {
err, schema, index := fx(t, limit, minGram, false)
schema, index := fx(t, limit, minGram, false)

defer index.Free()

doc, err := addDoc(t, "Example Title", "Example body content.", "1", index)
doc, err := addDoc(t, "Example Title", "Example body doing.", "1", index)
require.NoError(t, err)

err = index.AddAndConsumeDocuments(doc)
Expand All @@ -46,13 +48,13 @@ func Test(t *testing.T) {
require.Equal(t, DocSample{
"Example Title",
"1",
"Example body content.",
"Example body doing.",
[]Highlight{
{
NameBody,
Fragment{
[][2]int{{8, 12}},
"RXhhbXBsZSBib2R5IGNvbnRlbnQ=",
base64.StdEncoding.EncodeToString([]byte("Example body doing")),
},
}},
},
Expand All @@ -70,7 +72,7 @@ func Test(t *testing.T) {
})

t.Run("docs remove - when by body token", func(t *testing.T) {
err, _, index := fx(t, limit, minGram, false)
_, index := fx(t, limit, minGram, false)

defer index.Free()

Expand All @@ -93,7 +95,7 @@ func Test(t *testing.T) {
})

t.Run("docs remove - when by wrong body token", func(t *testing.T) {
err, _, index := fx(t, limit, minGram, false)
_, index := fx(t, limit, minGram, false)

defer index.Free()

Expand All @@ -116,7 +118,7 @@ func Test(t *testing.T) {
})

t.Run("docs remove - when by proper token and wrong field length", func(t *testing.T) {
err, _, index := fx(t, 1, minGram, false)
_, index := fx(t, 1, minGram, false)

defer index.Free()

Expand All @@ -138,7 +140,7 @@ func Test(t *testing.T) {
})

t.Run("docs search and remove - when thai", func(t *testing.T) {
err, _, index := fx(t, limit, 1, false)
_, index := fx(t, limit, 1, false)

defer index.Free()

Expand Down Expand Up @@ -178,7 +180,7 @@ func Test(t *testing.T) {
})

t.Run("docs search and remove - when fast", func(t *testing.T) {
err, _, index := fx(t, limit, minGram, false)
_, index := fx(t, limit, minGram, false)

defer index.Free()

Expand All @@ -204,6 +206,60 @@ func Test(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(0), docs)
})

t.Run("docs search and remove - when title", func(t *testing.T) {
schema, index := fx(t, limit, minGram, false)

defer index.Free()

doc, err := addDoc(t, "Create Body", "Example title content.", "1", index)
require.NoError(t, err)

err = index.AddAndConsumeDocuments(doc)
require.NoError(t, err)

result, err := index.Search("create", 100, true, NameTitle)
require.NoError(t, err)

size, err := result.GetSize()
require.Equal(t, 1, int(size))

results, err := tantivy.GetSearchResults(result, schema, func(jsonStr string) (interface{}, error) {
var doc DocSample
return doc, json.Unmarshal([]byte(jsonStr), &doc)
}, NameId, NameTitle, NameBody)
require.NoError(t, err)

require.Equal(t, len(results), int(size))
require.NoError(t, err)

for next := range results {
model := results[next].(DocSample)
require.Equal(t, DocSample{
"Create Body",
"1",
"Example title content.",
[]Highlight{
{
NameTitle,
Fragment{
[][2]int{{0, 2}, {0, 3}, {0, 4}},
base64.StdEncoding.EncodeToString([]byte("Crea")),
},
}},
},
model)
}

docs, err := index.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)

err = index.DeleteDocuments(NameId, "1")
require.NoError(t, err)
docs, err = index.NumDocs()
require.Equal(t, uint64(0), docs)
})
}

func addDoc(
Expand All @@ -230,8 +286,9 @@ func fx(
limit uintptr,
minGram uintptr,
isFastId bool,
) (error, *tantivy.Schema, *tantivy.Index) {
tantivy.LibInit("release")
) (*tantivy.Schema, *tantivy.Index) {
err := tantivy.LibInit("release")
assert.NoError(t, err)
builder, err := tantivy.NewSchemaBuilder()
require.NoError(t, err)

Expand Down Expand Up @@ -280,5 +337,5 @@ func fx(

err = index.RegisterTextAnalyzerRaw(tantivy.TokenizerRaw)
require.NoError(t, err)
return err, schema, index
return schema, index
}
2 changes: 1 addition & 1 deletion go/tantivy/searchresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *SearchResult) GetSize() (uint64, error) {
errorMessage := C.GoString(errBuffer)
defer C.string_free(errBuffer) // Освобождение C строки после использования

if len(errorMessage) == 0 {
if errorMessage == "" {
return uint64(size), nil
} else {
return uint64(0), fmt.Errorf(errorMessage)
Expand Down
2 changes: 1 addition & 1 deletion go/tantivy/tantivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func LibInit(directive ...string) error {
errorMessage := C.GoString(errBuffer)
defer C.string_free(errBuffer)

if len(errorMessage) == 0 {
if errorMessage != "" {
err = fmt.Errorf(errorMessage)
}
})
Expand Down
2 changes: 1 addition & 1 deletion go/tantivy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func tryExtractError(errBuffer *C.char) error {
errorMessage := C.GoString(errBuffer)
defer C.string_free(errBuffer)

if len(errorMessage) == 0 {
if errorMessage == "" {
return nil
} else {
return fmt.Errorf(errorMessage)
Expand Down

0 comments on commit 877018f

Please sign in to comment.