Compare commits

..

2 Commits

Author SHA1 Message Date
fc85bd7eed
return an event channel for the test engine 2024-10-09 14:06:16 +03:00
5869324c16
remove unused variable 2024-10-09 14:03:36 +03:00
3 changed files with 36 additions and 23 deletions

View File

@ -5,11 +5,13 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"time"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/handlers"
"git.grassecon.net/urdt/ussd/internal/handlers/server"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
testdataloader "github.com/peteole/testdata-loader" testdataloader "github.com/peteole/testdata-loader"
) )
@ -20,12 +22,13 @@ var (
scriptDir = path.Join(baseDir, "services", "registration") scriptDir = path.Join(baseDir, "services", "registration")
) )
func TestEngine(sessionId string) (engine.Engine, func()) { func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
//var accountService server.AccountServiceInterface
ctx := context.Background() ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "SessionId", sessionId)
pfp := path.Join(scriptDir, "pp.csv") pfp := path.Join(scriptDir, "pp.csv")
var eventChannel = make(chan bool)
cfg := engine.Config{ cfg := engine.Config{
Root: "root", Root: "root",
SessionId: sessionId, SessionId: sessionId,
@ -75,6 +78,21 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
switch AccountService.(type) {
case *server.MockAccountService:
go func() {
eventChannel <- false
}()
case *server.AccountService:
go func() {
time.Sleep(5 * time.Second) // Wait for 5 seconds
eventChannel <- true
}()
default:
panic("Unknown account service type")
}
hl, err := lhs.GetHandler(AccountService) hl, err := lhs.GetHandler(AccountService)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
@ -83,7 +101,6 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
en := lhs.GetEngine() en := lhs.GetEngine()
en = en.WithFirst(hl.Init) en = en.WithFirst(hl.Init)
cleanFn := func() { cleanFn := func() {
err := en.Finish() err := en.Finish()
if err != nil { if err != nil {
@ -98,5 +115,5 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
} }
//en = en.WithDebug(nil) //en = en.WithDebug(nil)
return en, cleanFn return en, cleanFn, eventChannel
} }

View File

@ -1,15 +1,12 @@
//go:build online
// +build online // +build online
package testutil package testutil
const OnlineTestEnabled = true import "git.grassecon.net/urdt/ussd/internal/handlers/server"
var AccountService server.AccountServiceInterface var AccountService server.AccountServiceInterface
func init() { func init() {
AccountService = &server.AccountService{} AccountService = &server.AccountService{}
} }

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"regexp" "regexp"
"testing" "testing"
"time"
"git.grassecon.net/urdt/ussd/driver" "git.grassecon.net/urdt/ussd/driver"
"git.grassecon.net/urdt/ussd/internal/testutil" "git.grassecon.net/urdt/ussd/internal/testutil"
@ -55,7 +54,7 @@ func TestMain(m *testing.M) {
} }
func TestAccountCreationSuccessful(t *testing.T) { func TestAccountCreationSuccessful(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, eventChannel := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -86,8 +85,8 @@ func TestAccountCreationSuccessful(t *testing.T) {
} }
} }
} }
// Adding a sleep after the test to wait for registration to complete the process <-eventChannel
time.Sleep(5 * time.Second)
} }
func TestAccountRegistrationRejectTerms(t *testing.T) { func TestAccountRegistrationRejectTerms(t *testing.T) {
@ -98,7 +97,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
t.Fail() t.Fail()
} }
edgeCaseSessionID := v.String() edgeCaseSessionID := v.String()
en, fn := testutil.TestEngine(edgeCaseSessionID) en, fn, _ := testutil.TestEngine(edgeCaseSessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -133,7 +132,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
} }
func TestSendWithInvalidInputs(t *testing.T) { func TestSendWithInvalidInputs(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -174,7 +173,7 @@ func TestSendWithInvalidInputs(t *testing.T) {
} }
func TestMyAccount_Check_My_Balance(t *testing.T) { func TestMyAccount_Check_My_Balance(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -209,7 +208,7 @@ func TestMyAccount_Check_My_Balance(t *testing.T) {
} }
func TestMainMenuHelp(t *testing.T) { func TestMainMenuHelp(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -244,7 +243,7 @@ func TestMainMenuHelp(t *testing.T) {
} }
func TestMainMenuQuit(t *testing.T) { func TestMainMenuQuit(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -279,7 +278,7 @@ func TestMainMenuQuit(t *testing.T) {
} }
func TestMyAccount_Check_Community_Balance(t *testing.T) { func TestMyAccount_Check_Community_Balance(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -314,7 +313,7 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) {
} }
func TestMyAccount_MyAddress(t *testing.T) { func TestMyAccount_MyAddress(t *testing.T) {
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
sessions := testData sessions := testData
@ -356,7 +355,7 @@ func TestGroups(t *testing.T) {
if err != nil { if err != nil {
log.Fatalf("Failed to load test groups: %v", err) log.Fatalf("Failed to load test groups: %v", err)
} }
en, fn := testutil.TestEngine(sessionID) en, fn, _ := testutil.TestEngine(sessionID)
defer fn() defer fn()
ctx := context.Background() ctx := context.Background()
// Create test cases from loaded groups // Create test cases from loaded groups