1package bug
2
3import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "github.com/stretchr/testify/require"
9
10 "github.com/MichaelMure/git-bug/entity"
11 "github.com/MichaelMure/git-bug/identity"
12 "github.com/MichaelMure/git-bug/repository"
13 "github.com/MichaelMure/git-bug/util/timestamp"
14)
15
16func TestCreate(t *testing.T) {
17 snapshot := Snapshot{}
18
19 repo := repository.NewMockRepoClock()
20
21 rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
22 require.NoError(t, err)
23
24 unix := time.Now().Unix()
25
26 create := NewCreateOp(rene, unix, "title", "message", nil)
27
28 create.Apply(&snapshot)
29
30 id := create.Id()
31 require.NoError(t, id.Validate())
32
33 comment := Comment{
34 id: entity.CombineIds(create.Id(), create.Id()),
35 Author: rene,
36 Message: "message",
37 UnixTime: timestamp.Timestamp(create.UnixTime),
38 }
39
40 expected := Snapshot{
41 id: create.Id(),
42 Title: "title",
43 Comments: []Comment{
44 comment,
45 },
46 Author: rene,
47 Participants: []identity.Interface{rene},
48 Actors: []identity.Interface{rene},
49 CreateTime: create.Time(),
50 Timeline: []TimelineItem{
51 &CreateTimelineItem{
52 CommentTimelineItem: NewCommentTimelineItem(comment),
53 },
54 },
55 }
56
57 require.Equal(t, expected, snapshot)
58}
59
60func TestCreateSerialize(t *testing.T) {
61 repo := repository.NewMockRepo()
62
63 rene, err := identity.NewIdentity(repo, "René Descartes", "rene@descartes.fr")
64 require.NoError(t, err)
65
66 unix := time.Now().Unix()
67 before := NewCreateOp(rene, unix, "title", "message", nil)
68
69 data, err := json.Marshal(before)
70 require.NoError(t, err)
71
72 var after CreateOperation
73 err = json.Unmarshal(data, &after)
74 require.NoError(t, err)
75
76 // enforce creating the ID
77 before.Id()
78
79 // Replace the identity as it's not serialized
80 after.Author_ = rene
81
82 require.Equal(t, before, &after)
83}