Adjust + Test
This commit is contained in:
@@ -3,127 +3,107 @@ package peer_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type MockAccessor struct {
|
||||
mock.Mock
|
||||
utils.AbstractAccessor
|
||||
// ---- PeerRelation ----
|
||||
|
||||
func TestPeerRelation_String(t *testing.T) {
|
||||
assert.Equal(t, "UNKNOWN", peer.NONE.String())
|
||||
assert.Equal(t, "SELF", peer.SELF.String())
|
||||
assert.Equal(t, "PARTNER", peer.PARTNER.String())
|
||||
assert.Equal(t, "BLACKLIST", peer.BLACKLIST.String())
|
||||
}
|
||||
|
||||
func (m *MockAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
func TestPeerRelation_Path(t *testing.T) {
|
||||
assert.Equal(t, "unknown", peer.NONE.Path())
|
||||
assert.Equal(t, "self", peer.SELF.Path())
|
||||
assert.Equal(t, "partner", peer.PARTNER.Path())
|
||||
assert.Equal(t, "blacklist", peer.BLACKLIST.Path())
|
||||
}
|
||||
|
||||
func (m *MockAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(set, id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
func TestPeerRelation_EnumIndex(t *testing.T) {
|
||||
assert.Equal(t, 0, peer.NONE.EnumIndex())
|
||||
assert.Equal(t, 1, peer.SELF.EnumIndex())
|
||||
assert.Equal(t, 2, peer.PARTNER.EnumIndex())
|
||||
assert.Equal(t, 3, peer.BLACKLIST.EnumIndex())
|
||||
assert.Equal(t, 4, peer.PENDING_PARTNER.EnumIndex())
|
||||
}
|
||||
|
||||
func (m *MockAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
args := m.Called(data)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
func TestGetRelationPath(t *testing.T) {
|
||||
assert.Equal(t, 1, peer.GetRelationPath("self"))
|
||||
assert.Equal(t, 2, peer.GetRelationPath("partner"))
|
||||
assert.Equal(t, 3, peer.GetRelationPath("blacklist"))
|
||||
assert.Equal(t, -1, peer.GetRelationPath("nonexistent"))
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(utils.DBObject), args.Int(1), args.Error(2)
|
||||
// ---- Peer model ----
|
||||
|
||||
func TestPeer_VerifyAuth(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
assert.True(t, p.VerifyAuth("get", nil))
|
||||
assert.True(t, p.VerifyAuth("delete", &tools.APIRequest{}))
|
||||
}
|
||||
|
||||
func (m *MockAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
func TestPeer_CanDelete(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
assert.False(t, p.CanDelete())
|
||||
}
|
||||
|
||||
func (m *MockAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
|
||||
args := m.Called(filters, search, isDraft)
|
||||
return args.Get(0).([]utils.ShallowDBObject), args.Int(1), args.Error(2)
|
||||
func TestPeer_GetAccessor(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
req := &tools.APIRequest{}
|
||||
acc := p.GetAccessor(req)
|
||||
assert.NotNil(t, acc)
|
||||
}
|
||||
|
||||
func newTestPeer() *peer.Peer {
|
||||
return &peer.Peer{
|
||||
NATSAddress: "",
|
||||
StreamAddress: "127.0.0.1",
|
||||
APIUrl: "http://localhost",
|
||||
WalletAddress: "0x123",
|
||||
PublicKey: "pubkey",
|
||||
Relation: peer.SELF,
|
||||
}
|
||||
func TestPeer_AddExecution_Deduplication(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
exec := peer.PeerExecution{Method: "POST", Url: "http://peer/data", Body: "body1"}
|
||||
|
||||
p.AddExecution(exec)
|
||||
assert.Len(t, p.FailedExecution, 1)
|
||||
|
||||
// Second add of same execution should not duplicate
|
||||
p.AddExecution(exec)
|
||||
assert.Len(t, p.FailedExecution, 1)
|
||||
|
||||
// Different execution should be added
|
||||
exec2 := peer.PeerExecution{Method: "GET", Url: "http://peer/data", Body: nil}
|
||||
p.AddExecution(exec2)
|
||||
assert.Len(t, p.FailedExecution, 2)
|
||||
}
|
||||
|
||||
func TestDeleteOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
mockAcc.On("DeleteOne", "id").Return(newTestPeer(), 200, nil)
|
||||
func TestPeer_RemoveExecution(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
exec1 := peer.PeerExecution{Method: "POST", Url: "http://peer/a", Body: nil}
|
||||
exec2 := peer.PeerExecution{Method: "DELETE", Url: "http://peer/b", Body: nil}
|
||||
|
||||
obj, code, err := mockAcc.DeleteOne("id")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.NotNil(t, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
p.AddExecution(exec1)
|
||||
p.AddExecution(exec2)
|
||||
assert.Len(t, p.FailedExecution, 2)
|
||||
|
||||
p.RemoveExecution(exec1)
|
||||
assert.Len(t, p.FailedExecution, 1)
|
||||
assert.Equal(t, exec2, p.FailedExecution[0])
|
||||
}
|
||||
|
||||
func TestUpdateOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
peerObj := newTestPeer()
|
||||
mockAcc.On("UpdateOne", peerObj, "id").Return(peerObj, 200, nil)
|
||||
func TestPeer_RemoveExecution_NotFound(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
exec := peer.PeerExecution{Method: "POST", Url: "http://peer/x", Body: nil}
|
||||
p.AddExecution(exec)
|
||||
|
||||
obj, code, err := mockAcc.UpdateOne(peerObj, "id")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, peerObj, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
other := peer.PeerExecution{Method: "DELETE", Url: "http://other/x", Body: nil}
|
||||
p.RemoveExecution(other)
|
||||
assert.Len(t, p.FailedExecution, 1) // unchanged
|
||||
}
|
||||
|
||||
func TestStoreOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
peerObj := newTestPeer()
|
||||
mockAcc.On("StoreOne", peerObj).Return(peerObj, 200, nil)
|
||||
|
||||
obj, code, err := mockAcc.StoreOne(peerObj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, peerObj, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestLoadOne_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
mockAcc.On("LoadOne", "test-id").Return(newTestPeer(), 200, nil)
|
||||
|
||||
obj, code, err := mockAcc.LoadOne("test-id")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.NotNil(t, obj)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestLoadAll_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
expected := []utils.ShallowDBObject{newTestPeer()}
|
||||
mockAcc.On("LoadAll", false).Return(expected, 200, nil)
|
||||
|
||||
objs, code, err := mockAcc.LoadAll(false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, expected, objs)
|
||||
mockAcc.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestSearch_UsingMock(t *testing.T) {
|
||||
mockAcc := new(MockAccessor)
|
||||
filters := &dbs.Filters{}
|
||||
expected := []utils.ShallowDBObject{newTestPeer()}
|
||||
mockAcc.On("Search", filters, "test", false).Return(expected, 200, nil)
|
||||
|
||||
objs, code, err := mockAcc.Search(filters, "test", false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, expected, objs)
|
||||
mockAcc.AssertExpectations(t)
|
||||
func TestPeer_RemoveExecution_Empty(t *testing.T) {
|
||||
p := &peer.Peer{}
|
||||
// Should not panic on empty list
|
||||
exec := peer.PeerExecution{Method: "GET", Url: "http://peer/x", Body: nil}
|
||||
p.RemoveExecution(exec)
|
||||
assert.Empty(t, p.FailedExecution)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user