From 916026985f0e5bac647316523de9ef7c957f03be Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 3 Jul 2025 17:16:44 +0300 Subject: [PATCH] move balance tests --- handlers/application/balance_test.go | 146 +++++++++++++++++++++++ handlers/application/menuhandler_test.go | 134 --------------------- 2 files changed, 146 insertions(+), 134 deletions(-) create mode 100644 handlers/application/balance_test.go diff --git a/handlers/application/balance_test.go b/handlers/application/balance_test.go new file mode 100644 index 0000000..c8da2ea --- /dev/null +++ b/handlers/application/balance_test.go @@ -0,0 +1,146 @@ +package application + +import ( + "context" + "testing" + + "git.defalsify.org/vise.git/lang" + "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/state" + "git.grassecon.net/grassrootseconomics/sarafu-api/testutil/mocks" + storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" + "github.com/alecthomas/assert/v2" +) + +func TestCheckBalance(t *testing.T) { + ctx, store := InitializeTestStore(t) + + tests := []struct { + name string + sessionId string + publicKey string + alias string + activeSym string + activeBal string + expectedResult resource.Result + expectError bool + }{ + { + name: "User with no active sym", + sessionId: "session123", + publicKey: "0X98765432109", + alias: "", + activeSym: "", + activeBal: "", + expectedResult: resource.Result{Content: "Balance: 0.00 \n"}, + expectError: false, + }, + { + name: "User with active sym", + sessionId: "session123", + publicKey: "0X98765432109", + alias: "", + activeSym: "ETH", + activeBal: "1.5", + expectedResult: resource.Result{Content: "Balance: 1.50 ETH\n"}, + expectError: false, + }, + { + name: "User with active sym and alias", + sessionId: "session123", + publicKey: "0X98765432109", + alias: "user72", + activeSym: "SRF", + activeBal: "10.967", + expectedResult: resource.Result{Content: "user72 balance: 10.96 SRF\n"}, + expectError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockAccountService := new(mocks.MockAccountService) + ctx := context.WithValue(ctx, "SessionId", tt.sessionId) + + h := &MenuHandlers{ + userdataStore: store, + accountService: mockAccountService, + } + + if tt.alias != "" { + err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(tt.alias)) + if err != nil { + t.Fatal(err) + } + } + + if tt.activeSym != "" { + err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym)) + if err != nil { + t.Fatal(err) + } + } + + if tt.activeBal != "" { + err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal)) + if err != nil { + t.Fatal(err) + } + } + + res, err := h.CheckBalance(ctx, "check_balance", []byte("")) + + if tt.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.expectedResult, res, "Result should match expected output") + } + + mockAccountService.AssertExpectations(t) + }) + } +} + +func TestFetchCommunityBalance(t *testing.T) { + // Define test data + sessionId := "session123" + ctx, store := InitializeTestStore(t) + + tests := []struct { + name string + languageCode string + expectedResult resource.Result + }{ + { + name: "Test community balance content when language is english", + expectedResult: resource.Result{ + Content: "Community Balance: 0.00", + }, + languageCode: "eng", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + mockAccountService := new(mocks.MockAccountService) + mockState := state.NewState(16) + + h := &MenuHandlers{ + userdataStore: store, + st: mockState, + accountService: mockAccountService, + } + ctx = context.WithValue(ctx, "SessionId", sessionId) + ctx = context.WithValue(ctx, "Language", lang.Language{ + Code: tt.languageCode, + }) + + // Call the method + res, _ := h.FetchCommunityBalance(ctx, "fetch_community_balance", []byte("")) + + //Assert that the result set to content is what was expected + assert.Equal(t, res, tt.expectedResult, "Result should match expected result") + }) + } +} diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index 85ffe03..dba9749 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -1534,96 +1534,6 @@ func TestValidateRecipient(t *testing.T) { } } -func TestCheckBalance(t *testing.T) { - ctx, store := InitializeTestStore(t) - - tests := []struct { - name string - sessionId string - publicKey string - alias string - activeSym string - activeBal string - expectedResult resource.Result - expectError bool - }{ - { - name: "User with no active sym", - sessionId: "session123", - publicKey: "0X98765432109", - alias: "", - activeSym: "", - activeBal: "", - expectedResult: resource.Result{Content: "Balance: 0.00 \n"}, - expectError: false, - }, - { - name: "User with active sym", - sessionId: "session123", - publicKey: "0X98765432109", - alias: "", - activeSym: "ETH", - activeBal: "1.5", - expectedResult: resource.Result{Content: "Balance: 1.50 ETH\n"}, - expectError: false, - }, - { - name: "User with active sym and alias", - sessionId: "session123", - publicKey: "0X98765432109", - alias: "user72", - activeSym: "SRF", - activeBal: "10.967", - expectedResult: resource.Result{Content: "user72 balance: 10.96 SRF\n"}, - expectError: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - mockAccountService := new(mocks.MockAccountService) - ctx := context.WithValue(ctx, "SessionId", tt.sessionId) - - h := &MenuHandlers{ - userdataStore: store, - accountService: mockAccountService, - } - - if tt.alias != "" { - err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(tt.alias)) - if err != nil { - t.Fatal(err) - } - } - - if tt.activeSym != "" { - err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym)) - if err != nil { - t.Fatal(err) - } - } - - if tt.activeBal != "" { - err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal)) - if err != nil { - t.Fatal(err) - } - } - - res, err := h.CheckBalance(ctx, "check_balance", []byte("")) - - if tt.expectError { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, tt.expectedResult, res, "Result should match expected output") - } - - mockAccountService.AssertExpectations(t) - }) - } -} - func TestGetProfile(t *testing.T) { sessionId := "session123" ctx, store := InitializeTestStore(t) @@ -1702,50 +1612,6 @@ func TestGetProfile(t *testing.T) { } } -func TestFetchCommunityBalance(t *testing.T) { - - // Define test data - sessionId := "session123" - ctx, store := InitializeTestStore(t) - - tests := []struct { - name string - languageCode string - expectedResult resource.Result - }{ - { - name: "Test community balance content when language is english", - expectedResult: resource.Result{ - Content: "Community Balance: 0.00", - }, - languageCode: "eng", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - - mockAccountService := new(mocks.MockAccountService) - mockState := state.NewState(16) - - h := &MenuHandlers{ - userdataStore: store, - st: mockState, - accountService: mockAccountService, - } - ctx = context.WithValue(ctx, "SessionId", sessionId) - ctx = context.WithValue(ctx, "Language", lang.Language{ - Code: tt.languageCode, - }) - - // Call the method - res, _ := h.FetchCommunityBalance(ctx, "fetch_community_balance", []byte("")) - - //Assert that the result set to content is what was expected - assert.Equal(t, res, tt.expectedResult, "Result should match expected result") - }) - } -} - func TestManageVouchers(t *testing.T) { sessionId := "session123" publicKey := "0X13242618721"