aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/entity_test.go
blob: 6d621bbeb2c418a8996f71e973e2e65c75a6c79b (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package dag

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestWriteRead(t *testing.T) {
	repo, id1, id2, resolver, def := makeTestContext()

	entity := New(def)
	require.False(t, entity.NeedCommit())

	entity.Append(newOp1(id1, "foo"))
	entity.Append(newOp2(id1, "bar"))

	require.True(t, entity.NeedCommit())
	require.NoError(t, entity.CommitAsNeeded(repo))
	require.False(t, entity.NeedCommit())

	entity.Append(newOp2(id2, "foobar"))
	require.True(t, entity.NeedCommit())
	require.NoError(t, entity.CommitAsNeeded(repo))
	require.False(t, entity.NeedCommit())

	read, err := Read(def, repo, resolver, entity.Id())
	require.NoError(t, err)

	assertEqualEntities(t, entity, read)
}

func TestWriteReadMultipleAuthor(t *testing.T) {
	repo, id1, id2, resolver, def := makeTestContext()

	entity := New(def)

	entity.Append(newOp1(id1, "foo"))
	entity.Append(newOp2(id2, "bar"))

	require.NoError(t, entity.CommitAsNeeded(repo))

	entity.Append(newOp2(id1, "foobar"))
	require.NoError(t, entity.CommitAsNeeded(repo))

	read, err := Read(def, repo, resolver, entity.Id())
	require.NoError(t, err)

	assertEqualEntities(t, entity, read)
}

func assertEqualEntities(t *testing.T, a, b *Entity) {
	// testify doesn't support comparing functions and systematically fail if they are not nil
	// so we have to set them to nil temporarily

	backOpUnA := a.Definition.OperationUnmarshaler
	backOpUnB := b.Definition.OperationUnmarshaler

	a.Definition.OperationUnmarshaler = nil
	b.Definition.OperationUnmarshaler = nil

	defer func() {
		a.Definition.OperationUnmarshaler = backOpUnA
		b.Definition.OperationUnmarshaler = backOpUnB
	}()

	require.Equal(t, a, b)
}