Compare commits
No commits in common. "c8a5391435fe7bee9839072d30fa60a9d1754dd4" and "d01af482168c93dc0fec2cae0bfd214faa5732f2" have entirely different histories.
c8a5391435
...
d01af48216
@ -2,86 +2,23 @@ package driver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Step struct {
|
||||
type StepTest struct {
|
||||
Input string `json:"input"`
|
||||
ExpectedContent string `json:"expectedContent"`
|
||||
}
|
||||
|
||||
func (s *Step) MatchesExpectedContent(content []byte) (bool, error) {
|
||||
pattern := `.*\?.*|.*`
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Check if the content matches the regex pattern
|
||||
if re.Match(content) {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Group represents a group of steps
|
||||
type Group struct {
|
||||
// Group represents a group of steps.
|
||||
type GroupTest struct {
|
||||
Name string `json:"name"`
|
||||
Steps []Step `json:"steps"`
|
||||
}
|
||||
|
||||
type TestCase struct {
|
||||
Name string
|
||||
Input string
|
||||
ExpectedContent string
|
||||
}
|
||||
|
||||
func (s *TestCase) MatchesExpectedContent(content []byte) (bool, error) {
|
||||
re, err := regexp.Compile(s.ExpectedContent)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Check if the content matches the regex pattern
|
||||
if re.Match(content) {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
Groups []GroupTest `json:"groups"`
|
||||
}
|
||||
|
||||
func LoadTestGroups(filePath string) (DataGroup, error) {
|
||||
@ -94,12 +31,24 @@ func LoadTestGroups(filePath string) (DataGroup, error) {
|
||||
return sessionsData, err
|
||||
}
|
||||
|
||||
func CreateTestCases(group DataGroup) []TestCase {
|
||||
var tests []TestCase
|
||||
func CreateTestCases(group DataGroup) []struct {
|
||||
Name string
|
||||
Input string
|
||||
ExpectedContent string
|
||||
} {
|
||||
var tests []struct {
|
||||
Name string
|
||||
Input string
|
||||
ExpectedContent string
|
||||
}
|
||||
for _, group := range group.Groups {
|
||||
for _, step := range group.Steps {
|
||||
// Create a test case for each group
|
||||
tests = append(tests, TestCase{
|
||||
tests = append(tests, struct {
|
||||
Name string
|
||||
Input string
|
||||
ExpectedContent string
|
||||
}{
|
||||
Name: group.Name,
|
||||
Input: step.Input,
|
||||
ExpectedContent: step.ExpectedContent,
|
||||
|
55
driver/setup_driver .go
Normal file
55
driver/setup_driver .go
Normal file
@ -0,0 +1,55 @@
|
||||
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
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"git.defalsify.org/vise.git/engine"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||
testdataloader "github.com/peteole/testdata-loader"
|
||||
)
|
||||
|
||||
var (
|
||||
baseDir = testdataloader.GetBasePath()
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join(baseDir, "services", "registration")
|
||||
)
|
||||
|
||||
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
pfp := path.Join(scriptDir, "pp.csv")
|
||||
|
||||
var eventChannel = make(chan bool)
|
||||
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
SessionId: sessionId,
|
||||
OutputSize: uint32(160),
|
||||
FlagCount: uint32(16),
|
||||
}
|
||||
|
||||
dbDir := ".test_state"
|
||||
resourceDir := scriptDir
|
||||
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
||||
|
||||
err := menuStorageService.EnsureDbDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rs, err := menuStorageService.GetResource(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pe, err := menuStorageService.GetPersister(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
userDataStore, err := menuStorageService.GetUserdataDb(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
dbResource, ok := rs.(*resource.DbResource)
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
||||
lhs.SetDataStore(&userDataStore)
|
||||
lhs.SetPersister(pe)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
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)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
en := lhs.GetEngine()
|
||||
en = en.WithFirst(hl.Init)
|
||||
cleanFn := func() {
|
||||
err := en.Finish()
|
||||
if err != nil {
|
||||
logg.Errorf(err.Error())
|
||||
}
|
||||
|
||||
err = menuStorageService.Close()
|
||||
if err != nil {
|
||||
logg.Errorf(err.Error())
|
||||
}
|
||||
logg.Infof("testengine storage closed")
|
||||
}
|
||||
|
||||
//en = en.WithDebug(nil)
|
||||
return en, cleanFn, eventChannel
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
// +build !online
|
||||
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
)
|
||||
|
||||
var AccountService server.AccountServiceInterface
|
||||
|
||||
func init() {
|
||||
AccountService = &server.MockAccountService{}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
// +build online
|
||||
|
||||
package testutil
|
||||
|
||||
import "git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
|
||||
var AccountService server.AccountServiceInterface
|
||||
|
||||
func init() {
|
||||
AccountService = &server.AccountService{}
|
||||
}
|
@ -19,3 +19,4 @@ INCMP enter_yob 4
|
||||
INCMP enter_location 5
|
||||
INCMP enter_offerings 6
|
||||
INCMP view_profile 7
|
||||
|
||||
|
@ -8,9 +8,10 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/driver"
|
||||
"git.grassecon.net/urdt/ussd/internal/testutil"
|
||||
enginetest "git.grassecon.net/urdt/ussd/engine"
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
@ -54,7 +55,7 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func TestAccountCreationSuccessful(t *testing.T) {
|
||||
en, fn, eventChannel := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -75,18 +76,14 @@ func TestAccountCreationSuccessful(t *testing.T) {
|
||||
t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err)
|
||||
}
|
||||
b := w.Bytes()
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
<-eventChannel
|
||||
|
||||
// Adding a sleep after the test to wait for registration to complete the process
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
func TestAccountRegistrationRejectTerms(t *testing.T) {
|
||||
@ -97,7 +94,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
edgeCaseSessionID := v.String()
|
||||
en, fn, _ := testutil.TestEngine(edgeCaseSessionID)
|
||||
en, fn := enginetest.TestEngine(edgeCaseSessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -119,11 +116,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
|
||||
}
|
||||
|
||||
b := w.Bytes()
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -132,7 +125,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSendWithInvalidInputs(t *testing.T) {
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -160,11 +153,8 @@ func TestSendWithInvalidInputs(t *testing.T) {
|
||||
|
||||
// Replace placeholder {public_key} with the actual dynamic public key
|
||||
expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1)
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
|
||||
if !bytes.Equal(b, expectedContent) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -173,7 +163,7 @@ func TestSendWithInvalidInputs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyAccount_Check_My_Balance(t *testing.T) {
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -195,11 +185,7 @@ func TestMyAccount_Check_My_Balance(t *testing.T) {
|
||||
t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
|
||||
}
|
||||
b := w.Bytes()
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -208,7 +194,7 @@ func TestMyAccount_Check_My_Balance(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMainMenuHelp(t *testing.T) {
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -230,11 +216,7 @@ func TestMainMenuHelp(t *testing.T) {
|
||||
}
|
||||
|
||||
b := w.Bytes()
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -243,7 +225,7 @@ func TestMainMenuHelp(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMainMenuQuit(t *testing.T) {
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -265,11 +247,7 @@ func TestMainMenuQuit(t *testing.T) {
|
||||
}
|
||||
|
||||
b := w.Bytes()
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -278,7 +256,7 @@ func TestMainMenuQuit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyAccount_Check_Community_Balance(t *testing.T) {
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -300,11 +278,7 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) {
|
||||
t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
|
||||
}
|
||||
b := w.Bytes()
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -313,7 +287,7 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyAccount_MyAddress(t *testing.T) {
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
@ -337,12 +311,10 @@ func TestMyAccount_MyAddress(t *testing.T) {
|
||||
b := w.Bytes()
|
||||
|
||||
publicKey := extractPublicKey(b)
|
||||
|
||||
expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1)
|
||||
match, err := step.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err)
|
||||
}
|
||||
if !match {
|
||||
|
||||
if !bytes.Equal(b, expectedContent) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b)
|
||||
}
|
||||
}
|
||||
@ -355,7 +327,7 @@ func TestGroups(t *testing.T) {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load test groups: %v", err)
|
||||
}
|
||||
en, fn, _ := testutil.TestEngine(sessionID)
|
||||
en, fn := enginetest.TestEngine(sessionID)
|
||||
defer fn()
|
||||
ctx := context.Background()
|
||||
// Create test cases from loaded groups
|
||||
@ -375,11 +347,7 @@ func TestGroups(t *testing.T) {
|
||||
t.Errorf("Test case '%s' failed during Flush: %v", tt.Name, err)
|
||||
}
|
||||
b := w.Bytes()
|
||||
match, err := tt.MatchesExpectedContent(b)
|
||||
if err != nil {
|
||||
t.Fatalf("Error compiling regex for step '%s': %v", tt.Input, err)
|
||||
}
|
||||
if !match {
|
||||
if !bytes.Equal(b, []byte(tt.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", tt.ExpectedContent, b)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user