From d7bfdc62e96b7273e53599a9381cad77138f8e84 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 7 Oct 2024 23:35:41 +0300 Subject: [PATCH] setup test data drivers --- driver/groupdriver.go | 60 +++++++++++++++++++++++++++++++++++++++++ driver/setup_driver .go | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 driver/groupdriver.go create mode 100644 driver/setup_driver .go diff --git a/driver/groupdriver.go b/driver/groupdriver.go new file mode 100644 index 0000000..ebb0706 --- /dev/null +++ b/driver/groupdriver.go @@ -0,0 +1,60 @@ +package driver + +import ( + "encoding/json" + "os" +) + +type StepTest struct { + Input string `json:"input"` + ExpectedContent string `json:"expectedContent"` +} + +// Group represents a group of steps. +type GroupTest struct { + Name string `json:"name"` + Steps []Step `json:"steps"` +} + +// DataGroup represents the overall structure of the JSON. +type DataGroup struct { + Groups []GroupTest `json:"groups"` +} + +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) []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, struct { + Name string + Input string + ExpectedContent string + }{ + Name: group.Name, + Input: step.Input, + ExpectedContent: step.ExpectedContent, + }) + } + } + + return tests +} diff --git a/driver/setup_driver .go b/driver/setup_driver .go new file mode 100644 index 0000000..673d417 --- /dev/null +++ b/driver/setup_driver .go @@ -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 +}