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:
Masih H. Derkani 2024-03-01 13:54:29 +00:00 committed by Rod Vagg
parent f9fef2ad75
commit 85584a44fb

View File

@ -14,10 +14,11 @@ 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",
testJsonMarshalling(
ActorEvent{
Entries: []EventEntry{ Entries: []EventEntry{
{ {
Key: "key1", Key: "key1",
@ -35,53 +36,14 @@ func TestActorEventJson(t *testing.T) {
Height: 1001, Height: 1001,
TipSetKey: NewTipSetKey(randomCid(t, rng)), TipSetKey: NewTipSetKey(randomCid(t, rng)),
MsgCid: 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"}}`,
),
)
bz, err := json.Marshal(in) t.Run("actor event filter",
require.NoError(t, err) testJsonMarshalling(
require.NotEmpty(t, bz) ActorEventFilter{
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) {
in := ActorEventBlock{
Codec: 1,
Value: []byte("test"),
}
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) {
tsk := NewTipSetKey(randomCid(t, pseudo.New(pseudo.NewSource(0))))
from := abi.ChainEpoch(0)
to := abi.ChainEpoch(100)
f := ActorEventFilter{
Addresses: []address.Address{ Addresses: []address.Address{
randomF4Addr(t, pseudo.New(pseudo.NewSource(0))), randomF4Addr(t, pseudo.New(pseudo.NewSource(0))),
randomF4Addr(t, pseudo.New(pseudo.NewSource(0))), randomF4Addr(t, pseudo.New(pseudo.NewSource(0))),
@ -100,21 +62,44 @@ func TestSubActorEventFilterJson(t *testing.T) {
}, },
}, },
}, },
FromHeight: &from, FromHeight: heightOf(0),
ToHeight: &to, ToHeight: heightOf(100),
TipSetKey: &tsk, TipSetKey: randomTipSetKey(t, rng),
},
`{"addresses":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromHeight":0,"toHeight":100,"tipsetKey":[{"/":"bafkqacxcqxwocuiukv4aq5i"}]}`,
),
)
t.Run("actor event block",
testJsonMarshalling(
ActorEventBlock{
Codec: 1,
Value: []byte("test"),
},
`{"codec":1,"value":"dGVzdA=="}`,
),
)
} }
bz, err := json.Marshal(f) func testJsonMarshalling[V ActorEvent | ActorEventBlock | ActorEventFilter](subject V, expect string) func(t *testing.T) {
return func(t *testing.T) {
gotMarshalled, err := json.Marshal(subject)
require.NoError(t, err) require.NoError(t, err)
require.NotEmpty(t, bz) require.JSONEqf(t, expect, string(gotMarshalled), "serialization mismatch")
t.Logf("%s", bz) var gotUnmarshalled V
require.NoError(t, json.Unmarshal([]byte(expect), &gotUnmarshalled))
require.Equal(t, subject, gotUnmarshalled)
}
}
s := `{"addresses":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromHeight":0,"toHeight":100,"tipsetKey":[{"/":"bafkqacqbst64f6rp7taeduy"}]}` func heightOf(h int64) *abi.ChainEpoch {
var out ActorEventFilter hp := abi.ChainEpoch(h)
err = json.Unmarshal([]byte(s), &out) return &hp
require.NoError(t, err) }
require.Equal(t, f, out)
func randomTipSetKey(tb testing.TB, rng *pseudo.Rand) *TipSetKey {
tb.Helper()
tk := NewTipSetKey(randomCid(tb, rng))
return &tk
} }
func randomF4Addr(tb testing.TB, rng *pseudo.Rand) address.Address { func randomF4Addr(tb testing.TB, rng *pseudo.Rand) address.Address {