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 Phi-rjan
parent 318695a44c
commit 718026a8c3

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=="}`,
),
)
}
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.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(f) func heightOf(h int64) *abi.ChainEpoch {
require.NoError(t, err) hp := abi.ChainEpoch(h)
require.NotEmpty(t, bz) return &hp
t.Logf("%s", bz) }
s := `{"addresses":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromHeight":0,"toHeight":100,"tipsetKey":[{"/":"bafkqacqbst64f6rp7taeduy"}]}` func randomTipSetKey(tb testing.TB, rng *pseudo.Rand) *TipSetKey {
var out ActorEventFilter tb.Helper()
err = json.Unmarshal([]byte(s), &out) tk := NewTipSetKey(randomCid(tb, rng))
require.NoError(t, err) return &tk
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 {