aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/operation_testing.go
blob: 0ca47d4b5dd065454c11e71e84c699e13256745b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package dag

import (
	"encoding/json"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/MichaelMure/git-bug/entities/identity"
	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/repository"
)

// 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,
	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, resolvers := maker(rene, time.Now().Unix())
	// enforce having an id
	op.Id()

	data, err := json.Marshal(op)
	require.NoError(t, err)

	after, err := unmarshaler(data, resolvers)
	require.NoError(t, err)

	// Set the id from the serialized data
	after.setId(entity.DeriveId(data))
	// Set the author, as OperationPack would do
	after.setAuthor(rene)

	require.Equal(t, op, after)
}