From f847c0c447e4a21a0f97f751c74d642b3e916ccf Mon Sep 17 00:00:00 2001 From: Sebastian Delgado Date: Fri, 25 May 2018 11:08:28 -0300 Subject: [PATCH] Basic tests with mocks Basic test setup with mocks using dictionary.go as an example. --- Makefile | 13 +++++ README.md | 12 ++++- dictionary.go | 1 - dictionary_test.go | 120 +++++++++++++++++++++++++++++++++++++++++++++ sdk_mock.go | 50 +++++++++++++++++++ sdk_test.go | 17 +++++++ 6 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 dictionary_test.go create mode 100644 sdk_mock.go create mode 100644 sdk_test.go diff --git a/Makefile b/Makefile index 28f12a3..f76786d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index a8e8845..f96d84e 100644 --- a/README.md +++ b/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 ``` -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: diff --git a/dictionary.go b/dictionary.go index 1816579..7cfba9c 100644 --- a/dictionary.go +++ b/dictionary.go @@ -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) } diff --git a/dictionary_test.go b/dictionary_test.go new file mode 100644 index 0000000..b66288b --- /dev/null +++ b/dictionary_test.go @@ -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) + }) + } +} diff --git a/sdk_mock.go b/sdk_mock.go new file mode 100644 index 0000000..51ceb4d --- /dev/null +++ b/sdk_mock.go @@ -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...) +} diff --git a/sdk_test.go b/sdk_test.go new file mode 100644 index 0000000..6ebaf86 --- /dev/null +++ b/sdk_test.go @@ -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) +}