git-bug/bug/operation_pack_test.go

65 lines
1.6 KiB
Go
Raw Normal View History

package bug
import (
"encoding/json"
"testing"
2019-02-16 19:32:30 +03:00
"time"
"github.com/stretchr/testify/assert"
2019-02-16 19:32:30 +03:00
"github.com/stretchr/testify/require"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
)
func TestOperationPackSerialize(t *testing.T) {
opp := &OperationPack{}
2019-02-16 19:32:30 +03:00
rene := identity.NewBare("René Descartes", "rene@descartes.fr")
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)
2018-07-13 23:53:53 +03:00
opp.Append(addCommentOp)
opp.Append(setStatusOp)
opp.Append(labelChangeOp)
2019-02-16 19:32:30 +03:00
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)
2019-02-16 19:32:30 +03:00
ensureIDs(t, opp)
2019-02-16 19:32:30 +03:00
assert.Equal(t, opp, opp2)
}
2019-02-16 19:32:30 +03:00
func ensureIDs(t *testing.T, opp *OperationPack) {
2019-02-16 19:32:30 +03:00
for _, op := range opp.Operations {
id := op.Id()
require.NoError(t, id.Validate())
id = op.GetAuthor().Id()
require.NoError(t, id.Validate())
2019-02-16 19:32:30 +03:00
}
}