From afe98b8695ec6187d9f6b3fc5fff7acfebe91dcf Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 27 Sep 2024 15:02:29 +0300 Subject: [PATCH] setup traversal tests --- enginetest/engine.go | 88 +++++++++++ internal/handlers/ussd/menu_traversal_test.go | 141 ------------------ menu_traversal_test.go | 61 ++++++++ test_data.json | 18 +++ testdata/testdata.go | 5 +- 5 files changed, 169 insertions(+), 144 deletions(-) create mode 100644 enginetest/engine.go delete mode 100644 internal/handlers/ussd/menu_traversal_test.go create mode 100644 menu_traversal_test.go create mode 100644 test_data.json diff --git a/enginetest/engine.go b/enginetest/engine.go new file mode 100644 index 0000000..f6ab886 --- /dev/null +++ b/enginetest/engine.go @@ -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 + +} diff --git a/internal/handlers/ussd/menu_traversal_test.go b/internal/handlers/ussd/menu_traversal_test.go deleted file mode 100644 index 86c909d..0000000 --- a/internal/handlers/ussd/menu_traversal_test.go +++ /dev/null @@ -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) - } - }) - } -} diff --git a/menu_traversal_test.go b/menu_traversal_test.go new file mode 100644 index 0000000..091d02e --- /dev/null +++ b/menu_traversal_test.go @@ -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) + +} diff --git a/test_data.json b/test_data.json new file mode 100644 index 0000000..cc54b50 --- /dev/null +++ b/test_data.json @@ -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" + } + ] +} diff --git a/testdata/testdata.go b/testdata/testdata.go index b3b1f00..338c1f2 100644 --- a/testdata/testdata.go +++ b/testdata/testdata.go @@ -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)