Remove duplicate code from actor event type marshalling tests
Reduce verbosity and remove duplicate test logic from actor event types JSON marshalling tests.
This commit is contained in:
parent
318695a44c
commit
718026a8c3
@ -14,107 +14,92 @@ import (
|
|||||||
builtintypes "github.com/filecoin-project/go-state-types/builtin"
|
builtintypes "github.com/filecoin-project/go-state-types/builtin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestActorEventJson(t *testing.T) {
|
func TestJSONMarshalling(t *testing.T) {
|
||||||
// generate a mock Actor event for me
|
|
||||||
rng := pseudo.New(pseudo.NewSource(0))
|
rng := pseudo.New(pseudo.NewSource(0))
|
||||||
in := ActorEvent{
|
t.Run("actor event with entries",
|
||||||
Entries: []EventEntry{
|
testJsonMarshalling(
|
||||||
{
|
ActorEvent{
|
||||||
Key: "key1",
|
Entries: []EventEntry{
|
||||||
Codec: 0x51,
|
{
|
||||||
Value: []byte("value1"),
|
Key: "key1",
|
||||||
|
Codec: 0x51,
|
||||||
|
Value: []byte("value1"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Key: "key2",
|
||||||
|
Codec: 0x52,
|
||||||
|
Value: []byte("value2"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Emitter: randomF4Addr(t, rng),
|
||||||
|
Reverted: false,
|
||||||
|
Height: 1001,
|
||||||
|
TipSetKey: NewTipSetKey(randomCid(t, rng)),
|
||||||
|
MsgCid: randomCid(t, rng),
|
||||||
},
|
},
|
||||||
{
|
`{"entries":[{"Flags":0,"Key":"key1","Codec":81,"Value":"dmFsdWUx"},{"Flags":0,"Key":"key2","Codec":82,"Value":"dmFsdWUy"}],"emitter":"f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","reverted":false,"height":1001,"tipsetKey":[{"/":"bafkqacx3dag26sfht3qlcdi"}],"msgCid":{"/":"bafkqacrziziykd6uuf4islq"}}`,
|
||||||
Key: "key2",
|
),
|
||||||
Codec: 0x52,
|
)
|
||||||
Value: []byte("value2"),
|
|
||||||
|
t.Run("actor event filter",
|
||||||
|
testJsonMarshalling(
|
||||||
|
ActorEventFilter{
|
||||||
|
Addresses: []address.Address{
|
||||||
|
randomF4Addr(t, pseudo.New(pseudo.NewSource(0))),
|
||||||
|
randomF4Addr(t, pseudo.New(pseudo.NewSource(0))),
|
||||||
|
},
|
||||||
|
Fields: map[string][]ActorEventBlock{
|
||||||
|
"key1": {
|
||||||
|
{
|
||||||
|
Codec: 0x51,
|
||||||
|
Value: []byte("value1"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"key2": {
|
||||||
|
{
|
||||||
|
Codec: 0x52,
|
||||||
|
Value: []byte("value2"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
FromHeight: heightOf(0),
|
||||||
|
ToHeight: heightOf(100),
|
||||||
|
TipSetKey: randomTipSetKey(t, rng),
|
||||||
},
|
},
|
||||||
},
|
`{"addresses":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromHeight":0,"toHeight":100,"tipsetKey":[{"/":"bafkqacxcqxwocuiukv4aq5i"}]}`,
|
||||||
Emitter: randomF4Addr(t, rng),
|
),
|
||||||
Reverted: false,
|
)
|
||||||
Height: 1001,
|
t.Run("actor event block",
|
||||||
TipSetKey: NewTipSetKey(randomCid(t, rng)),
|
testJsonMarshalling(
|
||||||
MsgCid: randomCid(t, rng),
|
ActorEventBlock{
|
||||||
}
|
Codec: 1,
|
||||||
|
Value: []byte("test"),
|
||||||
bz, err := json.Marshal(in)
|
},
|
||||||
require.NoError(t, err)
|
`{"codec":1,"value":"dGVzdA=="}`,
|
||||||
require.NotEmpty(t, bz)
|
),
|
||||||
|
)
|
||||||
var out ActorEvent
|
|
||||||
err = json.Unmarshal(bz, &out)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, in, out)
|
|
||||||
|
|
||||||
s := `
|
|
||||||
{"entries":[{"Flags":0,"Key":"key1","Codec":81,"Value":"dmFsdWUx"},{"Flags":0,"Key":"key2","Codec":82,"Value":"dmFsdWUy"}],"emitter":"f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","reverted":false,"height":1001,"tipsetKey":[{"/":"bafkqacx3dag26sfht3qlcdi"}],"msgCid":{"/":"bafkqacrziziykd6uuf4islq"}}
|
|
||||||
`
|
|
||||||
var out2 ActorEvent
|
|
||||||
err = json.Unmarshal([]byte(s), &out2)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, out, out2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestActorEventBlockJson(t *testing.T) {
|
func testJsonMarshalling[V ActorEvent | ActorEventBlock | ActorEventFilter](subject V, expect string) func(t *testing.T) {
|
||||||
in := ActorEventBlock{
|
return func(t *testing.T) {
|
||||||
Codec: 1,
|
gotMarshalled, err := json.Marshal(subject)
|
||||||
Value: []byte("test"),
|
require.NoError(t, err)
|
||||||
|
require.JSONEqf(t, expect, string(gotMarshalled), "serialization mismatch")
|
||||||
|
var gotUnmarshalled V
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(expect), &gotUnmarshalled))
|
||||||
|
require.Equal(t, subject, gotUnmarshalled)
|
||||||
}
|
}
|
||||||
|
|
||||||
bz, err := json.Marshal(in)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEmpty(t, bz)
|
|
||||||
|
|
||||||
var out ActorEventBlock
|
|
||||||
err = json.Unmarshal(bz, &out)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, in, out)
|
|
||||||
|
|
||||||
var out2 ActorEventBlock
|
|
||||||
s := "{\"codec\":1,\"value\":\"dGVzdA==\"}"
|
|
||||||
err = json.Unmarshal([]byte(s), &out2)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, in, out2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSubActorEventFilterJson(t *testing.T) {
|
func heightOf(h int64) *abi.ChainEpoch {
|
||||||
tsk := NewTipSetKey(randomCid(t, pseudo.New(pseudo.NewSource(0))))
|
hp := abi.ChainEpoch(h)
|
||||||
from := abi.ChainEpoch(0)
|
return &hp
|
||||||
to := abi.ChainEpoch(100)
|
}
|
||||||
f := ActorEventFilter{
|
|
||||||
Addresses: []address.Address{
|
|
||||||
randomF4Addr(t, pseudo.New(pseudo.NewSource(0))),
|
|
||||||
randomF4Addr(t, pseudo.New(pseudo.NewSource(0))),
|
|
||||||
},
|
|
||||||
Fields: map[string][]ActorEventBlock{
|
|
||||||
"key1": {
|
|
||||||
{
|
|
||||||
Codec: 0x51,
|
|
||||||
Value: []byte("value1"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"key2": {
|
|
||||||
{
|
|
||||||
Codec: 0x52,
|
|
||||||
Value: []byte("value2"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
FromHeight: &from,
|
|
||||||
ToHeight: &to,
|
|
||||||
TipSetKey: &tsk,
|
|
||||||
}
|
|
||||||
|
|
||||||
bz, err := json.Marshal(f)
|
func randomTipSetKey(tb testing.TB, rng *pseudo.Rand) *TipSetKey {
|
||||||
require.NoError(t, err)
|
tb.Helper()
|
||||||
require.NotEmpty(t, bz)
|
tk := NewTipSetKey(randomCid(tb, rng))
|
||||||
t.Logf("%s", bz)
|
return &tk
|
||||||
|
|
||||||
s := `{"addresses":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromHeight":0,"toHeight":100,"tipsetKey":[{"/":"bafkqacqbst64f6rp7taeduy"}]}`
|
|
||||||
var out ActorEventFilter
|
|
||||||
err = json.Unmarshal([]byte(s), &out)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, f, out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomF4Addr(tb testing.TB, rng *pseudo.Rand) address.Address {
|
func randomF4Addr(tb testing.TB, rng *pseudo.Rand) address.Address {
|
||||||
|
Loading…
Reference in New Issue
Block a user