Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hengfeiyang committed May 17, 2022
1 parent 1b734ee commit 19a0c9e
Show file tree
Hide file tree
Showing 16 changed files with 824 additions and 739 deletions.
14 changes: 5 additions & 9 deletions coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
export ZINC_FIRST_ADMIN_USER=admin
export ZINC_FIRST_ADMIN_PASSWORD=Complexpass#123

go test ./... -race -covermode=atomic -coverprofile=coverage.out
find ./pkg -name data -type dir|xargs rm -fR
find ./test -name data -type dir|xargs rm -fR

rm -rf ./test/data
rm -rf ./pkg/handlers/data
rm -rf ./pkg/core/data
rm -rf ./pkg/auth/data
go test ./... -race -covermode=atomic -coverprofile=coverage.out

# make sure to set CODECOV_TOKEN env variable before doing this
# codecov -f coverage.out
Expand All @@ -23,10 +21,8 @@ COVERAGE_THRESHOLD=20
totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]'`

# clean up
rm -rf ./test/data
rm -rf ./pkg/handlers/data
rm -rf ./pkg/core/data
rm -rf ./pkg/auth/data
find ./pkg -name data -type dir|xargs rm -fR
find ./test -name data -type dir|xargs rm -fR
rm coverage.out
# clean up finished

Expand Down
2 changes: 2 additions & 0 deletions pkg/handlers/index/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func TestMapping(t *testing.T) {

err = core.StoreIndex(index)
assert.NoError(t, err)
err = index.Close()
assert.NoError(t, err)
})

t.Run("set mapping", func(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/bin/bash

rm -fR pkg/core/data
rm -fR test/data

export ZINC_FIRST_ADMIN_USER="admin"
export ZINC_FIRST_ADMIN_PASSWORD="Complexpass#123"

find ./pkg -name data -type dir|xargs rm -fR
find ./test -name data -type dir|xargs rm -fR

if [[ $1 == "bench" ]]; then
go test -v -test.run=NONE -test.bench=Bulk -benchmem ./test/ -cpuprofile=./tmp/cpu.pprof -memprofile=./tmp/mem.pprof
go test -v -test.run=NONE -test.bench=Bulk -benchmem ./test/benchmark/ -cpuprofile=./tmp/cpu.pprof -memprofile=./tmp/mem.pprof
# go tool pprof -http=:9999 ./tmp/mem.pprof
else
go test -v ./... -test.run=$1
fi

find ./pkg -name data -type dir|xargs rm -fR
find ./test -name data -type dir|xargs rm -fR
77 changes: 45 additions & 32 deletions test/analyze_test.go → test/api/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
* limitations under the License.
*/

package test
package api

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"

"github.com/goccy/go-json"
. "github.com/smartystreets/goconvey/convey"
)

Expand All @@ -46,6 +47,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("standard analyzer with stopwords", func() {
indexName := "my-index-001"
index := `{
"settings": {
"analysis": {
Expand All @@ -67,23 +69,26 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
buf, err := io.ReadAll(resp.Body)
fmt.Println(string(buf), err)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("standard analyzer with stopwords and filters", func() {
indexName := "my-index-002"
index := `{
"settings": {
"analysis": {
Expand Down Expand Up @@ -113,20 +118,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("simple analyzer", func() {
Expand Down Expand Up @@ -181,6 +186,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("regexp analyzer with pattern", func() {
indexName := "my-index-003"
index := `{
"settings": {
"analysis": {
Expand All @@ -203,20 +209,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("stop analyzer", func() {
Expand All @@ -237,6 +243,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("stop analyzer with stopwords", func() {
indexName := "my-index-004"
index := `{
"settings": {
"analysis": {
Expand All @@ -258,20 +265,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("whitespace analyzer", func() {
Expand Down Expand Up @@ -396,6 +403,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("n-gram tokenizer with configuration", func() {
indexName := "my-index-005"
index := `{
"settings": {
"analysis": {
Expand Down Expand Up @@ -427,20 +435,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("edge n-gram tokenizer", func() {
Expand All @@ -461,6 +469,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("edge n-gram tokenizer with configuration", func() {
indexName := "my-index-006"
index := `{
"settings": {
"analysis": {
Expand Down Expand Up @@ -492,20 +501,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("keyword tokenizer", func() {
Expand Down Expand Up @@ -561,6 +570,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("regexp tokenizer with configuration example1", func() {
indexName := "my-index-007"
index := `{
"settings": {
"analysis": {
Expand All @@ -587,23 +597,24 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("regexp tokenizer with configuration example2", func() {
indexName := "my-index-008"
index := `{
"settings": {
"analysis": {
Expand All @@ -630,20 +641,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})

Convey("character group tokenizer", func() {
Expand Down Expand Up @@ -688,6 +699,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("path hierarchy tokenizer with configuration", func() {
indexName := "my-index-009"
index := `{
"settings": {
"analysis": {
Expand Down Expand Up @@ -716,20 +728,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})
})

Expand Down Expand Up @@ -1224,6 +1236,7 @@ func TestAnalyze(t *testing.T) {
})

Convey("Pattern replace character filter", func() {
indexName := "my-index-010"
index := `{
"settings": {
"analysis": {
Expand Down Expand Up @@ -1254,20 +1267,20 @@ func TestAnalyze(t *testing.T) {
// create index with custom analyzer
body := bytes.NewBuffer(nil)
body.WriteString(index)
resp := request("PUT", "/api/index/my-index-001", body)
resp := request("PUT", "/api/index/"+indexName, body)
So(resp.Code, ShouldEqual, http.StatusOK)

// analyze
body.Reset()
body.WriteString(input)
resp = request("POST", "/api/my-index-001/_analyze", body)
resp = request("POST", "/api/"+indexName+"/_analyze", body)
So(resp.Code, ShouldEqual, http.StatusOK)
tokens, err := getTokenStrings(resp.Body.Bytes())
So(err, ShouldBeNil)
So(tokens, ShouldEqual, output)

// delete index
request("DELETE", "/api/index/my-index-001", nil)
request("DELETE", "/api/index/"+indexName, nil)
})
})
}
Expand Down

0 comments on commit 19a0c9e

Please sign in to comment.