Basic tests with mocks
Basic test setup with mocks using dictionary.go as an example.
This commit is contained in:
parent
a5d2dbafc9
commit
f847c0c447
13
Makefile
13
Makefile
|
@ -5,3 +5,16 @@ lint-install:
|
||||||
lint:
|
lint:
|
||||||
@echo "lint"
|
@echo "lint"
|
||||||
@gometalinter ./...
|
@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
|
||||||
|
|
12
README.md
12
README.md
|
@ -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
|
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
|
go get github.com/ethereum/go-ethereum/rpc
|
||||||
```
|
```
|
||||||
|
If you are developing, run:
|
||||||
|
```
|
||||||
|
make dev-deps
|
||||||
|
```
|
||||||
|
|
||||||
## API Reference
|
## API Reference
|
||||||
TBD
|
TBD
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
TBD
|
status-go-sdk currently runs tests under
|
||||||
|
```
|
||||||
|
make test
|
||||||
|
```
|
||||||
|
End-to-end tests are still in the works
|
||||||
|
|
||||||
## How to use?
|
## 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:
|
`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:
|
||||||
|
|
|
@ -69,7 +69,6 @@ func statusLoginRequest(sdk *SDK, address, password string) (*loginResponse, err
|
||||||
Address: address,
|
Address: address,
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &res, sdk.RPCClient.Call(&res, "status_login", params)
|
return &res, sdk.RPCClient.Call(&res, "status_login", params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -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...)
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue