From 8751c4f5bdc5fe14d4985907830a28ae2a154ab6 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Tue, 8 Oct 2024 22:19:48 +0300 Subject: [PATCH] use just one driver for the tests --- driver/testdriver.go | 85 ++++++++++++++++++++++++++++++ test_engine/menu_traversal_test.go | 22 ++++---- 2 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 driver/testdriver.go diff --git a/driver/testdriver.go b/driver/testdriver.go new file mode 100644 index 0000000..67f534b --- /dev/null +++ b/driver/testdriver.go @@ -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 +} diff --git a/test_engine/menu_traversal_test.go b/test_engine/menu_traversal_test.go index 2246d85..96f216a 100644 --- a/test_engine/menu_traversal_test.go +++ b/test_engine/menu_traversal_test.go @@ -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