Skip to content
Snippets Groups Projects
Commit 6e13c55e authored by Jake Taylor's avatar Jake Taylor
Browse files

added first test and use utils package

parent 805b1c97
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!4Xx 4114/index db
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"github.com/hack-pad/go-indexeddb/idb" "github.com/hack-pad/go-indexeddb/idb"
"github.com/pkg/errors" "github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/xxdk-wasm/utils"
"syscall/js" "syscall/js"
"time" "time"
...@@ -40,17 +41,6 @@ func newContext() (context.Context, context.CancelFunc) { ...@@ -40,17 +41,6 @@ func newContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), dbTimeout) return context.WithTimeout(context.Background(), dbTimeout)
} }
// convertJsonToJs is a helper that converts JSON bytes input
// to a [js.Value] of the object subtype.
func convertJsonToJs(inputJson []byte) (js.Value, error) {
jsObj := make(map[string]interface{})
err := json.Unmarshal(inputJson, &jsObj)
if err != nil {
return js.Value{}, err
}
return js.ValueOf(jsObj), nil
}
// JoinChannel is called whenever a channel is joined locally. // JoinChannel is called whenever a channel is joined locally.
func (w *wasmModel) JoinChannel(channel *cryptoBroadcast.Channel) { func (w *wasmModel) JoinChannel(channel *cryptoBroadcast.Channel) {
parentErr := errors.New("failed to JoinChannel") parentErr := errors.New("failed to JoinChannel")
...@@ -69,7 +59,7 @@ func (w *wasmModel) JoinChannel(channel *cryptoBroadcast.Channel) { ...@@ -69,7 +59,7 @@ func (w *wasmModel) JoinChannel(channel *cryptoBroadcast.Channel) {
"Unable to marshal Channel: %+v", err)) "Unable to marshal Channel: %+v", err))
return return
} }
channelObj, err := convertJsonToJs(newChannelJson) channelObj, err := utils.JsonToJS(newChannelJson)
if err != nil { if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr, jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr,
"Unable to marshal Channel: %+v", err)) "Unable to marshal Channel: %+v", err))
...@@ -240,7 +230,7 @@ func (w *wasmModel) receiveHelper(newMessage *Message) error { ...@@ -240,7 +230,7 @@ func (w *wasmModel) receiveHelper(newMessage *Message) error {
if err != nil { if err != nil {
return errors.Errorf("Unable to marshal Message: %+v", err) return errors.Errorf("Unable to marshal Message: %+v", err)
} }
messageObj, err := convertJsonToJs(newMessageJson) messageObj, err := utils.JsonToJS(newMessageJson)
if err != nil { if err != nil {
return errors.Errorf("Unable to marshal Message: %+v", err) return errors.Errorf("Unable to marshal Message: %+v", err)
} }
...@@ -273,33 +263,43 @@ func (w *wasmModel) receiveHelper(newMessage *Message) error { ...@@ -273,33 +263,43 @@ func (w *wasmModel) receiveHelper(newMessage *Message) error {
} }
// dump is used to output given ObjectStore contents to log for debugging // dump is used to output given ObjectStore contents to log for debugging
func (w *wasmModel) dump(objectStoreName string) { func (w *wasmModel) dump(objectStoreName string) ([]string, error) {
parentErr := errors.Errorf("failed to dump %s", objectStoreName)
txn, err := w.db.Transaction(idb.TransactionReadOnly, objectStoreName) txn, err := w.db.Transaction(idb.TransactionReadOnly, objectStoreName)
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to create Transaction: %+v", err) return nil, errors.WithMessagef(parentErr,
"Unable to create Transaction: %+v", err)
} }
store, err := txn.ObjectStore(objectStoreName) store, err := txn.ObjectStore(objectStoreName)
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to get ObjectStore: %+v", err) return nil, errors.WithMessagef(parentErr,
"Unable to get ObjectStore: %+v", err)
} }
cursorRequest, err := store.OpenCursor(idb.CursorNext) cursorRequest, err := store.OpenCursor(idb.CursorNext)
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to open Cursor: %+v", err) return nil, errors.WithMessagef(parentErr,
"Unable to open Cursor: %+v", err)
} }
// Run the query // Run the query
jww.INFO.Printf("%s values:", objectStoreName) jww.DEBUG.Printf("%s values:", objectStoreName)
results := make([]string, 0)
ctx, cancel := newContext() ctx, cancel := newContext()
err = cursorRequest.Iter(ctx, func(cursor *idb.CursorWithValue) error { err = cursorRequest.Iter(ctx, func(cursor *idb.CursorWithValue) error {
value, err := cursor.Value() value, err := cursor.Value()
if err != nil { if err != nil {
return err return err
} }
jww.INFO.Printf("- %v", js.Global().Get("JSON").Call("stringify", value)) valueStr := utils.JsToJson(value)
results = append(results, valueStr)
jww.DEBUG.Printf("- %v", valueStr)
return nil return nil
}) })
cancel() cancel()
if err != nil { if err != nil {
jww.ERROR.Printf("Failed to dump ObjectStore: %+v", err) return nil, errors.WithMessagef(parentErr,
"Unable to dump ObjectStore: %+v", err)
} }
return results, nil
} }
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
////////////////////////////////////////////////////////////////////////////////
package indexedDb
import (
jww "github.com/spf13/jwalterweatherman"
cryptoBroadcast "gitlab.com/elixxir/crypto/broadcast"
"gitlab.com/xx_network/primitives/id"
"testing"
)
func TestWasmModel_JoinChannel_LeaveChannel(t *testing.T) {
testDbName := "test"
jww.SetStdoutThreshold(jww.LevelTrace)
eventModel, err := newWasmModel(testDbName)
if err != nil {
t.Fatalf("%+v", err)
}
testChannel := &cryptoBroadcast.Channel{
ReceptionID: id.NewIdFromString("test", id.Generic, t),
Name: "test",
Description: "test",
Salt: nil,
RsaPubKey: nil,
}
testChannel2 := &cryptoBroadcast.Channel{
ReceptionID: id.NewIdFromString("test2", id.Generic, t),
Name: "test2",
Description: "test2",
Salt: nil,
RsaPubKey: nil,
}
eventModel.JoinChannel(testChannel)
eventModel.JoinChannel(testChannel2)
results, err := eventModel.dump(channelsStoreName)
if err != nil {
t.Fatalf("%+v", err)
}
if len(results) != 2 {
t.Fatalf("Expected 2 channels to exist")
}
eventModel.LeaveChannel(testChannel.ReceptionID)
results, err = eventModel.dump(channelsStoreName)
if err != nil {
t.Fatalf("%+v", err)
}
if len(results) != 1 {
t.Fatalf("Expected 1 channels to exist")
}
}
...@@ -62,16 +62,15 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value { ...@@ -62,16 +62,15 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value {
} }
} }
// JsonToJS converts a marshalled JSON bytes to a Javascript object. // JsonToJS is a helper that converts JSON bytes input
func JsonToJS(src []byte) (js.Value, error) { // to a [js.Value] of the object subtype.
var inInterface map[string]interface{} func JsonToJS(inputJson []byte) (js.Value, error) {
err := json.Unmarshal(src, &inInterface) jsObj := make(map[string]interface{})
err := json.Unmarshal(inputJson, &jsObj)
if err != nil { if err != nil {
Throw(TypeError, err) return js.Value{}, err
return js.ValueOf(nil), err
} }
return js.ValueOf(jsObj), nil
return js.ValueOf(inInterface), nil
} }
// JsToJson converts the Javascript value to JSON. // JsToJson converts the Javascript value to JSON.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment