use just one driver for the tests

This commit is contained in:
Carlosokumu 2024-10-08 22:19:48 +03:00
parent fc935535f9
commit 8751c4f5bd
Signed by untrusted user: carlos
GPG Key ID: 7BD6BC8160A5C953
2 changed files with 95 additions and 12 deletions

85
driver/testdriver.go Normal file
View File

@ -0,0 +1,85 @@
package driver
import (
"encoding/json"
"log"
"os"
)
type Step struct {
Input string `json:"input"`
ExpectedContent string `json:"expectedContent"`
}
// 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)
if err != nil {
return sessionsData, err
}
err = json.Unmarshal(data, &sessionsData)
return sessionsData, err
}
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, TestCase{
Name: group.Name,
Input: step.Input,
ExpectedContent: step.ExpectedContent,
})
}
}
return tests
}

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