Skip to content

Commit

Permalink
fixed tests and added coverage calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhatsharma committed May 5, 2022
1 parent 96678e0 commit 120b02c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/zinc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
Release: "zinc@" + v1.Version,
})
if err != nil {
log.Print("sentry.Init: %s", err)
log.Print("sentry.Init: ", err.Error())
}
/******** sentry initialize complete *******/
}
Expand Down
33 changes: 33 additions & 0 deletions coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! /bin/sh

go test ./... -race -covermode=atomic -coverprofile=coverage.out

# make sure to set CODECOV_TOKEN env variable before doing this
# codecov -f coverage.out
# or
# bash <(curl -s https://codecov.io/bash)


# Example setup https://github.com/lluuiissoo/go-testcoverage/blob/main/.github/workflows/ci.yml

# enable threshold
COVERAGE_THRESHOLD=20

totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]'`

echo "Total Coverage is $totalCoverage %"

diff=$(echo "$totalCoverage < $COVERAGE_THRESHOLD" | bc)

if [ $diff -eq 1 ]; then
echo "Coverage is below threshold of $COVERAGE_THRESHOLD %"
exit 1
else
echo "Coverage is above threshold of $COVERAGE_THRESHOLD %"
exit 0
fi





18 changes: 16 additions & 2 deletions pkg/core/newindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
package core

import (
"fmt"
"math/rand"
"os"
"strconv"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)

func TestNewIndex(t *testing.T) {
Convey("test new index storage dick", t, func() {
indexName := "create.new.index"
Convey("test new index storage - disk", t, func() {
rand.Seed(time.Now().UnixNano())
id := rand.Intn(1000)
indexName := "create.new.index_" + strconv.Itoa(id)

index, err := NewIndex(indexName, "disk", UseNewIndexMeta, nil)
So(err, ShouldBeNil)
So(index.Name, ShouldEqual, indexName)
Expand All @@ -34,4 +42,10 @@ func TestNewIndex(t *testing.T) {
Convey("test new index storage minio", t, func() {
// TODO: support
})

// Cleanup data folder
err := os.RemoveAll("data")
if err != nil {
fmt.Println(err)
}
}
19 changes: 9 additions & 10 deletions pkg/handlers/v2/index_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ func UpdateIndexMapping(c *gin.Context) {
return
}

_, exists := core.GetIndex(indexName)
if exists {
c.JSON(http.StatusBadRequest, gin.H{"error": "index [" + indexName + "] already exists"})
return
var index *core.Index
index, exists := core.GetIndex(indexName)
if !exists {
index1, err := core.NewIndex(indexName, newIndex.StorageType, core.UseNewIndexMeta, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
index = index1
}

mappings, err := mappings.Request(nil, newIndex.Mappings)
Expand All @@ -67,12 +72,6 @@ func UpdateIndexMapping(c *gin.Context) {
return
}

index, err := core.NewIndex(indexName, newIndex.StorageType, core.UseNewIndexMeta, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

// update mappings
if mappings != nil && len(mappings.Properties) > 0 {
index.SetMappings(mappings)
Expand Down
5 changes: 4 additions & 1 deletion test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ func TestApiStandard(t *testing.T) {
resp := request("GET", "/api/index", nil)
So(resp.Code, ShouldEqual, http.StatusOK)

data := make(map[string]interface{})
// data := make(map[string]interface{})
data := []interface{}{}
err := json.Unmarshal(resp.Body.Bytes(), &data)
So(err, ShouldBeNil)
So(len(data), ShouldBeGreaterThanOrEqualTo, 1)
Expand Down Expand Up @@ -628,5 +629,7 @@ func TestApiStandard(t *testing.T) {
So(len(data.Aggregations), ShouldBeGreaterThanOrEqualTo, 1)
})
})

})

}

0 comments on commit 120b02c

Please sign in to comment.