update the TestCheckBalance

This commit is contained in:
Alfred Kamanda 2024-10-12 16:29:12 +03:00
parent e9684fcf45
commit b6d24bf929
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -1560,33 +1560,72 @@ func TestValidateRecipient(t *testing.T) {
} }
func TestCheckBalance(t *testing.T) { func TestCheckBalance(t *testing.T) {
tests := []struct {
mockDataStore := new(mocks.MockUserDataStore) name string
sessionId := "session123" sessionId string
publicKey := "0X13242618721" publicKey string
balance := "0.003 CELO" activeSym string
activeBal string
expectedResult := resource.Result{ expectedResult resource.Result
Content: "0.003 CELO", expectError bool
}{
{
name: "User with active sym",
sessionId: "session456",
publicKey: "0X98765432109",
activeSym: "ETH",
activeBal: "1.5",
expectedResult: resource.Result{Content: "1.5 ETH"},
expectError: false,
},
{
name: "User without active sym",
sessionId: "session123",
publicKey: "0X13242618721",
activeSym: "",
activeBal: "",
expectedResult: resource.Result{Content: "0.003 CELO"},
expectError: false,
},
} }
mockCreateAccountService := new(mocks.MockAccountService) for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), "SessionId", sessionId) mockDataStore := new(mocks.MockUserDataStore)
mockAccountService := new(mocks.MockAccountService)
ctx := context.WithValue(context.Background(), "SessionId", tt.sessionId)
h := &Handlers{ h := &Handlers{
userdataStore: mockDataStore, userdataStore: mockDataStore,
accountService: mockCreateAccountService, accountService: mockAccountService,
//flagManager: fm.parser,
} }
//mock call operations
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(balance, nil)
res, _ := h.CheckBalance(ctx, "check_balance", []byte("123456")) // Mock calls for public key
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(tt.publicKey), nil)
assert.Equal(t, res, expectedResult, "Result should contain flag(s) that have been reset") if tt.activeSym == "" {
// Mock for user without active sym
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte{}, db.ErrNotFound{})
mockAccountService.On("CheckBalance", tt.publicKey).Return("0.003 CELO", nil)
} else {
// Mock for user with active sym
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(tt.activeSym), nil)
mockDataStore.On("ReadEntry", ctx, tt.sessionId, utils.DATA_ACTIVE_BAL).Return([]byte(tt.activeBal), nil)
}
res, err := h.CheckBalance(ctx, "check_balance", []byte("123456"))
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedResult, res, "Result should match expected output")
}
mockDataStore.AssertExpectations(t)
mockAccountService.AssertExpectations(t)
})
}
} }
func TestGetProfile(t *testing.T) { func TestGetProfile(t *testing.T) {