Basic tests with mocks

Basic test setup with mocks using dictionary.go as an example.
This commit is contained in:
Sebastian Delgado 2018-05-25 11:08:28 -03:00
parent a5d2dbafc9
commit f847c0c447
No known key found for this signature in database
GPG Key ID: F6A589DA7A8AA59C
6 changed files with 210 additions and 3 deletions

View File

@ -5,3 +5,16 @@ lint-install:
lint:
@echo "lint"
@gometalinter ./...
UNIT_TEST_PACKAGES := $(shell go list ./...)
test:
go test -coverpkg= $(UNIT_TEST_PACKAGES)
dev-deps:
go get -u github.com/stretchr/testify
go get -u github.com/golang/mock/gomock
go get -u github.com/golang/mock/mockgen
mock:
mockgen -package=sdk -destination=sdk_mock.go -source=sdk.go

View File

@ -56,16 +56,24 @@ status-go-sdk is a lightweight dependency package, that means we try to avoid as
```
go get github.com/status-im/status-go-sdk
```
However, and to run some examples you may also want to install `go-ethereum/rpc` with
However, and to run some examples or you may also want to install `go-ethereum/rpc` with
```
go get github.com/ethereum/go-ethereum/rpc
```
If you are developing, run:
```
make dev-deps
```
## API Reference
TBD
## Tests
TBD
status-go-sdk currently runs tests under
```
make test
```
End-to-end tests are still in the works
## How to use?
`status-go-sdk` relies on a running instance of `statusd`, we can quickly configure it by [following its official instructions](https://github.com/status-im/status-go#build), but you can use this as a quick-start:

View File

@ -69,7 +69,6 @@ func statusLoginRequest(sdk *SDK, address, password string) (*loginResponse, err
Address: address,
Password: password,
}
return &res, sdk.RPCClient.Call(&res, "status_login", params)
}

120
dictionary_test.go Normal file
View File

@ -0,0 +1,120 @@
package sdk
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func TestDictionaryMethods(t *testing.T) {
var str string
var genericRes interface{}
testCases := []struct {
Description string
Response interface{}
Method string
Params interface{}
Callback func(*SDK)
}{
{
Description: "statusLoginRequest",
Response: &loginResponse{},
Method: "status_login",
Params: &statusLoginParam{
Address: "ADDRESS",
Password: "PASSWORD",
},
Callback: func(sdk *SDK) {
response, err := statusLoginRequest(sdk, "ADDRESS", "PASSWORD")
assert.NoError(t, err)
assert.NotNil(t, response)
},
},
{
Description: "statusSignupRequest",
Response: &signupResponse{},
Method: "status_signup",
Params: &statusSignupParam{
Password: "PASSWORD",
},
Callback: func(sdk *SDK) {
response, err := statusSignupRequest(sdk, "PASSWORD")
assert.NoError(t, err)
assert.NotNil(t, response)
},
},
{
Description: "shhGenerateSymKeyFromPasswordRequest",
Response: &str,
Method: "shh_generateSymKeyFromPassword",
Params: "PASSWORD",
Callback: func(sdk *SDK) {
response, err := shhGenerateSymKeyFromPasswordRequest(sdk, "PASSWORD")
assert.NoError(t, err)
assert.NotNil(t, response)
},
},
{
Description: "shhPostRequest",
Response: &str,
Method: "shh_post",
Params: &Message{},
Callback: func(sdk *SDK) {
response, err := shhPostRequest(sdk, &Message{})
assert.NoError(t, err)
assert.NotNil(t, response)
},
},
{
Description: "shhGetFilterMessagesRequest",
Response: &genericRes,
Method: "shh_getFilterMessages",
Params: "",
Callback: func(sdk *SDK) {
response, err := shhGetFilterMessagesRequest(sdk, "")
assert.NoError(t, err)
assert.Nil(t, response)
},
},
{
Description: "web3Sha3Request",
Response: &str,
Method: "web3_sha3",
Params: "",
Callback: func(sdk *SDK) {
response, err := web3Sha3Request(sdk, "")
assert.NoError(t, err)
assert.NotNil(t, response)
},
},
{
Description: "newShhMessageFilterFormatRequest",
Response: &str,
Method: "shh_newMessageFilter",
Params: &shhFilterFormatParam{
AllowP2P: true,
Topics: []string{},
Type: "sym",
SymKeyID: "",
},
Callback: func(sdk *SDK) {
response, err := newShhMessageFilterFormatRequest(sdk, []string{}, "")
assert.NoError(t, err)
assert.NotNil(t, response)
},
},
}
for _, tc := range testCases {
t.Run(tc.Description, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
rpcClient := NewMockRPCClient(mockCtrl)
sdk := New(rpcClient)
rpcClient.EXPECT().Call(tc.Response, tc.Method, tc.Params)
tc.Callback(sdk)
})
}
}

50
sdk_mock.go Normal file
View File

@ -0,0 +1,50 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: sdk.go
// Package sdk is a generated GoMock package.
package sdk
import (
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
// MockRPCClient is a mock of RPCClient interface
type MockRPCClient struct {
ctrl *gomock.Controller
recorder *MockRPCClientMockRecorder
}
// MockRPCClientMockRecorder is the mock recorder for MockRPCClient
type MockRPCClientMockRecorder struct {
mock *MockRPCClient
}
// NewMockRPCClient creates a new mock instance
func NewMockRPCClient(ctrl *gomock.Controller) *MockRPCClient {
mock := &MockRPCClient{ctrl: ctrl}
mock.recorder = &MockRPCClientMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockRPCClient) EXPECT() *MockRPCClientMockRecorder {
return m.recorder
}
// Call mocks base method
func (m *MockRPCClient) Call(result interface{}, method string, args ...interface{}) error {
varargs := []interface{}{result, method}
for _, a := range args {
varargs = append(varargs, a)
}
ret := m.ctrl.Call(m, "Call", varargs...)
ret0, _ := ret[0].(error)
return ret0
}
// Call indicates an expected call of Call
func (mr *MockRPCClientMockRecorder) Call(result, method interface{}, args ...interface{}) *gomock.Call {
varargs := append([]interface{}{result, method}, args...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Call", reflect.TypeOf((*MockRPCClient)(nil).Call), varargs...)
}

17
sdk_test.go Normal file
View File

@ -0,0 +1,17 @@
package sdk
import (
"testing"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
)
func TestNewSDK(t *testing.T) {
rpcClient, err := rpc.Dial("http://localhost:8545")
assert.NoError(t, err)
client := New(rpcClient)
assert.Equal(t, 0.001, client.minimumPoW)
assert.Equal(t, rpcClient, client.RPCClient)
}