Compare commits

...

6 Commits

Author SHA1 Message Date
8751c4f5bd
use just one driver for the tests 2024-10-08 22:19:48 +03:00
fc935535f9
remove deprecated code 2024-10-08 22:19:21 +03:00
ef4846b11f
move online,offline and test engine into the testutil package 2024-10-08 22:18:44 +03:00
827b99e48d
Revert "add dummy vouchers list"
This reverts commit 188cb573dd.
2024-10-07 20:36:02 +03:00
e09c1e1e98
Revert "add dummy vouchers list"
This reverts commit 188cb573dd.
2024-10-07 20:35:18 +03:00
87c6029199
Revert "add dummy vouchers list"
This reverts commit 188cb573dd.
2024-10-07 20:33:02 +03:00
11 changed files with 65 additions and 151 deletions

View File

@ -1,55 +0,0 @@
package driver
import (
"encoding/json"
"log"
"os"
)
type Step struct {
Input string `json:"input"`
ExpectedContent string `json:"expectedContent"`
}
type Group struct {
Name string `json:"name"`
Steps []Step `json:"steps"`
}
type Session struct {
Name string `json:"name"`
Groups []Group `json:"groups"`
}
func ReadData() []Session {
data, err := os.ReadFile("test_setup.json")
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
// Unmarshal JSON data
var sessions []Session
err = json.Unmarshal(data, &sessions)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
}
return sessions
}
func FilterGroupsByName(groups []Group, name string) []Group {
var filteredGroups []Group
for _, group := range groups {
if group.Name == name {
filteredGroups = append(filteredGroups, group)
}
}
return filteredGroups
}
func Map[T any, U any](input []T, fn func(T) U) []U {
result := make([]U, len(input))
for i, v := range input {
result[i] = fn(v)
}
return result
}

View File

@ -2,25 +2,62 @@ package driver
import (
"encoding/json"
"log"
"os"
)
type StepTest struct {
type Step struct {
Input string `json:"input"`
ExpectedContent string `json:"expectedContent"`
}
// Group represents a group of steps.
type GroupTest struct {
// Group represents a group of steps
type Group struct {
Name string `json:"name"`
Steps []Step `json:"steps"`
}
type TestCase struct {
Name string
Input string
ExpectedContent string
}
// DataGroup represents the overall structure of the JSON.
type DataGroup struct {
Groups []Group `json:"groups"`
}
type Session struct {
Name string `json:"name"`
Groups []Group `json:"groups"`
}
func ReadData() []Session {
data, err := os.ReadFile("test_setup.json")
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
// Unmarshal JSON data
var sessions []Session
err = json.Unmarshal(data, &sessions)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
}
return sessions
}
func FilterGroupsByName(groups []Group, name string) []Group {
var filteredGroups []Group
for _, group := range groups {
if group.Name == name {
filteredGroups = append(filteredGroups, group)
}
}
return filteredGroups
}
func LoadTestGroups(filePath string) (DataGroup, error) {
var sessionsData DataGroup
data, err := os.ReadFile(filePath)
@ -31,24 +68,12 @@ func LoadTestGroups(filePath string) (DataGroup, error) {
return sessionsData, err
}
func CreateTestCases(group DataGroup) []struct {
Name string
Input string
ExpectedContent string
} {
var tests []struct {
Name string
Input string
ExpectedContent string
}
func CreateTestCases(group DataGroup) []TestCase {
var tests []TestCase
for _, group := range group.Groups {
for _, step := range group.Steps {
// Create a test case for each group
tests = append(tests, struct {
Name string
Input string
ExpectedContent string
}{
tests = append(tests, TestCase{
Name: group.Name,
Input: step.Input,
ExpectedContent: step.ExpectedContent,

View File

@ -94,7 +94,6 @@ func (ls *LocalHandlerService) GetHandler(accountService server.AccountServiceIn
ls.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
ls.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
ls.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp)
ls.DbRs.AddLocalFunc("get_vouchers", ussdHandlers.GetVoucherList)
return ussdHandlers, nil
}

View File

@ -238,56 +238,16 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
accountPIN := string(input)
if accountPIN != "0" { // for the 0:Back case
// Validate that the PIN is a 4-digit number
if !isValidPIN(accountPIN) {
res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
return res, nil
}
store := h.userdataStore
err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(accountPIN))
if err != nil {
return res, err
}
// Validate that the PIN is a 4-digit number
if !isValidPIN(accountPIN) {
res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
return res, nil
}
return res, nil
}
func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result
vouchers := []string{
"SRF",
"CRF",
"VCF",
"VSAPA",
"FSTMP",
"FSAW",
"PTAQ",
"VCRXT",
"VSGAQ",
"QPWIQQ",
"FSTMP",
"FSAW",
"PTAQ",
"VCRXT",
"VSGAQ",
"QPWIQQ",
"FSTMP",
"FSAW",
"PTAQ",
"VCRXT",
"VSGAQ",
"QPWIQQ",
store := h.userdataStore
err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(accountPIN))
if err != nil {
return res, err
}
var numberedVouchers []string
for i, voucher := range vouchers {
numberedVouchers = append(numberedVouchers, fmt.Sprintf("%d:%s", i+1, voucher))
}
res.Content = strings.Join(numberedVouchers, "\n")
return res, nil
}

View File

@ -1,4 +1,4 @@
package engine
package testutil
import (
"context"
@ -104,6 +104,6 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
logg.Infof("testengine storage closed")
}
//en = en.WithDebug(nil)
en = en.WithDebug(nil)
return en, cleanFn
}

View File

@ -1,5 +1,5 @@
// +build !online
package engine
package testutil
const OnlineTestEnabled = false

View File

@ -1,5 +1,5 @@
// +build online
package engine
package testutil
const OnlineTestEnabled = true

View File

@ -8,7 +8,7 @@ MOUT help 4
MOUT quit 9
HALT
INCMP send 1
INCMP my_vouchers 2
INCMP quit 2
INCMP my_account 3
INCMP help 4
INCMP quit 9

View File

@ -1,2 +0,0 @@
Select number or symbol from your vouchers:
{{.get_vouchers}}

View File

@ -1,11 +0,0 @@
LOAD get_vouchers 0
MAP get_vouchers
MOUT back 0
MOUT quit 9
MNEXT next 11
MPREV prev 22
HALT
INCMP _ 0
INCMP quit 9
INCMP > 11
INCMP < 22

View File

@ -11,7 +11,7 @@ import (
"time"
"git.grassecon.net/urdt/ussd/driver"
enginetest "git.grassecon.net/urdt/ussd/engine"
"git.grassecon.net/urdt/ussd/internal/testutil"
"github.com/gofrs/uuid"
)
@ -55,7 +55,7 @@ func TestMain(m *testing.M) {
}
func TestAccountCreationSuccessful(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -94,7 +94,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
t.Fail()
}
edgeCaseSessionID := v.String()
en, fn := enginetest.TestEngine(edgeCaseSessionID)
en, fn := testutil.TestEngine(edgeCaseSessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -125,7 +125,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
}
func TestSendWithInvalidInputs(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -163,7 +163,7 @@ func TestSendWithInvalidInputs(t *testing.T) {
}
func TestMyAccount_Check_My_Balance(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -194,7 +194,7 @@ func TestMyAccount_Check_My_Balance(t *testing.T) {
}
func TestMainMenuHelp(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -225,7 +225,7 @@ func TestMainMenuHelp(t *testing.T) {
}
func TestMainMenuQuit(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -256,7 +256,7 @@ func TestMainMenuQuit(t *testing.T) {
}
func TestMyAccount_Check_Community_Balance(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -286,10 +286,8 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) {
}
}
func TestMyAccount_MyAddress(t *testing.T) {
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
sessions := testData
@ -329,7 +327,7 @@ func TestGroups(t *testing.T) {
if err != nil {
log.Fatalf("Failed to load test groups: %v", err)
}
en, fn := enginetest.TestEngine(sessionID)
en, fn := testutil.TestEngine(sessionID)
defer fn()
ctx := context.Background()
// Create test cases from loaded groups