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 CreatedAt: create.Time(),
40 Timeline: []TimelineItem{
41 &CreateTimelineItem{
42 CommentTimelineItem: NewCommentTimelineItem(hash, comment),
43 },
44 },
45 }
46
47 assert.Equal(t, expected, snapshot)
48}
49
50func TestCreateSerialize(t *testing.T) {
51 var rene = identity.NewBare("René Descartes", "rene@descartes.fr")
52 unix := time.Now().Unix()
53 before := NewCreateOp(rene, unix, "title", "message", nil)
54
55 data, err := json.Marshal(before)
56 assert.NoError(t, err)
57
58 var after CreateOperation
59 err = json.Unmarshal(data, &after)
60 assert.NoError(t, err)
61
62 assert.Equal(t, before, &after)
63}