Updated tests

This commit is contained in:
Alfred Kamanda 2024-10-24 18:15:54 +03:00
parent 728815f0c6
commit a92c640cb7
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -494,26 +494,30 @@ func TestGetSender(t *testing.T) {
} }
func TestGetAmount(t *testing.T) { func TestGetAmount(t *testing.T) {
mockStore := new(mocks.MockUserDataStore) mockDataStore := new(mocks.MockUserDataStore)
// Define test data // Define test data
sessionId := "session123" sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId) ctx := context.WithValue(context.Background(), "SessionId", sessionId)
Amount := "0.03CELO" amount := "0.03"
activeSym := "SRF"
// Set up the expected behavior of the mock // Set up the expected behavior of the mock
mockStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return([]byte(Amount), nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(activeSym), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return([]byte(amount), nil)
// Create the Handlers instance with the mock store // Create the Handlers instance with the mock store
h := &Handlers{ h := &Handlers{
userdataStore: mockStore, userdataStore: mockDataStore,
} }
// Call the method // Call the method
res, _ := h.GetAmount(ctx, "get_amount", []byte("Getting amount...")) res, _ := h.GetAmount(ctx, "get_amount", []byte(""))
formattedAmount := fmt.Sprintf("%s %s", amount, activeSym)
//Assert that the retrieved amount is what was set as the content //Assert that the retrieved amount is what was set as the content
assert.Equal(t, Amount, res.Content) assert.Equal(t, formattedAmount, res.Content)
} }
@ -1276,16 +1280,18 @@ func TestInitiateTransaction(t *testing.T) {
input []byte input []byte
Recipient []byte Recipient []byte
Amount []byte Amount []byte
ActiveSym []byte
status string status string
expectedResult resource.Result expectedResult resource.Result
}{ }{
{ {
name: "Test initiate transaction", name: "Test initiate transaction",
Amount: []byte("0.002 CELO"), Amount: []byte("0.002"),
ActiveSym: []byte("SRF"),
Recipient: []byte("0x12415ass27192"), Recipient: []byte("0x12415ass27192"),
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagReset: []uint32{account_authorized_flag}, FlagReset: []uint32{account_authorized_flag},
Content: "Your request has been sent. 0x12415ass27192 will receive 0.002 CELO from 254712345678.", Content: "Your request has been sent. 0x12415ass27192 will receive 0.002 SRF from 254712345678.",
}, },
}, },
} }
@ -1294,6 +1300,7 @@ func TestInitiateTransaction(t *testing.T) {
// Define expected interactions with the mock // Define expected interactions with the mock
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return(tt.Amount, nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_AMOUNT).Return(tt.Amount, nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_RECIPIENT).Return(tt.Recipient, nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_RECIPIENT).Return(tt.Recipient, nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return(tt.ActiveSym, nil)
// Call the method under test // Call the method under test
res, _ := h.InitiateTransaction(ctx, "transaction_reset_amount", tt.input) res, _ := h.InitiateTransaction(ctx, "transaction_reset_amount", tt.input)
@ -1444,28 +1451,28 @@ func TestValidateAmount(t *testing.T) {
}{ }{
{ {
name: "Test with valid amount", name: "Test with valid amount",
input: []byte("0.001"), input: []byte("4.10"),
activeBal: []byte("0.003"), activeBal: []byte("5"),
expectedResult: resource.Result{ expectedResult: resource.Result{
Content: "0.001", Content: "4.10",
}, },
}, },
{ {
name: "Test with amount larger than active balance", name: "Test with amount larger than active balance",
input: []byte("0.02"), input: []byte("5.02"),
activeBal: []byte("0.003"), activeBal: []byte("5"),
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_amount}, FlagSet: []uint32{flag_invalid_amount},
Content: "0.02", Content: "5.02",
}, },
}, },
{ {
name: "Test with invalid amount format", name: "Test with invalid amount format",
input: []byte("0.02ms"), input: []byte("0.02ms"),
balance: "0.003 CELO", activeBal: []byte("5"),
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_amount}, FlagSet: []uint32{flag_invalid_amount},
Content: "0.02ms", Content: "0.02ms",
}, },
}, },
} }
@ -1567,7 +1574,7 @@ func TestCheckBalance(t *testing.T) {
publicKey: "0X98765432109", publicKey: "0X98765432109",
activeSym: "ETH", activeSym: "ETH",
activeBal: "1.5", activeBal: "1.5",
expectedResult: resource.Result{Content: "1.5 ETH"}, expectedResult: resource.Result{Content: "Balance: 1.5 ETH\n"},
expectError: false, expectError: false,
}, },
} }