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
f9fef2ad75
commit
85584a44fb
@ -14,10 +14,11 @@ 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{
|
||||
t.Run("actor event with entries",
|
||||
testJsonMarshalling(
|
||||
ActorEvent{
|
||||
Entries: []EventEntry{
|
||||
{
|
||||
Key: "key1",
|
||||
@ -35,53 +36,14 @@ func TestActorEventJson(t *testing.T) {
|
||||
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"}}`,
|
||||
),
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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{
|
||||
t.Run("actor event filter",
|
||||
testJsonMarshalling(
|
||||
ActorEventFilter{
|
||||
Addresses: []address.Address{
|
||||
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,
|
||||
ToHeight: &to,
|
||||
TipSetKey: &tsk,
|
||||
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"}]}`,
|
||||
),
|
||||
)
|
||||
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.NotEmpty(t, bz)
|
||||
t.Logf("%s", bz)
|
||||
require.JSONEqf(t, expect, string(gotMarshalled), "serialization mismatch")
|
||||
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"}]}`
|
||||
var out ActorEventFilter
|
||||
err = json.Unmarshal([]byte(s), &out)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, f, out)
|
||||
func heightOf(h int64) *abi.ChainEpoch {
|
||||
hp := abi.ChainEpoch(h)
|
||||
return &hp
|
||||
}
|
||||
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user