setup traversal tests

This commit is contained in:
Carlosokumu 2024-09-27 15:02:29 +03:00
parent ece80b31f6
commit afe98b8695
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953
5 changed files with 169 additions and 144 deletions

88
enginetest/engine.go Normal file
View File

@ -0,0 +1,88 @@
package enginetest
import (
"context"
"fmt"
"os"
"path"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"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){
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
OutputSize: uint32(106),
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)
}
hl, err := lhs.GetHandler()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
en := lhs.GetEngine()
en = en.WithFirst(hl.Init)
//en = en.WithDebug(nil)
return en,pe
}

View File

@ -1,141 +0,0 @@
package ussd
import (
"bytes"
"context"
"fmt"
"path"
"testing"
"git.defalsify.org/vise.git/db"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/testdata"
testdataloader "github.com/peteole/testdata-loader"
)
var (
dataGenerated bool = false
dataDir string = testdata.DataDir
BaseDir = testdataloader.GetBasePath()
)
type testWrapper struct {
resource.Resource
db db.Db
}
func generateTestData(t *testing.T) {
if dataGenerated {
return
}
var err error
dataDir, err = testdata.Generate()
if err != nil {
t.Fatal(err)
}
}
func newTestWrapper(path string) testWrapper {
ctx := context.Background()
store := fsdb.NewFsDb()
store.Connect(ctx, path)
rs := resource.NewDbResource(store)
rs.With(db.DATATYPE_STATICLOAD)
wr := testWrapper{
rs,
store,
}
rs.AddLocalFunc("quit", quit)
return wr
}
func quit(ctx context.Context, sym string, input []byte) (resource.Result, error) {
return resource.Result{
Content: "Thank you for using Sarafu network",
}, nil
}
func TestTerms(t *testing.T) {
generateTestData(t)
ctx := context.Background()
rs := newTestWrapper(dataDir)
cfg := engine.Config{
Root: "terms",
FlagCount: uint32(9),
}
store := storage.NewThreadGdbmDb()
storeFile := path.Join(baseDir, "state.gdbm")
err := store.Connect(ctx, storeFile)
if err != nil {
t.Fail()
}
pr := persist.NewPersister(store)
en := engine.NewEngine(cfg, &rs)
en.WithPersister(pr)
if pr.GetState() == nil || pr.GetMemory() == nil {
t.Fail()
}
_, err = en.Exec(ctx, []byte{})
if err != nil {
t.Fatal(err)
}
w := bytes.NewBuffer(nil)
_, err = en.Flush(ctx, w)
if err != nil {
t.Fatal(err)
}
b := w.Bytes()
expect_str := `Do you agree to terms and conditions?
1:yes
2:no`
if !bytes.Equal(b, []byte(expect_str)) {
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect_str, b)
}
tests := []struct {
name string
expectedSymbol string
input []byte
}{
{
name: "Test accept terms option(yes)",
expectedSymbol: "create_pin",
input: []byte("1"),
},
// {
// name: "Test reject terms option(no)",
// input: []byte("2"),
// expectedSymbol: "quit",
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err = en.Exec(ctx, tt.input)
if err != nil {
t.Fatal(err)
}
w := bytes.NewBuffer(nil)
_, err = en.Flush(ctx, w)
if err != nil {
t.Fatal(err)
}
b = w.Bytes()
fmt.Println("result", string(b))
symbol, _ := pr.State.Where()
if symbol != tt.expectedSymbol {
t.Fatalf("expected symbol to be 'create_pin', got %s", symbol)
}
})
}
}

61
menu_traversal_test.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"git.defalsify.org/vise.git/engine"
"git.grassecon.net/urdt/ussd/enginetest"
)
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, pe := enginetest.TestEngine("session1234112")
w := bytes.NewBuffer(nil)
file, err := os.Open("test_data.json")
if err != nil {
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()
fmt.Println("Menu:",string(b))
fmt.Println("Rendering symbol:", sym)
}

18
test_data.json Normal file
View File

@ -0,0 +1,18 @@
{
"user_registration": [
{
"input": ["0", "0","1234","1234"],
"expected": "Registration successful"
}
],
"pincheck": [
{
"input": ["1234"],
"expected": "PIN valid"
},
{
"input": ["5678"],
"expected": "PIN invalid"
}
]
}

View File

@ -4,7 +4,8 @@ import (
"context"
"io/ioutil"
"os"
"path"
"path"
"git.defalsify.org/vise.git/db"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.defalsify.org/vise.git/logging"
@ -12,8 +13,6 @@ import (
testdataloader "github.com/peteole/testdata-loader"
)
func outNew(sym string, b []byte, tpl string, data map[string]string) error {
logg.Debugf("testdata out", "sym", sym)
store.SetPrefix(db.DATATYPE_TEMPLATE)