Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-mk committed Dec 18, 2024
1 parent fda6823 commit ecfdab4
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions internal/handlers/ussd/menuhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"path"
"strings"
"testing"

"git.defalsify.org/vise.git/lang"
Expand Down Expand Up @@ -67,12 +68,20 @@ func TestNewHandlers(t *testing.T) {
_, store := InitializeTestStore(t)

fm, err := NewFlagManager(flagsPath)
accountService := testservice.TestAccountService{}
if err != nil {
t.Logf(err.Error())
log.Fatal(err)
}

accountService := testservice.TestAccountService{}

// Mock function for replaceSeparator
mockReplaceSeparator := func(input string) string {
return strings.ReplaceAll(input, ":", ": ")
}

// Test case for valid UserDataStore
t.Run("Valid UserDataStore", func(t *testing.T) {
handlers, err := NewHandlers(fm.parser, store, nil, &accountService)
handlers, err := NewHandlers(fm.parser, store, nil, &accountService, mockReplaceSeparator)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand All @@ -82,19 +91,30 @@ func TestNewHandlers(t *testing.T) {
if handlers.userdataStore == nil {
t.Fatal("expected userdataStore to be set in handlers")
}
if handlers.ReplaceSeparator == nil {
t.Fatal("expected ReplaceSeparator to be set in handlers")
}

// Test ReplaceSeparator functionality
input := "1:Menu item"
expectedOutput := "1: Menu item"
if handlers.ReplaceSeparator(input) != expectedOutput {
t.Fatalf("ReplaceSeparator function did not return expected output: got %v, want %v", handlers.ReplaceSeparator(input), expectedOutput)
}
})

// Test case for nil userdataStore
// Test case for nil UserDataStore
t.Run("Nil UserDataStore", func(t *testing.T) {
handlers, err := NewHandlers(fm.parser, nil, nil, &accountService)
handlers, err := NewHandlers(fm.parser, nil, nil, &accountService, mockReplaceSeparator)
if err == nil {
t.Fatal("expected an error, got none")
}
if handlers != nil {
t.Fatal("expected handlers to be nil")
}
if err.Error() != "cannot create handler with nil userdata store" {
t.Fatalf("expected specific error, got %v", err)
expectedError := "cannot create handler with nil userdata store"
if err.Error() != expectedError {
t.Fatalf("expected error '%s', got '%v'", expectedError, err)
}
})
}
Expand Down Expand Up @@ -1982,30 +2002,36 @@ func TestCheckVouchers(t *testing.T) {

func TestGetVoucherList(t *testing.T) {
sessionId := "session123"
menuSeparator := ":"


ctx := context.WithValue(context.Background(), "SessionId", sessionId)
ctx = context.WithValue(ctx, "MenuSeparator", menuSeparator)


spdb := InitializeTestSubPrefixDb(t, ctx)

// Mock replaceSeparator function
replaceSeparator := func(input string) string {
return strings.ReplaceAll(input, ":", ": ")
}

// Initialize Handlers
h := &Handlers{
prefixDb: spdb,
prefixDb: spdb,
ReplaceSeparator: replaceSeparator,
}

expectedSym := []byte("1:SRF\n2:MILO")
mockSyms := []byte("1:SRF\n2:MILO")

// Put voucher sym data from the store
err := spdb.Put(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS), expectedSym)
err := spdb.Put(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS), mockSyms)
if err != nil {
t.Fatal(err)
}

expectedSyms := []byte("1: SRF\n2: MILO")

res, err := h.GetVoucherList(ctx, "", []byte(""))

assert.NoError(t, err)
assert.Equal(t, res.Content, string(expectedSym))
assert.Equal(t, res.Content, string(expectedSyms))
}

func TestViewVoucher(t *testing.T) {
Expand Down

0 comments on commit ecfdab4

Please sign in to comment.