Compare commits
No commits in common. "170d075545c618a7c0da38aba75083104b0d3d09" and "afe98b8695ec6187d9f6b3fc5fff7acfebe91dcf" have entirely different histories.
170d075545
...
afe98b8695
@ -1,55 +0,0 @@
|
||||
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_data_two.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
|
||||
}
|
@ -13,13 +13,12 @@ import (
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
)
|
||||
|
||||
func TestEngine(sessionId string) (engine.Engine, *persist.Persister) {
|
||||
func TestEngine(sessionId string) (engine.Engine,*persist.Persister){
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
pfp := path.Join(scriptDir, "pp.csv")
|
||||
@ -27,7 +26,7 @@ func TestEngine(sessionId string) (engine.Engine, *persist.Persister) {
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
SessionId: sessionId,
|
||||
OutputSize: uint32(160),
|
||||
OutputSize: uint32(106),
|
||||
FlagCount: uint32(16),
|
||||
}
|
||||
|
||||
@ -84,6 +83,6 @@ func TestEngine(sessionId string) (engine.Engine, *persist.Persister) {
|
||||
en = en.WithFirst(hl.Init)
|
||||
|
||||
//en = en.WithDebug(nil)
|
||||
return en, pe
|
||||
return en,pe
|
||||
|
||||
}
|
@ -1,70 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/driver"
|
||||
"git.grassecon.net/urdt/ussd/engine"
|
||||
"git.defalsify.org/vise.git/engine"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/enginetest"
|
||||
)
|
||||
|
||||
var (
|
||||
testData = driver.ReadData()
|
||||
)
|
||||
type TestCase struct {
|
||||
Input []string `json:"input"`
|
||||
Expected string `json:"expected"`
|
||||
}
|
||||
|
||||
type UserRegistration struct {
|
||||
UserRegistration []TestCase `json:"user_registration"`
|
||||
}
|
||||
|
||||
type TestData struct {
|
||||
UserRegistration []TestCase `json:"user_registration"`
|
||||
PinCheck []TestCase `json:"pincheck"`
|
||||
}
|
||||
|
||||
func TestUserRegistration(t *testing.T) {
|
||||
en, _ := enginetest.TestEngine("session1234112")
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
for _, session := range sessions {
|
||||
groups := driver.FilterGroupsByName(session.Groups, "account_creation_successful")
|
||||
for _, group := range groups {
|
||||
for _, step := range group.Steps {
|
||||
cont, _ := en.Exec(ctx, []byte(step.Input))
|
||||
if cont {
|
||||
en, pe := enginetest.TestEngine("session1234112")
|
||||
w := bytes.NewBuffer(nil)
|
||||
_, err = en.Flush(ctx, w)
|
||||
file, err := os.Open("test_data.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
fmt.Println("Error opening file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var testData TestData
|
||||
decoder := json.NewDecoder(file)
|
||||
if err := decoder.Decode(&testData); err != nil {
|
||||
fmt.Println("Error decoding JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var inputBuilder strings.Builder
|
||||
for _, testCase := range testData.UserRegistration {
|
||||
inputBuilder.WriteString(strings.Join(testCase.Input, "\n") + "\n")
|
||||
}
|
||||
readers := bufio.NewReader(strings.NewReader(inputBuilder.String()))
|
||||
engine.Loop(context.Background(), en, readers, w, nil)
|
||||
st := pe.GetState()
|
||||
sym, _ := st.Where()
|
||||
b := w.Bytes()
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
fmt.Println("Menu:",string(b))
|
||||
fmt.Println("Rendering symbol:", sym)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptTerms(t *testing.T) {
|
||||
en, _ := enginetest.TestEngine("session12341123")
|
||||
var err error
|
||||
ctx := context.Background()
|
||||
sessions := testData
|
||||
|
||||
for _, session := range sessions {
|
||||
groups := driver.FilterGroupsByName(session.Groups, "account_creation_accept_terms")
|
||||
for _, group := range groups {
|
||||
for _, step := range group.Steps {
|
||||
cont, _ := en.Exec(ctx, []byte(step.Input))
|
||||
if cont {
|
||||
w := bytes.NewBuffer(nil)
|
||||
_, err = en.Flush(ctx, w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := w.Bytes()
|
||||
if !bytes.Equal(b, []byte(step.ExpectedContent)) {
|
||||
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,50 +1,18 @@
|
||||
[
|
||||
{
|
||||
"user_registration": [
|
||||
{
|
||||
"name": "session one",
|
||||
"groups": [
|
||||
"input": ["0", "0","1234","1234"],
|
||||
"expected": "Registration successful"
|
||||
}
|
||||
],
|
||||
"pincheck": [
|
||||
{
|
||||
"name": "account_creation_successful",
|
||||
"steps": [
|
||||
{
|
||||
"input": "",
|
||||
"expectedContent": "Welcome to Sarafu Network\nPlease select a language\n0:english\n1:kiswahili"
|
||||
"input": ["1234"],
|
||||
"expected": "PIN valid"
|
||||
},
|
||||
{
|
||||
"input": "0",
|
||||
"expectedContent": "Do you agree to terms and conditions?\n0:yes\n1:no"
|
||||
},
|
||||
{
|
||||
"input": "0",
|
||||
"expectedContent": "Please enter a new four number PIN for your account:\n0:Exit"
|
||||
},
|
||||
{
|
||||
"input": "1234",
|
||||
"expectedContent": "Enter your four number PIN again:"
|
||||
},
|
||||
{
|
||||
"input": "1234",
|
||||
"expectedContent": "Enter your four number PIN again:"
|
||||
"input": ["5678"],
|
||||
"expected": "PIN invalid"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "account_creation_accept_terms",
|
||||
"steps": [
|
||||
{
|
||||
"input": "",
|
||||
"expectedContent": "Welcome to Sarafu Network\nPlease select a language\n0:english\n1:kiswahili"
|
||||
},
|
||||
{
|
||||
"input": "0",
|
||||
"expectedContent": "Do you agree to terms and conditions?\n0:yes\n1:no"
|
||||
},
|
||||
{
|
||||
"input": "0",
|
||||
"expectedContent": "Please enter a new four number PIN for your account:\n0:Exit"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user