Finish refactor result models

This commit is contained in:
lash 2024-10-31 01:51:36 +00:00
parent 1e638238ed
commit a48170321c
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -88,18 +88,14 @@ func TestCreateAccount(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
serverResponse *api.OKResponse serverResponse *models.AccountResult
expectedResult resource.Result expectedResult resource.Result
}{ }{
{ {
name: "Test account creation success", name: "Test account creation success",
serverResponse: &api.OKResponse{ serverResponse: &models.AccountResult{
Ok: true, TrackingId: "1234567890",
Description: "Account creation successed", PublicKey: "0xD3adB33f",
Result: map[string]any{
"trackingId": "1234567890",
"publicKey": "0xD3adB33f",
},
}, },
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_account_created}, FlagSet: []uint32{flag_account_created},
@ -119,9 +115,9 @@ func TestCreateAccount(t *testing.T) {
flagManager: fm.parser, flagManager: fm.parser,
} }
publicKey := tt.serverResponse.Result["publicKey"].(string) publicKey := tt.serverResponse.PublicKey
data := map[common.DataTyp]string{ data := map[common.DataTyp]string{
common.DATA_TRACKING_ID: tt.serverResponse.Result["trackingId"].(string), common.DATA_TRACKING_ID: tt.serverResponse.TrackingId,
common.DATA_PUBLIC_KEY: publicKey, common.DATA_PUBLIC_KEY: publicKey,
} }
@ -1055,7 +1051,7 @@ func TestCheckAccountStatus(t *testing.T) {
name string name string
input []byte input []byte
serverResponse *api.OKResponse serverResponse *api.OKResponse
response *models.TrackStatusResponse response *models.TrackStatusResult
expectedResult resource.Result expectedResult resource.Result
}{ }{
{ {
@ -1068,26 +1064,13 @@ func TestCheckAccountStatus(t *testing.T) {
"active": true, "active": true,
}, },
}, },
response: &models.TrackStatusResponse{ response: &models.TrackStatusResult {
Ok: true,
Result: struct {
Transaction struct {
CreatedAt time.Time "json:\"createdAt\""
Status string "json:\"status\""
TransferValue json.Number "json:\"transferValue\""
TxHash string "json:\"txHash\""
TxType string "json:\"txType\""
}
}{
Transaction: models.Transaction{
CreatedAt: time.Now(), CreatedAt: time.Now(),
Status: "SUCCESS", Status: "SUCCESS",
TransferValue: json.Number("0.5"), TransferValue: json.Number("0.5"),
TxHash: "0x123abc456def", TxHash: "0x123abc456def",
TxType: "transfer", TxType: "transfer",
}, },
},
},
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_account_success}, FlagSet: []uint32{flag_account_success},
FlagReset: []uint32{flag_api_error, flag_account_pending}, FlagReset: []uint32{flag_api_error, flag_account_pending},
@ -1096,26 +1079,13 @@ func TestCheckAccountStatus(t *testing.T) {
{ {
name: "Test when the account is not yet on the sarafu network", name: "Test when the account is not yet on the sarafu network",
input: []byte("TrackingId1234"), input: []byte("TrackingId1234"),
response: &models.TrackStatusResponse{ response: &models.TrackStatusResult{
Ok: true,
Result: struct {
Transaction struct {
CreatedAt time.Time "json:\"createdAt\""
Status string "json:\"status\""
TransferValue json.Number "json:\"transferValue\""
TxHash string "json:\"txHash\""
TxType string "json:\"txType\""
}
}{
Transaction: models.Transaction{
CreatedAt: time.Now(), CreatedAt: time.Now(),
Status: "SUCCESS", Status: "SUCCESS",
TransferValue: json.Number("0.5"), TransferValue: json.Number("0.5"),
TxHash: "0x123abc456def", TxHash: "0x123abc456def",
TxType: "transfer", TxType: "transfer",
}, },
},
},
serverResponse: &api.OKResponse{ serverResponse: &api.OKResponse{
Ok: true, Ok: true,
Description: "Account creation succeeded", Description: "Account creation succeeded",
@ -1140,7 +1110,7 @@ func TestCheckAccountStatus(t *testing.T) {
flagManager: fm.parser, flagManager: fm.parser,
} }
status := tt.response.Result.Transaction.Status status := tt.response.Status
// Define expected interactions with the mock // Define expected interactions with the mock
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return(tt.input, nil) mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return(tt.input, nil)
@ -1833,37 +1803,15 @@ func TestFetchCustodialBalances(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
balanceResonse *models.BalanceResponse balanceResponse *models.BalanceResult
expectedResult resource.Result expectedResult resource.Result
}{ }{
{ {
name: "Test when fetch custodial balances is not a success", name: "Test when fetch custodial balances is not a success",
balanceResonse: &models.BalanceResponse{ balanceResponse: &models.BalanceResult{
Ok: false,
Result: struct {
Balance string `json:"balance"`
Nonce json.Number `json:"nonce"`
}{
Balance: "0.003 CELO", Balance: "0.003 CELO",
Nonce: json.Number("0"), Nonce: json.Number("0"),
}, },
},
expectedResult: resource.Result{
FlagSet: []uint32{flag_api_error},
},
},
{
name: "Test when fetch custodial balances is a success",
balanceResonse: &models.BalanceResponse{
Ok: true,
Result: struct {
Balance string `json:"balance"`
Nonce json.Number `json:"nonce"`
}{
Balance: "0.003 CELO",
Nonce: json.Number("0"),
},
},
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagReset: []uint32{flag_api_error}, FlagReset: []uint32{flag_api_error},
}, },
@ -1886,7 +1834,7 @@ func TestFetchCustodialBalances(t *testing.T) {
// Set up the expected behavior of the mock // Set up the expected behavior of the mock
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(tt.balanceResonse, nil) mockCreateAccountService.On("CheckBalance", string(publicKey)).Return(tt.balanceResponse, nil)
// Call the method // Call the method
res, _ := h.FetchCustodialBalances(ctx, "fetch_custodial_balances", []byte("")) res, _ := h.FetchCustodialBalances(ctx, "fetch_custodial_balances", []byte(""))
@ -1918,35 +1866,24 @@ func TestSetDefaultVoucher(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
vouchersResp *models.VoucherHoldingResponse vouchersResp []dataserviceapi.TokenHoldings
expectedResult resource.Result expectedResult resource.Result
}{ }{
{ {
name: "Test set default voucher when no active voucher exists", name: "Test set default voucher when no active voucher exists",
vouchersResp: &models.VoucherHoldingResponse{ vouchersResp: []dataserviceapi.TokenHoldings {
Ok: true, dataserviceapi.TokenHoldings{
Description: "Vouchers fetched successfully",
Result: models.VoucherResult{
Holdings: []dataserviceapi.TokenHoldings{
{
ContractAddress: "0x123", ContractAddress: "0x123",
TokenSymbol: "TOKEN1", TokenSymbol: "TOKEN1",
TokenDecimals: "18", TokenDecimals: "18",
Balance: "100", Balance: "100",
}, },
}, },
},
},
expectedResult: resource.Result{}, expectedResult: resource.Result{},
}, },
{ {
name: "Test no vouchers available", name: "Test no vouchers available",
vouchersResp: &models.VoucherHoldingResponse{ vouchersResp: []dataserviceapi.TokenHoldings {
Ok: true,
Description: "No vouchers available",
Result: models.VoucherResult{
Holdings: []dataserviceapi.TokenHoldings{},
},
}, },
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_no_active_voucher}, FlagSet: []uint32{flag_no_active_voucher},
@ -1970,8 +1907,8 @@ func TestSetDefaultVoucher(t *testing.T) {
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil) mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
if len(tt.vouchersResp.Result.Holdings) > 0 { if len(tt.vouchersResp) > 0 {
firstVoucher := tt.vouchersResp.Result.Holdings[0] firstVoucher := tt.vouchersResp[0]
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(firstVoucher.TokenSymbol)).Return(nil) mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_SYM, []byte(firstVoucher.TokenSymbol)).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(firstVoucher.Balance)).Return(nil) mockDataStore.On("WriteEntry", ctx, sessionId, common.DATA_ACTIVE_BAL, []byte(firstVoucher.Balance)).Return(nil)
} }
@ -2006,8 +1943,7 @@ func TestCheckVouchers(t *testing.T) {
mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) mockDataStore.On("ReadEntry", ctx, sessionId, common.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockVouchersResponse := &models.VoucherHoldingResponse{} mockVouchersResponse := []dataserviceapi.TokenHoldings{
mockVouchersResponse.Result.Holdings = []dataserviceapi.TokenHoldings{
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"}, {ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"}, {ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
} }