aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation_pack_test.go
blob: e1388240decd23482606cc1a262fb8747aebedc7 (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
package bug

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

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

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

func TestOperationPackSerialize(t *testing.T) {
	opp := &OperationPack{}

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

	createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
	setTitleOp := NewSetTitleOp(rene, time.Now().Unix(), "title2", "title1")
	addCommentOp := NewAddCommentOp(rene, time.Now().Unix(), "message2", nil)
	setStatusOp := NewSetStatusOp(rene, time.Now().Unix(), ClosedStatus)
	labelChangeOp := NewLabelChangeOperation(rene, time.Now().Unix(), []Label{"added"}, []Label{"removed"})

	opp.Append(createOp)
	opp.Append(setTitleOp)
	opp.Append(addCommentOp)
	opp.Append(setStatusOp)
	opp.Append(labelChangeOp)

	opMeta := NewSetTitleOp(rene, time.Now().Unix(), "title3", "title2")
	opMeta.SetMetadata("key", "value")
	opp.Append(opMeta)

	assert.Equal(t, 1, len(opMeta.Metadata))

	opFile := NewAddCommentOp(rene, time.Now().Unix(), "message", []repository.Hash{
		"abcdef",
		"ghijkl",
	})
	opp.Append(opFile)

	assert.Equal(t, 2, len(opFile.Files))

	data, err := json.Marshal(opp)
	assert.NoError(t, err)

	var opp2 *OperationPack
	err = json.Unmarshal(data, &opp2)
	assert.NoError(t, err)

	ensureIds(opp)
	ensureAuthors(t, opp, opp2)

	assert.Equal(t, opp, opp2)
}

func ensureIds(opp *OperationPack) {
	for _, op := range opp.Operations {
		op.Id()
	}
}

func ensureAuthors(t *testing.T, opp1 *OperationPack, opp2 *OperationPack) {
	require.Equal(t, len(opp1.Operations), len(opp2.Operations))
	for i := 0; i < len(opp1.Operations); i++ {
		op1 := opp1.Operations[i]
		op2 := opp2.Operations[i]

		// ensure we have equivalent authors (IdentityStub vs Identity) then
		// enforce equality
		require.Equal(t, op1.base().Author.Id(), op2.base().Author.Id())
		op1.base().Author = op2.base().Author
	}
}