1package bug
 2
 3import (
 4	"encoding/json"
 5	"testing"
 6	"time"
 7
 8	"github.com/MichaelMure/git-bug/identity"
 9	"github.com/MichaelMure/git-bug/util/timestamp"
10	"github.com/stretchr/testify/assert"
11)
12
13func TestCreate(t *testing.T) {
14	snapshot := Snapshot{}
15
16	rene := identity.NewBare("René Descartes", "rene@descartes.fr")
17	unix := time.Now().Unix()
18
19	create := NewCreateOp(rene, unix, "title", "message", nil)
20
21	create.Apply(&snapshot)
22
23	hash, err := create.Hash()
24	assert.NoError(t, err)
25
26	comment := Comment{
27		id:       string(hash),
28		Author:   rene,
29		Message:  "message",
30		UnixTime: timestamp.Timestamp(create.UnixTime),
31	}
32
33	expected := Snapshot{
34		Title: "title",
35		Comments: []Comment{
36			comment,
37		},
38		Author:       rene,
39		Participants: []identity.Interface{rene},
40		Actors:       []identity.Interface{rene},
41		CreatedAt:    create.Time(),
42		Timeline: []TimelineItem{
43			&CreateTimelineItem{
44				CommentTimelineItem: NewCommentTimelineItem(hash, comment),
45			},
46		},
47	}
48
49	assert.Equal(t, expected, snapshot)
50}
51
52func TestCreateSerialize(t *testing.T) {
53	var rene = identity.NewBare("René Descartes", "rene@descartes.fr")
54	unix := time.Now().Unix()
55	before := NewCreateOp(rene, unix, "title", "message", nil)
56
57	data, err := json.Marshal(before)
58	assert.NoError(t, err)
59
60	var after CreateOperation
61	err = json.Unmarshal(data, &after)
62	assert.NoError(t, err)
63
64	assert.Equal(t, before, &after)
65}