Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
fix do option from empty context and add tests (#47)
Browse files Browse the repository at this point in the history
* fix do option from empty context and add tests

* set real coverage
  • Loading branch information
inotnako authored May 15, 2020
1 parent 1d59268 commit d6a6bba
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ install:
- go mod vendor

script:
- go test -v -mod=vendor ./...
- $GOPATH/bin/goveralls -service=travis-ci || true
- go test -race -mod=vendor -covermode=atomic -coverpkg=github.com/s7techlab/cckit/... -coverprofile=profile.cov ./...
- $GOPATH/bin/goveralls -coverprofile=profile.cov -service=travis-ci || true

go:
- 1.13.x
Expand Down
4 changes: 2 additions & 2 deletions gateway/service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func ContextWithDoOption(ctx context.Context, doOpts ...api.DoOption) context.Co
}

func DoOptionFromContext(ctx context.Context) []api.DoOption {
doOpts := ctx.Value(CtxDoOptionKey).([]api.DoOption)
if doOpts == nil {
doOpts, ok := ctx.Value(CtxDoOptionKey).([]api.DoOption)
if !ok {
doOpts = []api.DoOption{}
}
return doOpts
Expand Down
73 changes: 73 additions & 0 deletions gateway/service/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package service_test

import (
"context"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/s7techlab/hlf-sdk-go/api"
"github.com/s7techlab/hlf-sdk-go/client/chaincode"
"github.com/s7techlab/hlf-sdk-go/client/chaincode/txwaiter"

"github.com/s7techlab/cckit/gateway/service"
)

func TestContext(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Context Suite")
}

var (
doOptsEmpty = []api.DoOption{}
doOptsAll = []api.DoOption{chaincode.WithTxWaiter(txwaiter.All)}
doOptsSelf = []api.DoOption{chaincode.WithTxWaiter(txwaiter.Self)}
)

var _ = Describe(`DoOption`, func() {

It("Set Default DoOption", func() {
ctx := service.ContextWithDefaultDoOption(context.Background(), doOptsSelf...)
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsSelf))
})

It("Default dont allow update on ctx DoOption", func() {
ctx := service.ContextWithDefaultDoOption(context.Background(), doOptsAll...)
ctx = service.ContextWithDefaultDoOption(ctx, doOptsSelf...)
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsAll))
})

It("Set DoOption to Context", func() {
ctx := service.ContextWithDoOption(context.Background(), doOptsSelf...)
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsSelf))
})

It("Update DoOption to Context", func() {
ctx := service.ContextWithDoOption(context.Background(), doOptsAll...)
ctx = service.ContextWithDoOption(ctx, doOptsSelf...)
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsSelf))
})

It("Update DoOption to Context", func() {
ctx := service.ContextWithDoOption(context.Background(), doOptsAll...)
ctx = service.ContextWithDoOption(ctx, doOptsSelf...)
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsSelf))
})

It("Allow get DoOptions from empty Context", func() {
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsEmpty))
})

It("Allow get DoOptions from filled Context", func() {
ctx := service.ContextWithDoOption(ctx, doOptsSelf...)
result := service.DoOptionFromContext(ctx)
Expect(result).To(Equal(doOptsSelf))
})
})

0 comments on commit d6a6bba

Please sign in to comment.