From 85584a44fb130f1d83832ad6f7c61c1d6ab656c2 Mon Sep 17 00:00:00 2001 From: "Masih H. Derkani" Date: Fri, 1 Mar 2024 13:54:29 +0000 Subject: [PATCH] Remove duplicate code from actor event type marshalling tests Reduce verbosity and remove duplicate test logic from actor event types JSON marshalling tests. --- chain/types/actor_event_test.go | 169 +++++++++++++++----------------- 1 file changed, 77 insertions(+), 92 deletions(-) diff --git a/chain/types/actor_event_test.go b/chain/types/actor_event_test.go index 1060c010b..8c50b1717 100644 --- a/chain/types/actor_event_test.go +++ b/chain/types/actor_event_test.go @@ -14,107 +14,92 @@ import ( builtintypes "github.com/filecoin-project/go-state-types/builtin" ) -func TestActorEventJson(t *testing.T) { - // generate a mock Actor event for me +func TestJSONMarshalling(t *testing.T) { rng := pseudo.New(pseudo.NewSource(0)) - in := ActorEvent{ - Entries: []EventEntry{ - { - Key: "key1", - Codec: 0x51, - Value: []byte("value1"), + t.Run("actor event with entries", + testJsonMarshalling( + ActorEvent{ + Entries: []EventEntry{ + { + 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), }, - { - Key: "key2", - Codec: 0x52, - Value: []byte("value2"), + `{"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"}}`, + ), + ) + + 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), }, - }, - Emitter: randomF4Addr(t, rng), - Reverted: false, - Height: 1001, - TipSetKey: NewTipSetKey(randomCid(t, rng)), - MsgCid: randomCid(t, rng), - } - - bz, err := json.Marshal(in) - require.NoError(t, err) - 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) + `{"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 TestActorEventBlockJson(t *testing.T) { - in := ActorEventBlock{ - Codec: 1, - Value: []byte("test"), +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(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{ - 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, - } +func heightOf(h int64) *abi.ChainEpoch { + hp := abi.ChainEpoch(h) + return &hp +} - bz, err := json.Marshal(f) - require.NoError(t, err) - require.NotEmpty(t, bz) - 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"}]}` - var out ActorEventFilter - err = json.Unmarshal([]byte(s), &out) - 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 {