aboutsummaryrefslogtreecommitdiffstats
path: root/entity/dag/op_set_metadata_test.go
blob: 4dab8a96746258974429da49c01e170a3be90374 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package dag

import (
	"testing"
	"time"

	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/repository"

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

type snapshotMock struct {
	ops []Operation
}

func (s *snapshotMock) AllOperations() []Operation {
	return s.ops
}

func TestSetMetadata(t *testing.T) {
	snap := &snapshotMock{}

	repo := repository.NewMockRepo()

	rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
	require.NoError(t, err)

	unix := time.Now().Unix()

	target1 := NewNoOpOp[*snapshotMock](1, rene, unix)
	target1.SetMetadata("key", "value")
	snap.ops = append(snap.ops, target1)

	target2 := NewNoOpOp[*snapshotMock](1, rene, unix)
	target2.SetMetadata("key2", "value2")
	snap.ops = append(snap.ops, target2)

	op1 := NewSetMetadataOp[*snapshotMock](2, rene, unix, target1.Id(), map[string]string{
		"key":  "override",
		"key2": "value",
	})

	op1.Apply(snap)
	snap.ops = append(snap.ops, op1)

	target1Metadata := snap.AllOperations()[0].AllMetadata()
	require.Len(t, target1Metadata, 2)
	// original key is not overrided
	require.Equal(t, target1Metadata["key"], "value")
	// new key is set
	require.Equal(t, target1Metadata["key2"], "value")

	target2Metadata := snap.AllOperations()[1].AllMetadata()
	require.Len(t, target2Metadata, 1)
	require.Equal(t, target2Metadata["key2"], "value2")

	op2 := NewSetMetadataOp[*snapshotMock](2, rene, unix, target2.Id(), map[string]string{
		"key2": "value",
		"key3": "value3",
	})

	op2.Apply(snap)
	snap.ops = append(snap.ops, op2)

	target1Metadata = snap.AllOperations()[0].AllMetadata()
	require.Len(t, target1Metadata, 2)
	require.Equal(t, target1Metadata["key"], "value")
	require.Equal(t, target1Metadata["key2"], "value")

	target2Metadata = snap.AllOperations()[1].AllMetadata()
	require.Len(t, target2Metadata, 2)
	// original key is not overrided
	require.Equal(t, target2Metadata["key2"], "value2")
	// new key is set
	require.Equal(t, target2Metadata["key3"], "value3")

	op3 := NewSetMetadataOp[*snapshotMock](2, rene, unix, target1.Id(), map[string]string{
		"key":  "override",
		"key2": "override",
	})

	op3.Apply(snap)
	snap.ops = append(snap.ops, op3)

	target1Metadata = snap.AllOperations()[0].AllMetadata()
	require.Len(t, target1Metadata, 2)
	// original key is not overrided
	require.Equal(t, target1Metadata["key"], "value")
	// previously set key is not overrided
	require.Equal(t, target1Metadata["key2"], "value")

	target2Metadata = snap.AllOperations()[1].AllMetadata()
	require.Len(t, target2Metadata, 2)
	require.Equal(t, target2Metadata["key2"], "value2")
	require.Equal(t, target2Metadata["key3"], "value3")
}

func TestSetMetadataSerialize(t *testing.T) {
	SerializeRoundTripTest(t, func(author identity.Interface, unixTime int64) *SetMetadataOperation[*snapshotMock] {
		return NewSetMetadataOp[*snapshotMock](1, author, unixTime, "message", map[string]string{
			"key1": "value1",
			"key2": "value2",
		})
	})
}