SerializeRoundTripTest realize a marshall/unmarshall round-trip in the same condition as with OperationPack, and check if the recovered operation is identical.
( t *testing.T, unmarshaler OperationUnmarshaler, maker func(author identity.Interface, unixTime int64) (OpT, entity.Resolvers), )
| 15 | // SerializeRoundTripTest realize a marshall/unmarshall round-trip in the same condition as with OperationPack, |
| 16 | // and check if the recovered operation is identical. |
| 17 | func SerializeRoundTripTest[OpT Operation]( |
| 18 | t *testing.T, |
| 19 | unmarshaler OperationUnmarshaler, |
| 20 | maker func(author identity.Interface, unixTime int64) (OpT, entity.Resolvers), |
| 21 | ) { |
| 22 | repo := repository.NewMockRepo() |
| 23 | |
| 24 | rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr") |
| 25 | require.NoError(t, err) |
| 26 | |
| 27 | op, resolvers := maker(rene, time.Now().Unix()) |
| 28 | // enforce having an id |
| 29 | op.Id() |
| 30 | |
| 31 | data, err := json.Marshal(op) |
| 32 | require.NoError(t, err) |
| 33 | |
| 34 | after, err := unmarshaler(data, resolvers) |
| 35 | require.NoError(t, err) |
| 36 | |
| 37 | // Set the id from the serialized data |
| 38 | after.setId(entity.DeriveId(data)) |
| 39 | // Set the author, as OperationPack would do |
| 40 | after.setAuthor(rene) |
| 41 | |
| 42 | require.Equal(t, op, after) |
| 43 | } |