op_create_test.go

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