WIP use new events updater with storage service, now test panics

This commit is contained in:
lash 2025-01-13 21:40:37 +00:00
parent 8843e5baef
commit aa41a606dc
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
8 changed files with 44 additions and 111 deletions

View File

@ -4,21 +4,20 @@ import (
"context"
"testing"
memdb "git.defalsify.org/vise.git/db/mem"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/state"
"git.defalsify.org/vise.git/cache"
"git.grassecon.net/grassrootseconomics/visedriver/testutil/mocks"
"git.grassecon.net/grassrootseconomics/sarafu-api/remote/http"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/config"
"git.grassecon.net/grassrootseconomics/common/hex"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/internal/testutil"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
viseevent "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
)
func TestCustodialRegistration(t *testing.T) {
err := config.LoadConfig()
if err != nil {
@ -26,11 +25,8 @@ func TestCustodialRegistration(t *testing.T) {
}
ctx := context.Background()
userDb := memdb.NewMemDb()
err = userDb.Connect(ctx, "")
if err != nil {
panic(err)
}
storageService := mocks.NewMemStorageService(ctx)
userDb := storageService.Db
alice, err := hex.NormalizeHex(testutil.AliceChecksum)
if err != nil {
@ -43,13 +39,10 @@ func TestCustodialRegistration(t *testing.T) {
if err != nil {
t.Fatal(err)
}
store := store.UserDataStore{
Db: userDb,
}
st := state.NewState(248)
ca := cache.NewCache()
pr := persist.NewPersister(userDb)
pr, _ := storageService.GetPersister(ctx)
pr = pr.WithContent(st, ca)
err = pr.Save(testutil.AliceSession)
if err != nil {
@ -61,8 +54,9 @@ func TestCustodialRegistration(t *testing.T) {
}
// Use dev service or mock service instead
eh := viseevent.NewEventsHandler(&http.HTTPAccountService{})
err = eh.HandleCustodialRegistration(ctx, &store, pr, ev)
eu := viseevent.NewEventsUpdater(&http.HTTPAccountService{}, storageService)
eh := eu.ToEventsHandler()
err = eh.Handle(ctx, apievent.EventRegistrationTag, ev)
if err != nil {
t.Fatal(err)
}

View File

@ -9,10 +9,9 @@ import (
"github.com/nats-io/nats.go/jetstream"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/event"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/config"
viseevent "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
)
var (
@ -32,9 +31,9 @@ type NatsSubscription struct {
}
// NewNatsSubscription creates a new NatsSubscription with the given user store.
func NewNatsSubscription(store storage.StorageService, handler *viseevent.EventsHandler) *NatsSubscription {
func NewNatsSubscription(handler *apievent.EventsHandler) *NatsSubscription {
return &NatsSubscription{
Router: event.NewRouter(store, handler),
Router: event.NewRouter(handler),
}
}

View File

@ -13,7 +13,6 @@ import (
"github.com/nats-io/nats.go/jetstream"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
"git.defalsify.org/vise.git/db"
memdb "git.defalsify.org/vise.git/db/mem"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/config"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
@ -23,6 +22,7 @@ import (
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/internal/testutil"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application"
viseevent "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
"git.grassecon.net/grassrootseconomics/visedriver/testutil/mocks"
)
const (
@ -126,14 +126,10 @@ func TestHandleMsg(t *testing.T) {
},
}
lookup.Api = api
eh := viseevent.NewEventsHandler(api)
ctx := context.Background()
userDb := memdb.NewMemDb()
err = userDb.Connect(ctx, "")
if err != nil {
panic(err)
}
storageService := mocks.NewMemStorageService(ctx)
eu := viseevent.NewEventsUpdater(api, storageService)
userDb := storageService.Db
alice, err := hex.NormalizeHex(testutil.AliceChecksum)
if err != nil {
@ -147,11 +143,8 @@ func TestHandleMsg(t *testing.T) {
t.Fatal(err)
}
storageService := &testutil.TestStorageService{
Store: userDb,
}
sub := NewNatsSubscription(storageService, eh)
eh := eu.ToEventsHandler()
sub := NewNatsSubscription(eh)
data := fmt.Sprintf(`{
"block": %d,

View File

@ -7,9 +7,7 @@ import (
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
viseevent "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
)
var (
@ -18,13 +16,11 @@ var (
// Router is responsible for invoking handlers corresponding to events.
type Router struct {
store storage.StorageService
handler *viseevent.EventsHandler
handler *apievent.EventsHandler
}
func NewRouter(store storage.StorageService, handler *viseevent.EventsHandler) *Router {
func NewRouter(handler *apievent.EventsHandler) *Router {
return &Router{
store: store,
handler: handler,
}
}
@ -36,24 +32,13 @@ func NewRouter(store storage.StorageService, handler *viseevent.EventsHandler) *
// handler fails to successfully execute.
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
logg.DebugCtxf(ctx, "have event", "ev", gev)
userDb, err := r.store.GetUserdataDb(ctx)
if err != nil {
return err
}
userStore := &store.UserDataStore{
Db: userDb,
}
evCC, ok := asCustodialRegistrationEvent(gev)
if ok {
pr, err := r.store.GetPersister(ctx)
if err != nil {
return err
}
return r.handler.HandleCustodialRegistration(ctx, userStore, pr, evCC)
return r.handler.Handle(ctx, apievent.EventTokenTransferTag, evCC)
}
evTT, ok := asTokenTransferEvent(gev)
if ok {
return r.handler.HandleTokenTransfer(ctx, userStore, evTT)
return r.handler.Handle(ctx, apievent.EventRegistrationTag, evTT)
}
return fmt.Errorf("unexpected message")

View File

@ -11,7 +11,6 @@ import (
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
"git.defalsify.org/vise.git/db"
memdb "git.defalsify.org/vise.git/db/mem"
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/config"
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application"
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
@ -22,6 +21,7 @@ import (
"git.grassecon.net/grassrootseconomics/sarafu-vise-events/internal/testutil"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
viseevent "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
"git.grassecon.net/grassrootseconomics/visedriver/testutil/mocks"
)
const (
@ -74,14 +74,11 @@ func TestTokenTransfer(t *testing.T) {
},
}
lookup.Api = api
eh := viseevent.NewEventsHandler(api)
ctx := context.Background()
userDb := memdb.NewMemDb()
err = userDb.Connect(ctx, "")
if err != nil {
panic(err)
}
storageService := mocks.NewMemStorageService(ctx)
eu := viseevent.NewEventsUpdater(api, storageService)
userDb := storageService.Db
alice, err := hex.NormalizeHex(testutil.AliceChecksum)
if err != nil {
@ -105,7 +102,8 @@ func TestTokenTransfer(t *testing.T) {
Value: txValue,
}
err = eh.HandleTokenTransfer(ctx, &userStore, ev)
eh := eu.ToEventsHandler()
err = eh.Handle(ctx, apievent.EventTokenTransferTag, ev)
if err != nil {
t.Fatal(err)
}
@ -187,14 +185,12 @@ func TestTokenMint(t *testing.T) {
},
}
lookup.Api = api
eh := viseevent.NewEventsHandler(api)
ctx := context.Background()
userDb := memdb.NewMemDb()
err = userDb.Connect(ctx, "")
if err != nil {
panic(err)
}
storageService := mocks.NewMemStorageService(ctx)
eu := viseevent.NewEventsUpdater(api, storageService)
userDb := storageService.Db
alice, err := hex.NormalizeHex(testutil.AliceChecksum)
if err != nil {
@ -215,7 +211,9 @@ func TestTokenMint(t *testing.T) {
To: testutil.AliceChecksum,
Value: txValue,
}
err = eh.HandleTokenMint(ctx, &userStore, ev)
eh := eu.ToEventsHandler()
err = eh.Handle(ctx, apievent.EventTokenMintTag, ev)
if err != nil {
t.Fatal(err)
}
@ -247,7 +245,6 @@ func TestTokenMint(t *testing.T) {
t.Fatal("no transaction data")
}
mh, err := application.NewMenuHandlers(nil, userStore, nil, nil, testutil.ReplaceSeparatorFunc)
if err != nil {
t.Fatal(err)

6
go.mod
View File

@ -5,9 +5,9 @@ go 1.23.4
require (
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805
git.grassecon.net/grassrootseconomics/sarafu-vise v0.0.0-20250113181554-0d47c096e0d6
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7
git.grassecon.net/grassrootseconomics/sarafu-vise v0.0.0-20250113213915-63eb803022af
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b
github.com/grassrootseconomics/eth-tracker v1.3.0-rc
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta
github.com/nats-io/nats.go v1.37.0

12
go.sum
View File

@ -2,12 +2,12 @@ git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSG
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk=
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805 h1:Okih7rAAjD8cpv7pi8QQc36mmp7SRBnOc7dc125GP/E=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805/go.mod h1:e9Damfk0euyjWzn+CE8uxgg8p4ggd07wp99SOyEhGXY=
git.grassecon.net/grassrootseconomics/sarafu-vise v0.0.0-20250113181554-0d47c096e0d6 h1:65bJrk/h6N4f3JKWlgopW8qduqhMtfI8ZweUS/FxubY=
git.grassecon.net/grassrootseconomics/sarafu-vise v0.0.0-20250113181554-0d47c096e0d6/go.mod h1:XJALQQyuC+ZNgb/iceSmBc0M4FK3y7ZVmrK++LxWkT0=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893 h1:MyDINzwY1sjfXkIFoc+6T5lXF/1xdFV6yjHrpSNZzWM=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7 h1:P+Bi5jcQbnCrLnKNC5k8XDTBsocyZTVifqDyZY6d0w8=
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7/go.mod h1:X9aQF93xw3vcW2QftJfzLewvbotRM0U00DRtdmFw294=
git.grassecon.net/grassrootseconomics/sarafu-vise v0.0.0-20250113213915-63eb803022af h1:iwJ4iiF3Wbmkob++qCfj67URtdV2i6pdb9IkvmnBvQw=
git.grassecon.net/grassrootseconomics/sarafu-vise v0.0.0-20250113213915-63eb803022af/go.mod h1:1WryKcUnFI1J4Jl3wZJ4DX/7lnew+0PDY8xWvqc46UI=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=

View File

@ -1,35 +0,0 @@
package testutil
import (
"context"
"errors"
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/db"
)
// TestStorageService wraps db for nats subscription.
type TestStorageService struct {
Store db.Db
}
// GetUserdataDb implements grassrootseconomics/visedriver/storage.StorageService.
func(ss *TestStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
return ss.Store, nil
}
// GetPersister implements grassrootseconomics/visedriver/storage.StorageService.
func(ts *TestStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
return persist.NewPersister(ts.Store), nil
}
// GetResource implements grassrootseconomics/visedriver/storage.StorageService.
func(ts *TestStorageService) GetResource(ctx context.Context) (resource.Resource, error) {
return nil, errors.New("not implemented")
}
// EnsureDbDir implements grassrootseconomics/visedriver/storage.StorageService.
func(ss *TestStorageService) EnsureDbDir() error {
return nil
}