2022-01-20 08:22:06 -08:00
|
|
|
package ormtable_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/testing/protocmp"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/internal/testkv"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/internal/testpb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSingleton(t *testing.T) {
|
2022-01-31 16:23:15 -08:00
|
|
|
table, err := ormtable.Build(ormtable.Options{
|
|
|
|
MessageType: (&testpb.ExampleSingleton{}).ProtoReflect().Type(),
|
2022-01-20 08:22:06 -08:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2022-01-31 16:23:15 -08:00
|
|
|
ctx := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
|
2022-01-20 08:22:06 -08:00
|
|
|
|
2022-02-14 07:23:52 -08:00
|
|
|
store, err := testpb.NewExampleSingletonTable(table)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-31 16:23:15 -08:00
|
|
|
|
|
|
|
val, err := store.Get(ctx)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-31 16:23:15 -08:00
|
|
|
assert.Assert(t, val != nil) // singletons are always set
|
|
|
|
assert.NilError(t, store.Save(ctx, &testpb.ExampleSingleton{}))
|
2022-01-20 08:22:06 -08:00
|
|
|
|
|
|
|
val.Foo = "abc"
|
|
|
|
val.Bar = 3
|
2022-01-31 16:23:15 -08:00
|
|
|
assert.NilError(t, store.Save(ctx, val))
|
2022-01-20 08:22:06 -08:00
|
|
|
|
2022-01-31 16:23:15 -08:00
|
|
|
val2, err := store.Get(ctx)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-31 16:23:15 -08:00
|
|
|
assert.DeepEqual(t, val, val2, protocmp.Transform())
|
2022-01-20 08:22:06 -08:00
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2022-01-31 16:23:15 -08:00
|
|
|
assert.NilError(t, table.ExportJSON(ctx, buf))
|
|
|
|
assert.NilError(t, table.ValidateJSON(bytes.NewReader(buf.Bytes())))
|
2022-01-20 08:22:06 -08:00
|
|
|
store2 := ormtable.WrapContextDefault(testkv.NewSplitMemBackend())
|
2022-01-31 16:23:15 -08:00
|
|
|
assert.NilError(t, table.ImportJSON(store2, bytes.NewReader(buf.Bytes())))
|
2022-01-20 08:22:06 -08:00
|
|
|
|
2022-01-31 16:23:15 -08:00
|
|
|
val3, err := store.Get(ctx)
|
2022-01-20 08:22:06 -08:00
|
|
|
assert.NilError(t, err)
|
2022-01-31 16:23:15 -08:00
|
|
|
assert.DeepEqual(t, val, val3, protocmp.Transform())
|
2022-01-20 08:22:06 -08:00
|
|
|
}
|