aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/operation_testing.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-08-24 21:29:17 +0200
committerMichael Muré <batolettre@gmail.com>2022-08-24 21:31:48 +0200
commite1b172aaf0e984791b5af4a64b144339fd4042f6 (patch)
tree1306f7f617968e13e6f28eda51417ae8cd2c6c6a /entity/dag/operation_testing.go
parentccb71fea57bb17f267f135cb008470615595c5b7 (diff)
downloadgit-bug-e1b172aaf0e984791b5af4a64b144339fd4042f6.tar.gz
dag: test op serialisation with the unmarshaller, to allow resolving entities
Diffstat (limited to 'entity/dag/operation_testing.go')
-rw-r--r--entity/dag/operation_testing.go38
1 files changed, 12 insertions, 26 deletions
diff --git a/entity/dag/operation_testing.go b/entity/dag/operation_testing.go
index 6ebdcae8..0ca47d4b 100644
--- a/entity/dag/operation_testing.go
+++ b/entity/dag/operation_testing.go
@@ -14,44 +14,30 @@ import (
// SerializeRoundTripTest realize a marshall/unmarshall round-trip in the same condition as with OperationPack,
// and check if the recovered operation is identical.
-func SerializeRoundTripTest[OpT Operation](t *testing.T, maker func(author identity.Interface, unixTime int64) OpT) {
+func SerializeRoundTripTest[OpT Operation](
+ t *testing.T,
+ unmarshaler OperationUnmarshaler,
+ maker func(author identity.Interface, unixTime int64) (OpT, entity.Resolvers),
+) {
repo := repository.NewMockRepo()
rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
require.NoError(t, err)
- op := maker(rene, time.Now().Unix())
+ op, resolvers := maker(rene, time.Now().Unix())
// enforce having an id
op.Id()
- rdt := &roundTripper[OpT]{Before: op, author: rene}
-
- data, err := json.Marshal(rdt)
+ data, err := json.Marshal(op)
require.NoError(t, err)
- err = json.Unmarshal(data, &rdt)
+ after, err := unmarshaler(data, resolvers)
require.NoError(t, err)
- require.Equal(t, op, rdt.after)
-}
-
-type roundTripper[OpT Operation] struct {
- Before OpT
- author identity.Interface
- after OpT
-}
-
-func (r *roundTripper[OpT]) MarshalJSON() ([]byte, error) {
- return json.Marshal(r.Before)
-}
-
-func (r *roundTripper[OpT]) UnmarshalJSON(data []byte) error {
- if err := json.Unmarshal(data, &r.after); err != nil {
- return err
- }
// Set the id from the serialized data
- r.after.setId(entity.DeriveId(data))
+ after.setId(entity.DeriveId(data))
// Set the author, as OperationPack would do
- r.after.setAuthor(r.author)
- return nil
+ after.setAuthor(rene)
+
+ require.Equal(t, op, after)
}