git-bug/bug/op_create_test.go

59 lines
1.2 KiB
Go
Raw Normal View History

package bug
2018-07-12 22:31:41 +03:00
import (
2019-01-19 21:23:31 +03:00
"encoding/json"
2018-07-12 22:31:41 +03:00
"testing"
"time"
2018-11-21 20:56:12 +03:00
"github.com/MichaelMure/git-bug/identity"
2019-01-19 21:23:31 +03:00
"github.com/stretchr/testify/assert"
2018-07-12 22:31:41 +03:00
)
func TestCreate(t *testing.T) {
snapshot := Snapshot{}
2018-07-12 22:31:41 +03:00
2019-01-19 18:01:06 +03:00
var rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
2018-07-12 22:31:41 +03:00
unix := time.Now().Unix()
create := NewCreateOp(rene, unix, "title", "message", nil)
2018-07-12 22:31:41 +03:00
create.Apply(&snapshot)
2018-07-12 22:31:41 +03:00
hash, err := create.Hash()
assert.NoError(t, err)
2018-09-30 12:00:39 +03:00
comment := Comment{Author: rene, Message: "message", UnixTime: Timestamp(create.UnixTime)}
expected := Snapshot{
2018-07-12 22:31:41 +03:00
Title: "title",
Comments: []Comment{
comment,
2018-07-12 22:31:41 +03:00
},
2018-08-01 03:15:40 +03:00
Author: rene,
CreatedAt: create.Time(),
Timeline: []TimelineItem{
&CreateTimelineItem{
CommentTimelineItem: NewCommentTimelineItem(hash, comment),
},
},
2018-07-12 22:31:41 +03:00
}
assert.Equal(t, expected, snapshot)
2018-07-12 22:31:41 +03:00
}
2019-01-19 21:23:31 +03:00
func TestCreateSerialize(t *testing.T) {
var rene = identity.NewBare("René Descartes", "rene@descartes.fr")
unix := time.Now().Unix()
before := NewCreateOp(rene, unix, "title", "message", nil)
data, err := json.Marshal(before)
assert.NoError(t, err)
var after CreateOperation
err = json.Unmarshal(data, &after)
assert.NoError(t, err)
assert.Equal(t, before, &after)
}